Class: ActiveAdmin::FormBuilder

Inherits:
Formtastic::FormBuilder
  • 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.



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

#active_admin_input_class_name(as) ⇒ Object (protected)



91
92
93
# File 'lib/active_admin/form_builder.rb', line 91

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

#buttons(*args, &block) ⇒ Object

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



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

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


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

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



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

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


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

def commit_button_with_cancel_link
  content = commit_button
  content << cancel_link
end

#has_many(association, 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
# File 'lib/active_admin/form_builder.rb', line 51

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(method, *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



21
22
23
24
25
# File 'lib/active_admin/form_builder.rb', line 21

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

#input_class(as) ⇒ Object (protected)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/active_admin/form_builder.rb', line 95

def input_class(as)
  @input_classes_cache ||= {}
  @input_classes_cache[as] ||= begin
    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
    end
  end
end

#inputs(*args, &block) ⇒ Object



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

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