module BootstrapFormExtensions::ArrayedField

Constants

ARRAYED_HELPERS

Public Instance Methods

arrayed_json_field(method, fields, **options) click to toggle source
# 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

Private Instance Methods

arrayed_field(field_type, field, **options) click to toggle source
# File lib/bootstrap_form_extensions/arrayed_field.rb, line 46
def arrayed_field field_type, field, **options
  field_tag = "#{field_type}_tag".to_sym

  blueprint = @template.send field_tag, nil, nil, class: 'form-control', data: { name: "#{object_name}[#{field}][]" }
  blueprint = arrayed_field_row_builder blueprint
  blueprint = arrayed_field_blueprint_builder blueprint

  form_group_builder_for_arrayed_field field, blueprint, options do |value|
    input = @template.send field_tag, "#{object_name}[#{field}][]", value, class: 'form-control'
    arrayed_field_row_builder input
  end
end
arrayed_field_blueprint_builder(row) click to toggle source
# File lib/bootstrap_form_extensions/arrayed_field.rb, line 86
def arrayed_field_blueprint_builder row
  content_tag :div, row, class: 'blueprint-for-arrayed-field', style: 'display:none;'
end
arrayed_field_row_builder(args, col_class: 'col') click to toggle source
# File lib/bootstrap_form_extensions/arrayed_field.rb, line 73
def arrayed_field_row_builder args, col_class: 'col'
  args = [ args ].flatten

  inputs = args.inject(''.html_safe) do |content, input|
    content << content_tag(:div, input, class: col_class)
  end

  remove_button = @template.link_to '&times;'.html_safe, 'javascript:void(0);', class: 'btn btn-outline-danger remove-arrayed-field-row'
  remove_button = content_tag :div, remove_button, class: 'col-auto ml-auto'

  content_tag :div, inputs + remove_button, class: 'row mt-1'
end
form_group_builder_for_arrayed_field(method, blueprint, options, &row_builder_block) click to toggle source
# File lib/bootstrap_form_extensions/arrayed_field.rb, line 90
def form_group_builder_for_arrayed_field method, blueprint, options, &row_builder_block
  add_button = @template.link_to '+', 'javascript:void(0);', class: 'btn btn-outline-primary add-arrayed-field-row'
  add_button = content_tag :div, add_button, class: 'col-12'
  add_button = content_tag :div, add_button, class: 'row mt-1'

  rows = object.send(method) rescue []
  rows = [] unless rows.is_a? Array
  rows = rows.inject(''.html_safe) { |content, value| content << row_builder_block.call(value) }
  rows = content_tag :div, rows, class: 'arrayed-field-rows'

  form_group_builder method, options do
    content_tag :div, blueprint + rows + add_button, data: { arrayed_field: true }
  end
end
parse_fields_for_arrayed_json(fields) click to toggle source
# File lib/bootstrap_form_extensions/arrayed_field.rb, line 59
def parse_fields_for_arrayed_json fields
  fields.map do |field|
    if field.is_a? Hash
      name   = field.keys.first
      values = field.values.first
      type   = values.fetch :type, :text
      select_options = values.fetch :options, []
      { name: name, type: type, options: select_options }
    else
      { name: field, type: :text }
    end
  end
end