Class: ActiveAdmin::FormBuilder

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

Direct Known Subclasses

ActiveAdmin::Filters::FormBuilder

Defined Under Namespace

Modules: DeprecatedMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DeprecatedMethods

#buttons, #commit_button, #commit_button_with_cancel_link

Constructor Details

#initialize(*args) ⇒ FormBuilder

Returns a new instance of FormBuilder.



6
7
8
9
# File 'lib/active_admin/form_builder.rb', line 6

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

Instance Attribute Details

#form_buffersObject (readonly)

Returns the value of attribute form_buffers.



4
5
6
# File 'lib/active_admin/form_builder.rb', line 4

def form_buffers
  @form_buffers
end

Instance Method Details

#action(*args) ⇒ Object



35
36
37
# File 'lib/active_admin/form_builder.rb', line 35

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

#actions(*args, &block) ⇒ Object



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

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)



129
130
131
# File 'lib/active_admin/form_builder.rb', line 129

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


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

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


39
40
41
42
# File 'lib/active_admin/form_builder.rb', line 39

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.



154
155
156
157
158
# File 'lib/active_admin/form_builder.rb', line 154

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

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



44
45
46
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
# File 'lib/active_admin/form_builder.rb', line 44

def has_many(association, options = {}, &block)
  options = { :for => association, :new_record => true }.merge(options)
  options[:class] ||= ""
  options[:class] << "inputs has_many_fields"

  # Add Delete Links
  form_block = proc do |has_many_form|
    # @see https://github.com/justinfrench/formtastic/blob/2.2.1/lib/formtastic/helpers/inputs_helper.rb#L373
    contents = if block.arity == 1  # for backwards compatibility with REE & Ruby 1.8.x
      block.call(has_many_form)
    else
      index = parent_child_index(options[:parent]) if options[:parent]
      block.call(has_many_form, index)
    end

    if has_many_form.object.new_record?
      contents += template.(:li, :class => 'has_many_delete') do
        template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
      end
    elsif options[:allow_destroy]
      has_many_form.input :_destroy, :as => :boolean, :wrapper_html => {:class => "has_many_remove"},
                                                      :label => I18n.t('active_admin.has_many_remove')

    end

    contents
  end

  form_buffers.last << with_new_form_buffer do
    template. :div, :class => "has_many #{association}" do
      # Allow customization of the nested form heading
      unless options.key?(:heading) && !options[:heading]
        form_heading = options[:heading] ||
          object.class.reflect_on_association(association).klass.model_name.human(:count => 1.1)
        form_buffers.last << template.(:h3, form_heading)
      end

      inputs options, &form_block

      js = options[:new_record] ? js_for_has_many(association, form_block, template) : ""
      form_buffers.last << js.html_safe
    end
  end
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.



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

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

#input_class(as) ⇒ Object (protected)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/active_admin/form_builder.rb', line 133

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



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

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

#semantic_errors(*args) ⇒ Object



89
90
91
# File 'lib/active_admin/form_builder.rb', line 89

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