mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Extract common logic into the same method.
This commit is contained in:
parent
97a7f0ed51
commit
de40777334
4 changed files with 24 additions and 32 deletions
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue