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
#
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
# Loop through all mappings looking for a map that matches with the requested
@ -63,9 +63,11 @@ module Devise
@plural = 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
@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("/")
@controllers = Hash.new { |h,k| h[k] = "devise/#{k}" }
@ -82,12 +84,8 @@ module Devise
end
# Gives the class the mapping points to.
# Reload mapped class each time when cache_classes is false.
def to
return @to if @to
klass = @klass.constantize
@to = klass if Rails.configuration.cache_classes
klass
@ref.get
end
def strategies

View File

@ -92,10 +92,10 @@ module ActionDispatch::Routing
mapping = Devise.add_model(resource, options)
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
raise unless e.message.include?("undefined method `devise'")
raise_no_devise_method_error!(mapping.klass)
raise_no_devise_method_error!(mapping.class_name)
end
routes = mapping.routes