class Formtastic::NamespacedClassFinder
def self.finder_method
def self.finder_method @finder_method ||= use_const_defined? ? :find_with_const_defined : :find_by_trying end
def self.use_const_defined?
def self.use_const_defined? defined?(Rails) && ::Rails.application && ::Rails.application.config.respond_to?(:eager_load) && ::Rails.application.config.eager_load end
def class_name(as)
def class_name(as) as.to_s.camelize end
def find(as)
checks with .const_defined before referencing the constant.
const_missing machinery; the second one instead for production
reference the constant directly, triggering Rails' autoloading
Two finder methods are provided, one for development tries to
Looks up the given reference in the configured namespaces.
def find(as) @cache[as] ||= resolve(as) end
def find_by_trying(class_name)
def find_by_trying(class_name) @namespaces.find do |namespace| begin break namespace.const_get(class_name) rescue NameError end end end
def find_with_const_defined(class_name)
Looks up the given class name in the configured namespaces in order,
def find_with_const_defined(class_name) @namespaces.find do |namespace| if namespace.const_defined?(class_name) break namespace.const_get(class_name) end end end
def finder(class_name) # @private
- Private: -
def finder(class_name) # @private send(self.class.finder_method, class_name) end
def initialize(namespaces)
-
namespaces
(Array
) --
def initialize(namespaces) @namespaces = namespaces.flatten @cache = {} end
def resolve(as)
def resolve(as) class_name = class_name(as) finder(class_name) or raise NotFoundError, "class #{class_name}" end