class SimpleBootstrapForm::FieldFactory

Public Class Methods

new(builder, template) click to toggle source
# File lib/simple_bootstrap_form/field_factory.rb, line 4
def initialize(builder, template)
  @builder  = builder
  @template = template
end

Public Instance Methods

for_attribute(attr, options) click to toggle source
# File lib/simple_bootstrap_form/field_factory.rb, line 9
def for_attribute(attr, options)
  field_class(attr, options).new(@builder, @template, attr, options)
end

Private Instance Methods

attr_column_type(attr) click to toggle source
# File lib/simple_bootstrap_form/field_factory.rb, line 48
def attr_column_type(attr)
  ActiveSupport::Deprecation.silence do
    @builder.object.try(:column_for_attribute, attr).try(:type) || :string
  end
end
derive_field_class_prefix(attr) click to toggle source
# File lib/simple_bootstrap_form/field_factory.rb, line 38
def derive_field_class_prefix(attr)
  case attr_column_type(attr)
  when :boolean; 'Boolean'
  when :datetime; 'Datetime'
  when :string; string_field_class_prefix_based_on_column_name(attr)
  when :text  ; 'Textarea'
  else 'Text'
  end
end
field_class(attr, options) click to toggle source
# File lib/simple_bootstrap_form/field_factory.rb, line 15
def field_class(attr, options)
  field_class_name(attr, options).constantize
end
field_class_name(attr, options) click to toggle source
# File lib/simple_bootstrap_form/field_factory.rb, line 19
def field_class_name(attr, options)
  type_prefix = field_class_type_prefix attr, options
  class_name = "#{type_prefix}Field"
  @builder.class.fully_qualified_class_name_for_field class_name
end
field_class_type_prefix(attr, options) click to toggle source

Return first half of a field class name, based on the type of the field: ‘Text’, ‘Email’, ‘Password’, ‘Textarea’, ‘Boolean’, etc. Appending ‘Field’ to this gets you a real class, e.g. TextField

# File lib/simple_bootstrap_form/field_factory.rb, line 28
def field_class_type_prefix(attr, options)
  if options[:as]
    options[:as].to_s.capitalize
  elsif options.has_key? :collection
    'Select'
  else
    derive_field_class_prefix attr
  end
end
string_field_class_prefix_based_on_column_name(attr) click to toggle source
# File lib/simple_bootstrap_form/field_factory.rb, line 54
def string_field_class_prefix_based_on_column_name(attr)
  case
  when attr.to_s =~ /email/i    ; 'Email'
  when attr.to_s =~ /password/i ; 'Password'
  else 'Text'
  end
end