1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Use router_name from scope if available

Devise.available_router_name currently returns either
Devise.router_name or :main_app. As such, any redirecting is done
within either of those contexts. Which leads to undesirable redirects
for scopes that reside in an isolate_namespace mounted engine.

This commit makes it possible for FailureApp’s redirect behavior to be
performed in the context of the router_name given to devise_for.

Test case added to cover undesirable behavior. Without change to
lib/devise/failure_app.rb, test case throws exception.
This commit is contained in:
cipater 2015-03-30 16:18:05 -07:00
parent 79c6f47ad3
commit 15d3fc497c
2 changed files with 25 additions and 1 deletions

View file

@ -120,7 +120,8 @@ module Devise
opts[:script_name] = config.relative_url_root
end
context = send(Devise.available_router_name)
router_name = Devise.mappings[scope].router_name || Devise.available_router_name
context = send(router_name)
if context.respond_to?(route)
context.send(route, opts)

View file

@ -26,6 +26,22 @@ class FailureTest < ActiveSupport::TestCase
end
end
class FakeEngineApp < Devise::FailureApp
class FakeEngine
def new_user_on_engine_session_url _
'/user_on_engines/sign_in'
end
end
def main_app
raise 'main_app router called instead of fake_engine'
end
def fake_engine
@fake_engine ||= FakeEngine.new
end
end
def self.context(name, &block)
instance_eval(&block)
end
@ -85,6 +101,13 @@ class FailureTest < ActiveSupport::TestCase
end
end
test 'returns to the default redirect location considering the router for supplied scope' do
call_failure app: FakeEngineApp, 'warden.options' => { scope: :user_on_engine }
assert_equal 302, @response.first
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
assert_equal 'http://test.host/user_on_engines/sign_in', @response.second['Location']
end
if Rails.application.config.respond_to?(:relative_url_root)
test 'returns to the default redirect location considering the relative url root' do
swap Rails.application.config, relative_url_root: "/sample" do