class Formtastic::NamespacedClassFinder

This class implements class resolution in a namespace chain. It is used both by Formtastic::Helpers::InputHelper and Formtastic::Helpers::ActionHelper to look up action and input classes.

@example Implementing own class finder

# You can implement own class finder that for example prefixes the class name or uses custom module.
class MyInputClassFinder < Formtastic::NamespacedClassFinder
  def initialize(namespaces)
    super [MyNamespace] + namespaces # first lookup in MyNamespace then the defaults
  end

  private

  def class_name(as)
    "My#{super}Input" # for example MyStringInput
  end
end

# in config/initializers/formtastic.rb
Formtastic::FormBuilder.input_class_finder = MyInputClassFinder

Attributes

namespaces[R]

Public Class Methods

new(namespaces) click to toggle source

@param namespaces [Array<Module>]

# File lib/formtastic/namespaced_class_finder.rb, line 36
def initialize(namespaces)
  @namespaces = namespaces.flatten
  @cache = {}
end
use_const_defined?() click to toggle source
# File lib/formtastic/namespaced_class_finder.rb, line 31
def self.use_const_defined?
  defined?(Rails) && ::Rails.application && ::Rails.application.config.eager_load
end

Public Instance Methods

class_name(as) click to toggle source

Converts symbol to class name Overridden in subclasses to create `StringInput` and `ButtonAction` @example

class_name(:string) == "String"
# File lib/formtastic/namespaced_class_finder.rb, line 63
def class_name(as)
  as.to_s.camelize
end
find(as) click to toggle source

Looks up the given reference in the configured namespaces.

Two finder methods are provided, one for development tries to reference the constant directly, triggering Rails' autoloading const_missing machinery; the second one instead for production checks with .const_defined before referencing the constant.

# File lib/formtastic/namespaced_class_finder.rb, line 48
def find(as)
  @cache[as] ||= resolve(as)
end
resolve(as) click to toggle source
# File lib/formtastic/namespaced_class_finder.rb, line 52
def resolve(as)
  class_name = class_name(as)

  finder(class_name) or raise NotFoundError, "class #{class_name}"
end

Private Instance Methods

find_by_trying(class_name) click to toggle source

Use auto-loading in development environment

# File lib/formtastic/namespaced_class_finder.rb, line 90
def find_by_trying(class_name)
  @namespaces.find do |namespace|
    begin
      break namespace.const_get(class_name)
    rescue NameError
    end
  end
end
find_with_const_defined(class_name) click to toggle source

Looks up the given class name in the configured namespaces in order, returning the first one that has the class name constant defined.

# File lib/formtastic/namespaced_class_finder.rb, line 81
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
finder(class_name) click to toggle source
# File lib/formtastic/namespaced_class_finder.rb, line 70
def finder(class_name) # @private
  find_with_const_defined(class_name)
end