Module: Adminterface::Extensions::HasManyBuilder

Defined in:
lib/adminterface/extensions/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#already_in_an_inputs_blockObject

Force rendering div for the container



123
124
125
# File 'lib/adminterface/extensions/form_builder.rb', line 123

def already_in_an_inputs_block
  false
end

#content_has_many(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/adminterface/extensions/form_builder.rb', line 50

def content_has_many(&block)
  form_block = proc do |form_builder|
    render_has_many_form(form_builder, options[:parent], &block)
  end

  template.assigns[:has_many_block] = true
  contents =
    template. :div, class: "has-many-list" do
      without_wrapper { inputs(options, &form_block) }
    end.html_safe

  js = new_record ? js_for_has_many(options[:class], &form_block) : ""
  contents << js
end

#form_actions(form_builder) ⇒ Object



77
78
79
80
81
# File 'lib/adminterface/extensions/form_builder.rb', line 77

def form_actions(form_builder)
  template. :ol, class: "has-many-actions" do
    template.capture { has_many_actions(form_builder, "".html_safe) }
  end
end

#form_fields(form_builder, parent) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/adminterface/extensions/form_builder.rb', line 65

def form_fields(form_builder, parent)
  index = parent && form_builder.send(:parent_child_index, parent)
  template. :li, class: "has-many-inputs" do
    template.concat(
      template.(:ol) do
        template.capture { yield(form_builder, index) }
      end
    )
    template.concat form_actions(form_builder)
  end
end

#has_many_actions(form_builder, contents) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/adminterface/extensions/form_builder.rb', line 90

def has_many_actions(form_builder, contents)
  if form_builder.object.new_record?
    contents << template.(:li) do
      template.link_to I18n.t("active_admin.has_many_remove"), "#",
        class: "button has_many_remove #{destroy_class}".squish
    end
  elsif allow_destroy?(form_builder.object)
    form_builder.input(:_destroy,
      as: :boolean,
      wrapper_html: {class: "button has_many_delete #{destroy_class}".squish},
      label: I18n.t("active_admin.has_many_delete"))
  end

  contents
end

#initialize(*_args) ⇒ Object



25
26
27
28
# File 'lib/adminterface/extensions/form_builder.rb', line 25

def initialize(*_args)
  super
  @options[:class] = "#{@options[:class]} #{item_class}".squish
end

#js_for_has_many(class_string, &form_block) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/adminterface/extensions/form_builder.rb', line 106

def js_for_has_many(class_string, &form_block)
  assoc_name = assoc_klass.model_name
  placeholder = "NEW_#{assoc_name.to_s.underscore.upcase.tr("/", "_")}_RECORD"
  opts = {
    for: [assoc, assoc_klass.new],
    class: class_string,
    for_options: {child_index: placeholder}
  }
  html = template.capture { __getobj__.send(:inputs_for_nested_attributes, opts, &form_block) }
  text = new_record.is_a?(String) ? new_record : I18n.t("active_admin.has_many_new", model: assoc_name.human)

  template.link_to text, "#", class: "button has_many_add #{new_class}", data: {
    html: CGI.escapeHTML(html).html_safe, placeholder: placeholder
  }
end

#render(&block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/adminterface/extensions/form_builder.rb', line 30

def render(&block)
  html = "".html_safe
  html << render_heading
  html << template.capture { content_has_many(&block) }
  html = wrap_div_or_li(html)
  template.concat(html) if template.output_buffer
  html
end

#render_has_many_form(form_builder, parent, &block) ⇒ Object



45
46
47
48
# File 'lib/adminterface/extensions/form_builder.rb', line 45

def render_has_many_form(form_builder, parent, &block)
  template.concat sortable_action(form_builder)
  template.concat form_fields(form_builder, parent, &block)
end

#render_headingObject



39
40
41
42
43
# File 'lib/adminterface/extensions/form_builder.rb', line 39

def render_heading
  return if heading.blank?

  template.(:div, heading, class: "has-many-heading #{heading_class}")
end

#sortable_action(form_builder) ⇒ Object



83
84
85
86
87
88
# File 'lib/adminterface/extensions/form_builder.rb', line 83

def sortable_action(form_builder)
  return unless sortable_column

  form_builder.input sortable_column, as: :hidden
  template.(:li, "", class: "handle")
end