Class: ActiveAdmin::FormBuilder

Inherits:
Formtastic::FormBuilder
  • Object
show all
Defined in:
lib/active_admin/form_builder.rb

Direct Known Subclasses

ActiveAdmin::Filters::FormBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FormBuilder



13
14
15
16
# File 'lib/active_admin/form_builder.rb', line 13

def initialize(*args)
  @form_buffers = ["".html_safe]
  super
end

Instance Attribute Details

#form_buffersObject (readonly)

Returns the value of attribute form_buffers



11
12
13
# File 'lib/active_admin/form_builder.rb', line 11

def form_buffers
  @form_buffers
end

Instance Method Details

#action(*args) ⇒ Object



42
43
44
# File 'lib/active_admin/form_builder.rb', line 42

def action(*args)
  form_buffers.last << with_new_form_buffer{ super }
end

#actions(*args, &block) ⇒ Object



36
37
38
39
40
# File 'lib/active_admin/form_builder.rb', line 36

def actions(*args, &block)
  form_buffers.last << with_new_form_buffer do
    block_given? ? super : super{ commit_action_with_cancel_link }
  end
end

#active_admin_input_class_name(as) ⇒ Object (protected)



115
116
117
# File 'lib/active_admin/form_builder.rb', line 115

def active_admin_input_class_name(as)
  "ActiveAdmin::Inputs::#{as.to_s.camelize}Input"
end


30
31
32
33
34
# File 'lib/active_admin/form_builder.rb', line 30

def cancel_link(url = {action: "index"}, html_options = {}, li_attrs = {})
  li_attrs[:class] ||= "cancel"
  li_content = template.link_to I18n.t('active_admin.cancel'), url, html_options
  form_buffers.last << template.(:li, li_content, li_attrs)
end


46
47
48
49
# File 'lib/active_admin/form_builder.rb', line 46

def commit_action_with_cancel_link
  action(:submit)
  cancel_link
end

#field_set_and_list_wrapping(*args, &block) ⇒ Object (protected)

This method calls the block it's passed (in our case, the `f.inputs` block) and wraps the resulting HTML in a fieldset. If your block doesn't have a valid return value but it was otherwise built correctly, we instead use the most recent part of the Active Admin form buffer.



140
141
142
143
144
# File 'lib/active_admin/form_builder.rb', line 140

def field_set_and_list_wrapping(*args, &block)
  block_given? ? super{
    (val = yield).is_a?(String) ? val : form_buffers.last
  } : super
end

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



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_admin/form_builder.rb', line 51

def has_many(assoc, options = {}, &block)
  # remove options that should not render as attributes
  custom_settings = :new_record, :allow_destroy, :heading, :sortable
  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(' ')

  # Add Delete Links
  form_block = proc do |has_many_form|
    index    = parent_child_index options[:parent] if options[:parent]
    contents = block.call has_many_form, index

    if has_many_form.object.new_record?
      contents << template.(:li) do
        template.link_to I18n.t('active_admin.has_many_remove'), "#", class: 'button has_many_remove'
      end
    elsif builder_options[:allow_destroy]
      has_many_form.input :_destroy, as: :boolean, wrapper_html: {class: 'has_many_delete'},
                                                   label: I18n.t('active_admin.has_many_delete')
    end

    if builder_options[:sortable]
      has_many_form.input builder_options[:sortable], as: :hidden

      contents << template.(:li, class: 'handle') do
        Iconic.icon :move_vertical
      end
    end

    contents
  end

  # make sure that the sortable children sorted in stable ascending order
  if column = builder_options[:sortable]
    children = object.public_send(assoc).sort_by do |o|
      attribute = o.public_send column
      [attribute.nil? ? Float::INFINITY : attribute, o.id || Float::INFINITY]
    end
    options[:for] = [assoc, children]
  end

  html = without_wrapper do
    unless builder_options.key?(:heading) && !builder_options[:heading]
      form_buffers.last << template.(:h3) do
        builder_options[:heading] || object.class.reflect_on_association(assoc).klass.model_name.human(count: ::ActiveAdmin::Helpers::I18n::PLURAL_MANY_COUNT)
      end
    end

    inputs options, &form_block

    form_buffers.last << js_for_has_many(assoc, form_block, template, builder_options[:new_record], options[:class]) if builder_options[:new_record]
    form_buffers.last
  end

  tag = @already_in_an_inputs_block ? :li : :div
  form_buffers.last << template.(tag, html, class: "has_many_container #{assoc}", 'data-sortable' => builder_options[:sortable])
end

#input(method, *args) ⇒ Object

If this `input` call is inside a `inputs` block, add the content to the form buffer. Else, return it directly.



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

def input(method, *args)
  content = with_new_form_buffer{ super }
  @use_form_buffer ? form_buffers.last << content : content
end

#input_class(as) ⇒ Object (protected)



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/active_admin/form_builder.rb', line 119

def input_class(as)
  @input_classes_cache ||= {}
  @input_classes_cache[as] ||= begin
    begin
      custom_input_class_name(as).constantize
    rescue NameError
      begin
        active_admin_input_class_name(as).constantize
      rescue NameError
        standard_input_class_name(as).constantize
      end
    end
  rescue NameError
    raise Formtastic::UnknownInputError, "Unable to find input class for #{as}"
  end
end

#inputs(*args, &block) ⇒ Object



18
19
20
21
# File 'lib/active_admin/form_builder.rb', line 18

def inputs(*args, &block)
  @use_form_buffer = block_given?
  form_buffers.last << with_new_form_buffer{ super }
end

#semantic_errors(*args) ⇒ Object



109
110
111
# File 'lib/active_admin/form_builder.rb', line 109

def semantic_errors(*args)
  form_buffers.last << with_new_form_buffer{ super }
end