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

Use ActiveSupport::Dependencies.ref to abstract constant lookup logic.

This commit is contained in:
José Valim 2010-06-12 20:48:37 +02:00
parent 80895c3b9a
commit c4a4032b6b
2 changed files with 7 additions and 9 deletions

View file

@ -22,7 +22,7 @@ module Devise
# # is the modules included in the class # # is the modules included in the class
# #
class Mapping #:nodoc: class Mapping #:nodoc:
attr_reader :singular, :plural, :path, :controllers, :path_names, :path_prefix, :klass attr_reader :singular, :plural, :path, :controllers, :path_names, :path_prefix, :class_name
alias :name :singular alias :name :singular
# Loop through all mappings looking for a map that matches with the requested # Loop through all mappings looking for a map that matches with the requested
@ -63,9 +63,11 @@ module Devise
@plural = name.to_sym @plural = name.to_sym
@path = (options.delete(:path) || name).to_sym @path = (options.delete(:path) || name).to_sym
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
@singular = (options.delete(:singular) || name.to_s.singularize).to_sym @singular = (options.delete(:singular) || name.to_s.singularize).to_sym
@class_name = (options.delete(:class_name) || name.to_s.classify).to_s
@ref = ActiveSupport::Dependencies.ref(@class_name)
@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/") @path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")
@controllers = Hash.new { |h,k| h[k] = "devise/#{k}" } @controllers = Hash.new { |h,k| h[k] = "devise/#{k}" }
@ -82,12 +84,8 @@ module Devise
end end
# Gives the class the mapping points to. # Gives the class the mapping points to.
# Reload mapped class each time when cache_classes is false.
def to def to
return @to if @to @ref.get
klass = @klass.constantize
@to = klass if Rails.configuration.cache_classes
klass
end end
def strategies def strategies

View file

@ -92,10 +92,10 @@ module ActionDispatch::Routing
mapping = Devise.add_model(resource, options) mapping = Devise.add_model(resource, options)
begin begin
raise_no_devise_method_error!(mapping.klass) unless mapping.to.respond_to?(:devise) raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
rescue NoMethodError => e rescue NoMethodError => e
raise unless e.message.include?("undefined method `devise'") raise unless e.message.include?("undefined method `devise'")
raise_no_devise_method_error!(mapping.klass) raise_no_devise_method_error!(mapping.class_name)
end end
routes = mapping.routes routes = mapping.routes