Skip to content

Include Turbolinks if it is installed#4678

Merged
tegon merged 4 commits intoheartcombo:masterfrom
wnm:patch-1
Mar 29, 2018
Merged

Include Turbolinks if it is installed#4678
tegon merged 4 commits intoheartcombo:masterfrom
wnm:patch-1

Conversation

@wnm
Copy link
Copy Markdown

@wnm wnm commented Oct 23, 2017

Turbolinks is overwriting the redirect_to method of ActionController::Redirecting.

Devise only includes ActionController::Redirecting

So, if turbolinks is installed, it doesn't work will with devise.

This should fix it.

@rafaelfranca
Copy link
Copy Markdown
Collaborator

The failure app should not know about turbolinks. I know it will redirect without turbolinks in the case the authentication fails, but devise should not know anything about turbolinks and fully rendering a failure page is not a performance problem to justify this coupling. Thank you for the pull request.

@wnm
Copy link
Copy Markdown
Author

wnm commented Oct 24, 2017

@rafaelfranca it's not a performance issue, no. But using Turbolinks with the wrappers for Android and iOS requires Turbolinks redirects. The Webview doesn't follow normal redirects.

But I understand you don't want to include this.

For anyone stumbling opon this issue, put this in your devise config:

module Devise
  class FailureApp
    include Turbolinks::Controller
  end
end

@rafaelfranca rafaelfranca reopened this Oct 24, 2017
@rafaelfranca
Copy link
Copy Markdown
Collaborator

rafaelfranca commented Oct 24, 2017

I see. We could take a different approach. Instead of adding that in the controller we could allow people to extend the devise failure app without monkey patching. Something like:

ActiveSupport.run_load_hooks(:devise_failure_app)

in the controller and

ActiveSupport.on_load(:devise_failure_app)
  include Turbolinks::Controller
end

in the application initializer.

But I guess this case is too common to justify we doing that part in the devise engine if Turbolinks is defined.

@tegon
Copy link
Copy Markdown
Member

tegon commented Mar 24, 2018

I agree we can provide a lazy load hook. And it seems it shouldn't be much hard to do it.
If I understood it right, we just need to add ActiveSupport.run_load_hooks(:devise_failure_app, self) to the end of the failure app class.

@wnm If you're willing to change this implementation to use a lazy load hook, we'd happily accept the pull request. Otherwise we can implement it in another one.

Some related resources:

http://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html
https://simonecarletti.com/blog/2011/04/understanding-ruby-and-rails-lazy-load-hooks/

@wnm
Copy link
Copy Markdown
Author

wnm commented Mar 25, 2018

@tegon I updated the pull request:

  • Added ActiveSupport.run_load_hooks(:devise_failure_app, self) to the end of failure app class.
  • Included config to load Turbolinks::Controller in devise initilizer template (but commented out)

Hope I understood everything correctly?

Comment thread lib/generators/templates/devise.rb Outdated
# ==> Turbolinks configuration
# If your app is using Turbolinks, Turbolinks::Controller needs to be included to make redirection work correctly:
#
# ActiveSupport.on_load(:devise_failure_app)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we're missing a do block here

- # ActiveSupport.on_load(:devise_failure_app)
+ # ActiveSupport.on_load(:devise_failure_app) do

@tegon
Copy link
Copy Markdown
Member

tegon commented Mar 26, 2018

@wnm Thanks! I think that's basically it. I just think we should have a test for this.
One way we can test it is to create a simple module with a method and include it in the failure app using this hook, then we can check if the failure app responds to that method.
If you need help with this test, let me know.

@wnm
Copy link
Copy Markdown
Author

wnm commented Mar 27, 2018

@tegon not sure if the tests are in the right place. let me know if they are not...

@tegon
Copy link
Copy Markdown
Member

tegon commented Mar 29, 2018

@wnm Looks good, thanks again!

@dsandstrom
Copy link
Copy Markdown

This doesn't seem to be required for Rails 6 and may cause confusion. Was trying to troubleshoot and tried to toggle this config on, but it errors for me. My issue ended up being a conflict with my existing user routes.

I'm not using the turbolinks gem so there's no Controller to include. Instead, I installed turbolinks with yarn and it gets loaded in application.js.

Error:

uninitialized constant Turbolinks

More info about rails js: https://prathamesh.tech/2019/08/26/understanding-webpacker-in-rails-6/

Just an fyi. Thanks for the PR/gems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants