diff --git a/lib/devise/controllers/filters.rb b/lib/devise/controllers/filters.rb index e5bde1d0..79ab2aae 100644 --- a/lib/devise/controllers/filters.rb +++ b/lib/devise/controllers/filters.rb @@ -58,7 +58,7 @@ module Devise # sign_in @user # sign_in(resource) # def sign_in(resource_or_scope, resource=nil) - scope ||= find_devise_scope(resource_or_scope) + scope ||= Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope warden.set_user(resource, :scope => scope) end @@ -72,7 +72,7 @@ module Devise # sign_out @user # sign_out(resource) # def sign_out(resource_or_scope) - scope = find_devise_scope(resource_or_scope) + scope = Devise::Mapping.find_scope!(resource_or_scope) warden.user(scope) # Without loading user here, before_logout hook is not called warden.raw_session.inspect # Without this inspect here. The session does not clear. warden.logout(scope) @@ -86,7 +86,7 @@ module Devise # redirect_to stored_location_for(:user) || root_path # def stored_location_for(resource_or_scope) - scope = find_devise_scope(resource_or_scope) + scope = Devise::Mapping.find_scope!(resource_or_scope) session.delete(:"#{scope}.return_to") end @@ -134,16 +134,6 @@ module Devise METHODS end - protected - - def find_devise_scope(resource_or_scope) #:nodoc: - if resource_or_scope.is_a?(Symbol) - resource_or_scope - else - Devise::Mapping.find_by_class!(resource_or_scope.class).name - end - end - end end end diff --git a/lib/devise/mapping.rb b/lib/devise/mapping.rb index 3e3d94dd..2ecd7089 100644 --- a/lib/devise/mapping.rb +++ b/lib/devise/mapping.rb @@ -39,11 +39,17 @@ module Devise Devise.mappings.values.find { |m| return m if klass <= m.to } end - # Find by class but raising an error in case it can't be found. - def self.find_by_class!(klass) - mapping = find_by_class(klass) - raise "Could not find a valid mapping for #{klass}" unless mapping - mapping + # Receives an object and find a scope for it. If a scope cannot be found, + # raises an error. If a symbol is given, it's considered to be the scope. + def self.find_scope!(duck) + if duck.is_a?(Symbol) + duck + else + klass = duck.is_a?(Class) ? duck : duck.class + mapping = Devise::Mapping.find_by_class(klass) + raise "Could not find a valid mapping for #{duck}" unless mapping + mapping.name + end end # Default url options which can be used as prefix. diff --git a/lib/devise/test_helpers.rb b/lib/devise/test_helpers.rb index 143eac9a..3ff7461c 100644 --- a/lib/devise/test_helpers.rb +++ b/lib/devise/test_helpers.rb @@ -64,7 +64,7 @@ module Devise # sign_in @user # sign_in(resource) # def sign_in(resource_or_scope, resource=nil) - scope ||= find_devise_scope(resource_or_scope) + scope ||= Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope session["warden.user.#{scope}.key"] = resource.class.serialize_into_session(resource) end @@ -77,19 +77,9 @@ module Devise # sign_out @user # sign_out(resource) # def sign_out(resource_or_scope) - scope = find_devise_scope(resource_or_scope) + scope = Devise::Mapping.find_scope!(resource_or_scope) warden.logout(scope) end - protected - - def find_devise_scope(resource_or_scope) #:nodoc: - if resource_or_scope.is_a?(Symbol) - resource_or_scope - else - Devise::Mapping.find_by_class!(resource_or_scope.class).name - end - end - end end diff --git a/test/mapping_test.rb b/test/mapping_test.rb index 6c5946f2..ce515dbc 100644 --- a/test/mapping_test.rb +++ b/test/mapping_test.rb @@ -49,9 +49,15 @@ class MappingTest < ActiveSupport::TestCase assert_equal Devise.mappings[:user], Devise::Mapping.find_by_class(klass) end - test 'find mapping raises an error for invalid class' do + test 'find scope for a given object' do + assert_equal :user, Devise::Mapping.find_scope!(User) + assert_equal :user, Devise::Mapping.find_scope!(:user) + assert_equal :user, Devise::Mapping.find_scope!(User.new) + end + + test 'find scope raises an error if cannot be found' do assert_raise RuntimeError do - Devise::Mapping.find_by_class!(String) + Devise::Mapping.find_scope!(String) end end