Module: CaringForm::FieldContainer
- Defined in:
- lib/caring_form/field_container.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .custom_field_mapping ⇒ Object
- .field_for(type) ⇒ Object
- .included(klass) ⇒ Object
- .register_field(klass, type) ⇒ Object
Instance Method Summary collapse
- #default_field_id(field) ⇒ Object
- #field_definitions ⇒ Object
- #field_id(name) ⇒ Object
- #field_names(&block) ⇒ Object
- #normalize_attribute_value(name, value) ⇒ Object
Class Method Details
.custom_field_mapping ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/caring_form/field_container.rb', line 12 def self.custom_field_mapping { :full_name => :string, :email => :string, :tel => :string, :us_zip_code => :string, :dummy => :string, :looking_for => :select, :metadata => :hidden } end |
.field_for(type) ⇒ Object
8 9 10 |
# File 'lib/caring_form/field_container.rb', line 8 def self.field_for(type) @registered_fields[type] end |
.included(klass) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/caring_form/field_container.rb', line 24 def self.included(klass) klass.instance_eval do extend ClassMethods class << self class_attribute :fields end self.fields = ActiveSupport::OrderedHash.new end end |
.register_field(klass, type) ⇒ Object
3 4 5 6 |
# File 'lib/caring_form/field_container.rb', line 3 def self.register_field(klass, type) @registered_fields ||= {} @registered_fields[type] = klass end |
Instance Method Details
#default_field_id(field) ⇒ Object
50 51 52 |
# File 'lib/caring_form/field_container.rb', line 50 def default_field_id(field) [sanitized_form_name, index_on, field.name].compact.join('_') end |
#field_definitions ⇒ Object
38 39 40 |
# File 'lib/caring_form/field_container.rb', line 38 def field_definitions self.class.fields.values.dup end |
#field_id(name) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/caring_form/field_container.rb', line 42 def field_id(name) if (field = self.class.fields[name]) id = field.input_html[:id] || default_field_id(field) else nil end end |
#field_names(&block) ⇒ Object
34 35 36 |
# File 'lib/caring_form/field_container.rb', line 34 def field_names(&block) self.class.field_names(&block) end |
#normalize_attribute_value(name, value) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/caring_form/field_container.rb', line 54 def normalize_attribute_value(name, value) return value unless (field = self.class.fields[name.to_sym]) case when field.type == :check_box && value.is_a?(Array) value.last else value end end |