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



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

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

#actions(*args, &block) ⇒ Object



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

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

#active_admin_input_class_name(as) ⇒ Object (protected)



127
128
129
# File 'lib/active_admin/form_builder.rb', line 127

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


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

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


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

def commit_action_with_cancel_link
  action(:submit)
  cancel_link
end

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



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

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|
    # @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) do
        template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
      end
    end

    contents
  end

  content = with_new_form_buffer do
    template. :div, :class => "has_many #{association}" do
      form_buffers.last << template.(:h3, object.class.reflect_on_association(association).klass.model_name.human(:count => 1.1))
      inputs options, &form_block

      js = js_for_has_many(association, form_block, template)
      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_by_trying(as) ⇒ Object (protected)

use auto-loading in development environment



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/active_admin/form_builder.rb', line 147

def input_class_by_trying(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
  end
rescue NameError
  raise Formtastic::UnknownInputError
end

#input_class_with_const_defined(as) ⇒ Object (protected)

prevent exceptions in production environment for better performance



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

def input_class_with_const_defined(as)
  input_class_name = custom_input_class_name(as)

  if ::Object.const_defined?(input_class_name)
    input_class_name.constantize
  elsif ActiveAdmin::Inputs.const_defined?(input_class_name)
    active_admin_input_class_name(as).constantize 
  elsif Formtastic::Inputs.const_defined?(input_class_name)
    standard_input_class_name(as).constantize 
  else
    raise Formtastic::UnknownInputError
  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

#semantic_errors(*args) ⇒ Object



86
87
88
89
# File 'lib/active_admin/form_builder.rb', line 86

def semantic_errors(*args)
  content = with_new_form_buffer { super }
  form_buffers.last << content.html_safe unless content.nil?
end