Class: ActiveAdmin::FormBuilder

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

Direct Known Subclasses

FilterFormBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FormBuilder

Returns a new instance of FormBuilder.



8
9
10
11
# File 'lib/active_admin/form_builder.rb', line 8

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

Instance Attribute Details

#form_buffersObject (readonly)

Returns the value of attribute form_buffers.



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

def form_buffers
  @form_buffers
end

Instance Method Details

#buttons(*args, &block) ⇒ Object

The buttons method always needs to be wrapped in a new buffer



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

def buttons(*args, &block)
  content = with_new_form_buffer do
    block_given? ? super : super { commit_button_with_cancel_link }
  end
  form_buffers.last << content.html_safe
end


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

def cancel_link(url = nil, html_options = {}, li_attributes = {})
  li_attributes[:class] ||= "cancel"
  url ||= {:action => "index"}
  template.(:li, (template.link_to I18n.t('active_admin.cancel'), url, html_options), li_attributes)
end

#commit_button(*args) ⇒ Object



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

def commit_button(*args)
  content = with_new_form_buffer{ super }
  form_buffers.last << content.html_safe
end


48
49
50
51
# File 'lib/active_admin/form_builder.rb', line 48

def commit_button_with_cancel_link
  content = commit_button
  content << cancel_link
end

#datepicker_input(method, options) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/active_admin/form_builder.rb', line 53

def datepicker_input(method, options)
  options = options.dup
  options[:input_html] ||= {}
  options[:input_html][:class] = [options[:input_html][:class], "datepicker"].compact.join(' ')
  options[:input_html][:size] ||= "10"
  string_input(method, options)
end

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



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

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

  # Add Delete Links
  form_block = proc do |has_many_form|
    block.call(has_many_form) + if has_many_form.object.new_record?
                                  template. :li do
                                    template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
                                  end
                                else
                                end
  end

  content = with_new_form_buffer do
    template. :div, :class => "has_many #{association}" do
      form_buffers.last << template.(:h3, association.to_s.titlecase)
      inputs options, &form_block

      # Capture the ADD JS
      js = with_new_form_buffer do
        inputs_for_nested_attributes  :for => [association, object.class.reflect_on_association(association).klass.new],
                                      :class => "inputs has_many_fields",
                                      :for_options => {
                                        :child_index => "NEW_RECORD"
                                      }, &form_block
      end

      js = template.escape_javascript(js)
      js = template.link_to I18n.t('active_admin.has_many_new', :model => association.to_s.singularize.titlecase), "#", :onclick => "$(this).before('#{js}'.replace(/NEW_RECORD/g, new Date().getTime())); return false;", :class => "button"

      form_buffers.last << js.html_safe
    end
  end
  form_buffers.last << content.html_safe
end

#input(*args) ⇒ Object

The input method returns a properly formatted string for its contents, so we want to skip the internal buffering while building up its contents



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

def input(*args)
  content = with_new_form_buffer { super }
  return content.html_safe unless @inputs_with_block
  form_buffers.last << content.html_safe
end

#inputs(*args, &block) ⇒ Object



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

def inputs(*args, &block)
  # Store that we are creating inputs without a block
  @inputs_with_block = block_given? ? true : false
  content = with_new_form_buffer { super }
  form_buffers.last << content.html_safe
end