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

Simplify access to router_name using the existing scope lookup.

This commit is contained in:
David Henry 2014-05-05 23:19:03 +01:00
parent 33873426ad
commit 1e8e8516f0
5 changed files with 9 additions and 25 deletions

View file

@ -112,7 +112,8 @@ class Devise::RegistrationsController < DeviseController
# The path used after sign up for inactive accounts. You need to overwrite # The path used after sign up for inactive accounts. You need to overwrite
# this method in your own RegistrationsController. # this method in your own RegistrationsController.
def after_inactive_sign_up_path_for(resource) def after_inactive_sign_up_path_for(resource)
router_name = Devise::Mapping.find_mapping!(resource).router_name scope = Devise::Mapping.find_scope!(resource)
router_name = Devise.mappings[scope].router_name
context = router_name ? send(router_name) : self context = router_name ? send(router_name) : self
context.respond_to?(:root_path) ? context.root_path : "/" context.respond_to?(:root_path) ? context.root_path : "/"
end end

View file

@ -101,9 +101,9 @@ module Devise
# The scope root url to be used when they're signed in. By default, it first # The scope root url to be used when they're signed in. By default, it first
# tries to find a resource_root_path, otherwise it uses the root_path. # tries to find a resource_root_path, otherwise it uses the root_path.
def signed_in_root_path(resource_or_scope) def signed_in_root_path(resource_or_scope)
mapping = Devise::Mapping.find_mapping!(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope)
scope = mapping.scope router_name = Devise.mappings[scope].router_name
router_name = mapping.router_name
home_path = "#{scope}_root_path" home_path = "#{scope}_root_path"
context = router_name ? send(router_name) : self context = router_name ? send(router_name) : self
@ -157,7 +157,8 @@ module Devise
# #
# By default it is the root_path. # By default it is the root_path.
def after_sign_out_path_for(resource_or_scope) def after_sign_out_path_for(resource_or_scope)
router_name = Devise::Mapping.find_mapping!(resource_or_scope).router_name scope = Devise::Mapping.find_scope!(resource_or_scope)
router_name = Devise.mappings[scope].router_name
context = router_name ? send(router_name) : self context = router_name ? send(router_name) : self
context.respond_to?(:root_path) ? context.root_path : "/" context.respond_to?(:root_path) ? context.root_path : "/"
end end

View file

@ -46,9 +46,8 @@ module Devise
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1 class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
def #{method}(resource_or_scope, *args) def #{method}(resource_or_scope, *args)
mapping = Devise::Mapping.find_mapping!(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope)
scope = mapping.scope router_name = Devise.mappings[scope].router_name
router_name = mapping.router_name
context = router_name ? send(router_name) : _devise_route_context context = router_name ? send(router_name) : _devise_route_context
context.send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args) context.send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
end end

View file

@ -43,23 +43,6 @@ module Devise
raise "Could not find a valid mapping for #{obj.inspect}" raise "Could not find a valid mapping for #{obj.inspect}"
end end
# Receives an object and find a mapping for it, then return the routing
# details associated with the mapping. If a scope cannot be found,
# raises an error.
def self.find_mapping!(obj)
case obj
when String, Symbol
scope = obj.to_sym
Devise.mappings.each_value { |m| return RoutingDetails.new(m) if m.name == scope }
when Class
Devise.mappings.each_value { |m| return RoutingDetails.new(m) if obj <= m.to }
else
Devise.mappings.each_value { |m| return RoutingDetails.new(m) if obj.is_a?(m.to) }
end
raise "Could not find a valid mapping for #{obj.inspect}" unless mapping
end
def self.find_by_path!(path, path_type=:fullpath) def self.find_by_path!(path, path_type=:fullpath)
Devise.mappings.each_value { |m| return m if path.include?(m.send(path_type)) } Devise.mappings.each_value { |m| return m if path.include?(m.send(path_type)) }
raise "Could not find a valid mapping for path #{path.inspect}" raise "Could not find a valid mapping for path #{path.inspect}"