Method: ActiveAdmin::SimpleForm::SimpleFormBuilder#has_many

Defined in:
lib/activeadmin/simple_form/engine.rb

#has_many(assoc, options = {}, &block) ⇒ Object

-> ActiveAdmin::FormBuilder



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/activeadmin/simple_form/engine.rb', line 47

def has_many(assoc, options = {}, &block)  # -> ActiveAdmin::FormBuilder
  # remove options that should not render as attributes
  custom_settings = :new_record, :allow_destroy, :heading, :sortable, :sortable_start
  builder_options = {new_record: true}.merge! options.slice  *custom_settings
  options         = {for: assoc      }.merge! options.except *custom_settings
  options[:class] = [options[:class], "inputs has_many_fields"].compact.join(' ')
  sortable_column = builder_options[:sortable]
  sortable_start  = builder_options.fetch(:sortable_start, 0)

  if sortable_column
    options[:for] = [assoc, sorted_children(assoc, sortable_column)]
  end

  html = "".html_safe
  unless builder_options.key?(:heading) && !builder_options[:heading]
    html << template.(:h3) do
      builder_options[:heading] || assoc_heading(assoc)
    end
  end

  html << template.capture do
    form_block = proc do |has_many_form|
      index    = parent_child_index options[:parent] if options[:parent]
      block_contents = template.capture do
        block.call(has_many_form, index)
      end
      template.concat(block_contents)
      template.concat has_many_actions(has_many_form, builder_options, "".html_safe)
    end

    template.assigns[:has_many_block] = true
    contents = without_wrapper { inputs(options, &form_block) } || "".html_safe

    if builder_options[:new_record]
      contents << js_for_has_many(assoc, form_block, template, builder_options[:new_record], options[:class])
    else
      contents
    end
  end

  tag = @already_in_an_inputs_block ? :li : :div
  html = template.(tag, html, class: "has_many_container #{assoc}", 'data-sortable' => sortable_column, 'data-sortable-start' => sortable_start)
  template.concat(html) if template.output_buffer
  html
end