Module: BootstrapFormExtensions::ArrayedField

Includes:
Helpers
Defined in:
lib/bootstrap_form_extensions/arrayed_field.rb

Constant Summary collapse

ARRAYED_HELPERS =
%w[ url_field text_field ]

Instance Method Summary collapse

Methods included from Helpers

#merge_css_classes, #true?

Instance Method Details

#arrayed_json_field(method, fields, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bootstrap_form_extensions/arrayed_field.rb', line 16

def arrayed_json_field method, fields, **options
  fields = parse_fields_for_arrayed_json fields
  col_class = options.fetch :col_class, 'col-2'

  blueprint = fields.map do |field|
    case field[:type]
    when :select
      @template.select_tag nil, @template.options_for_select(field[:options]), class: 'form-control', data: { name: "#{object_name}[#{method}][][#{field[:name]}]" }
    else
      @template.text_field_tag nil, nil, class: 'form-control', placeholder: field[:name], data: { name: "#{object_name}[#{method}][][#{field[:name]}]" }
    end
  end
  blueprint = arrayed_field_row_builder blueprint, col_class: col_class
  blueprint = arrayed_field_blueprint_builder blueprint

  form_group_builder_for_arrayed_field method, blueprint, options do |values|
    inputs = fields.map do |field|
      case field[:type]
      when :select
        @template.select_tag "#{object_name}[#{method}][][#{field[:name]}]", @template.options_for_select(field[:options], values[field[:name].to_s]), class: 'form-control'
      else
        @template.text_field_tag "#{object_name}[#{method}][][#{field[:name]}]", values[field[:name].to_s], class: 'form-control', placeholder: field[:name]
      end
    end
    arrayed_field_row_builder inputs, col_class: col_class
  end
end