Class: ActiveAdmin::FormBuilder
- Inherits:
-
Formtastic::FormBuilder
- Object
- Formtastic::FormBuilder
- ActiveAdmin::FormBuilder
- Includes:
- DeprecatedMethods
- Defined in:
- lib/active_admin/form_builder.rb
Direct Known Subclasses
Defined Under Namespace
Modules: DeprecatedMethods
Instance Attribute Summary collapse
-
#form_buffers ⇒ Object
readonly
Returns the value of attribute form_buffers.
Instance Method Summary collapse
- #action(*args) ⇒ Object
- #actions(*args, &block) ⇒ Object
- #active_admin_input_class_name(as) ⇒ Object protected
- #cancel_link(url = {:action => "index"}, html_options = {}, li_attrs = {}) ⇒ Object
- #commit_action_with_cancel_link ⇒ Object
-
#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.
- #has_many(association, options = {}, &block) ⇒ Object
-
#initialize(*args) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
-
#input(method, *args) ⇒ Object
If this ‘input` call is inside a `inputs` block, add the content to the form buffer.
-
#input_class_by_trying(as) ⇒ Object
protected
use auto-loading in development environment.
-
#input_class_with_const_defined(as) ⇒ Object
protected
prevent exceptions in production environment for better performance.
- #inputs(*args, &block) ⇒ Object
- #semantic_errors(*args) ⇒ Object
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_buffers ⇒ Object (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)
118 119 120 |
# File 'lib/active_admin/form_builder.rb', line 118 def active_admin_input_class_name(as) "ActiveAdmin::Inputs::#{as.to_s.camelize}Input" end |
#cancel_link(url = {:action => "index"}, html_options = {}, li_attrs = {}) ⇒ Object
23 24 25 26 27 |
# File 'lib/active_admin/form_builder.rb', line 23 def cancel_link(url = {:action => "index"}, = {}, li_attrs = {}) li_attrs[:class] ||= "cancel" li_content = template.link_to I18n.t('active_admin.cancel'), url, form_buffers.last << template.content_tag(:li, li_content, li_attrs) end |
#commit_action_with_cancel_link ⇒ Object
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 happens to return nil (but it otherwise built the form correctly), the below override passes the most recent part of the Active Admin form buffer.
158 159 160 |
# File 'lib/active_admin/form_builder.rb', line 158 def field_set_and_list_wrapping(*args, &block) block_given? ? super{ yield || 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 |
# File 'lib/active_admin/form_builder.rb', line 44 def has_many(association, = {}, &block) = { :for => association }.merge() [:class] ||= "" [: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([:parent]) if [:parent] block.call(has_many_form, index) end if has_many_form.object.new_record? contents += template.content_tag(: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 form_buffers.last << with_new_form_buffer do template.content_tag :div, :class => "has_many #{association}" do form_buffers.last << template.content_tag(:h3, object.class.reflect_on_association(association).klass.model_name.human(:count => 1.1)) inputs , &form_block form_buffers.last << js_for_has_many(association, form_block, template) 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 } @inputs_with_block ? form_buffers.last << content : content end |
#input_class_by_trying(as) ⇒ Object (protected)
use auto-loading in development environment
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_admin/form_builder.rb', line 138 def (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
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_admin/form_builder.rb', line 123 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 |
# File 'lib/active_admin/form_builder.rb', line 11 def inputs(*args, &block) @inputs_with_block = block_given? form_buffers.last << with_new_form_buffer{ super } end |
#semantic_errors(*args) ⇒ Object
78 79 80 |
# File 'lib/active_admin/form_builder.rb', line 78 def semantic_errors(*args) form_buffers.last << with_new_form_buffer{ super } end |