Class: ActionView::Helpers::FormBuilder
- Inherits:
-
Object
- Object
- ActionView::Helpers::FormBuilder
- Defined in:
- lib/record_collection/rails/form_options_helper.rb
Instance Method Summary collapse
-
#add_collection_ids(content) ⇒ Object
This method adds the collection ids to the form if not already added.
-
#collection_ids ⇒ Object
Return inputs for the collection ids.
- #get_data_attributes(attr, options = {}) ⇒ Object
- #get_optional_classes(attr, options = {}) ⇒ Object
- #optional_boolean(attr, options = {}) ⇒ Object
- #optional_check_box(attr, options = {}) ⇒ Object
- #optional_input(attr, options = {}) ⇒ Object
- #optional_text_area(attr, options = {}) ⇒ Object
- #optional_text_field(attr, options = {}) ⇒ Object
Instance Method Details
#add_collection_ids(content) ⇒ Object
This method adds the collection ids to the form if not already added. For the lazy peaple that forget to add them manually and use an optional tag
6 7 8 9 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 6 def add_collection_ids(content) return content if @collection_ids_already_added collection_ids + content end |
#collection_ids ⇒ Object
Return inputs for the collection ids
12 13 14 15 16 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 12 def collection_ids @collection_ids_already_added = true return "".html_safe unless object.respond_to?(:map) object.map{|record| @template.hidden_field_tag('ids[]', record.id, id: nil) }.join.html_safe end |
#get_data_attributes(attr, options = {}) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 44 def get_data_attributes(attr, = {}) one = true one = object.one? if object.is_a? RecordCollection::Base attrs = {attribute: attr, one: one} attrs[:disabled] = true if [:disabled] attrs end |
#get_optional_classes(attr, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 52 def get_optional_classes(attr, = {}) active = false if object.respond_to?(:uniform_collection_attribute) uniform_value = object.uniform_collection_attribute(attr, set_if_nil: true) # The field is active when the uniform value is not nil active = !uniform_value.nil? end active = true unless object.public_send(attr).nil? # Activate if collection attribute is not nil classes = [[:base_class] || 'optional-input', 'optional-attribute-container', attr] classes << (active ? 'active' : 'inactive') classes << 'disabled' if [:disabled] if object.is_a? RecordCollection::Base classes << 'one' if object.one? else # Assume normal record, this always behaves like one classes << 'one' end classes << 'error' if object.errors[attr].present? classes end |
#optional_boolean(attr, options = {}) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 18 def optional_boolean(attr, = {}) classes = get_optional_classes(attr, .merge(base_class: 'optional-boolean')) label_tag = label(attr, [:label]) check_box_tag = check_box(attr, ) add_collection_ids @template.content_tag(:div, label_tag + check_box_tag , class: classes, data: get_data_attributes(attr, )) end |
#optional_check_box(attr, options = {}) ⇒ Object
25 26 27 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 25 def optional_check_box(attr, = {}) optional_boolean(attr, ) end |
#optional_input(attr, options = {}) ⇒ Object
29 30 31 32 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 29 def optional_input(attr, = {}) classes = get_optional_classes(attr, .reverse_merge(base_class: 'optional-input')) add_collection_ids @template.content_tag(:div, input(attr, ), class: classes, data: get_data_attributes(attr)) end |
#optional_text_area(attr, options = {}) ⇒ Object
39 40 41 42 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 39 def optional_text_area(attr, ={}) classes = get_optional_classes(attr, .reverse_merge(base_class: 'optional-text-area')) add_collection_ids @template.content_tag(:div, label(attr, [:label]) + text_area(attr, ), class: classes, data: get_data_attributes(attr)) end |
#optional_text_field(attr, options = {}) ⇒ Object
34 35 36 37 |
# File 'lib/record_collection/rails/form_options_helper.rb', line 34 def optional_text_field(attr, ={}) classes = get_optional_classes(attr, .reverse_merge(base_class: 'optional-text-field')) add_collection_ids @template.content_tag(:div, label(attr, [:label]) + text_field(attr, ), class: classes, data: get_data_attributes(attr)) end |