Class: ActiveAdmin::FormBuilder
- Inherits:
-
Formtastic::FormBuilder
- Object
- Formtastic::FormBuilder
- ActiveAdmin::FormBuilder
- Defined in:
- lib/active_admin/form_builder.rb
Direct Known Subclasses
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(assoc, 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(as) ⇒ Object protected
- #inputs(*args, &block) ⇒ Object
- #semantic_errors(*args) ⇒ Object
Constructor Details
#initialize(*args) ⇒ FormBuilder
Returns a new instance of FormBuilder.
13 14 15 16 |
# File 'lib/active_admin/form_builder.rb', line 13 def initialize(*args) @form_buffers = ["".html_safe] super end |
Instance Attribute Details
#form_buffers ⇒ Object (readonly)
Returns the value of attribute form_buffers.
11 12 13 |
# File 'lib/active_admin/form_builder.rb', line 11 def form_buffers @form_buffers end |
Instance Method Details
#action(*args) ⇒ Object
42 43 44 |
# File 'lib/active_admin/form_builder.rb', line 42 def action(*args) form_buffers.last << with_new_form_buffer{ super } end |
#actions(*args, &block) ⇒ Object
36 37 38 39 40 |
# File 'lib/active_admin/form_builder.rb', line 36 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)
116 117 118 |
# File 'lib/active_admin/form_builder.rb', line 116 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
30 31 32 33 34 |
# File 'lib/active_admin/form_builder.rb', line 30 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
46 47 48 49 |
# File 'lib/active_admin/form_builder.rb', line 46 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.
141 142 143 144 145 |
# File 'lib/active_admin/form_builder.rb', line 141 def field_set_and_list_wrapping(*args, &block) block_given? ? super{ (val = yield).is_a?(String) ? val : form_buffers.last } : super end |
#has_many(assoc, 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/active_admin/form_builder.rb', line 51 def has_many(assoc, = {}, &block) # remove options that should not render as attributes custom_settings = :new_record, :allow_destroy, :heading, :sortable = {new_record: true}.merge! .slice *custom_settings = {for: assoc }.merge! .except *custom_settings [:class] = [[:class], "inputs has_many_fields"].compact.join(' ') # Add Delete Links form_block = proc do |has_many_form| index = parent_child_index [:parent] if [:parent] contents = block.call has_many_form, index if has_many_form.object.new_record? contents << template.content_tag(:li) do template.link_to I18n.t('active_admin.has_many_remove'), "#", class: 'button has_many_remove' end elsif [:allow_destroy] has_many_form.input :_destroy, as: :boolean, wrapper_html: {class: 'has_many_delete'}, label: I18n.t('active_admin.has_many_delete') end if [:sortable] has_many_form.input [:sortable], as: :hidden contents << template.content_tag(:li, class: 'handle') do Iconic.icon :move_vertical end end contents end # make sure that the sortable children sorted in stable ascending order if column = [:sortable] children = object.send(assoc) children = children.sort_by {|o| [o.send(column), o.id]} [:for] = [assoc, children] end html = without_wrapper do unless .key?(:heading) && ![:heading] form_buffers.last << template.content_tag(:h3) do [:heading] || object.class.reflect_on_association(assoc).klass.model_name.human(count: ::ActiveAdmin::Helpers::I18n::PLURAL_MANY_COUNT) end end inputs , &form_block form_buffers.last << js_for_has_many(assoc, form_block, template, [:new_record], [:class]) if [:new_record] form_buffers.last end form_buffers.last << if @already_in_an_inputs_block template.content_tag :li, html, class: "has_many_container #{assoc}", 'data-sortable' => [:sortable] else template.content_tag :div, html, class: "has_many_container #{assoc}", 'data-sortable' => [:sortable] 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.
25 26 27 28 |
# File 'lib/active_admin/form_builder.rb', line 25 def input(method, *args) content = with_new_form_buffer{ super } @use_form_buffer ? form_buffers.last << content : content end |
#input_class(as) ⇒ Object (protected)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_admin/form_builder.rb', line 120 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
18 19 20 21 |
# File 'lib/active_admin/form_builder.rb', line 18 def inputs(*args, &block) @use_form_buffer = block_given? form_buffers.last << with_new_form_buffer{ super } end |
#semantic_errors(*args) ⇒ Object
110 111 112 |
# File 'lib/active_admin/form_builder.rb', line 110 def semantic_errors(*args) form_buffers.last << with_new_form_buffer{ super } end |