Class: FormattedForm::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- FormattedForm::FormBuilder
- Defined in:
- lib/formatted_form/form_builder.rb
Instance Method Summary collapse
-
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
Same as the old check box but it’s possible to send an array of values form.check_box :color form.check_box :color, {}, ‘white’ form.check_box :color, {}, [‘red’, ‘blue’] form.check_box :color, {}, [[‘Red’, ‘red’], [‘Blue’, ‘blue’]].
-
#element(label = false, value = nil, options = {}, &block) ⇒ Object
Form helper to render generic content as a form field.
-
#fields_for(record_name, record_object = nil, options = {}, &block) ⇒ Object
adding builder class for fields_for.
-
#radio_button(method, tag_value, options = {}) ⇒ Object
Radio button helper.
-
#select(method, choices, options = {}, html_options = {}) ⇒ Object
Wrapper for the select field.
-
#submit(value = nil, options = {}, &block) ⇒ Object
Creates submit button element with appropriate bootstrap classes.
Instance Method Details
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
Same as the old check box but it’s possible to send an array of values
form.check_box :color
form.check_box :color, {}, 'white'
form.check_box :color, {}, ['red', 'blue']
form.check_box :color, {}, [['Red', 'red'], ['Blue', 'blue']]
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/formatted_form/form_builder.rb', line 47 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") return super(method, , checked_value, unchecked_value) if .delete(:builder) == false is_array = checked_value.is_a?(Array) .merge!(:multiple => true) if is_array checked_value = is_array ? checked_value : [[checked_value, checked_value, unchecked_value]] choices = checked_value.collect do |label, checked, unchecked| label, checked = label, label if !checked && is_array checked = label if !checked label = method.to_s.humanize if !is_array match = super(method, , checked, unchecked).match(/(<input .*?\/>)?(<input .*?\/>)/) hidden, input = match[1], match[2] [hidden.to_s.html_safe, input.to_s.html_safe, label] end default_field(:check_box, method, .merge(:choices => choices)) end |
#element(label = false, value = nil, options = {}, &block) ⇒ Object
Form helper to render generic content as a form field. For example:
form.element 'Label', 'Content'
form.element 'Label do
Content
end
91 92 93 94 |
# File 'lib/formatted_form/form_builder.rb', line 91 def element(label = false, value = nil, = {}, &block) = {:label => label, :content => value } default_field(:element, nil, , &block) end |
#fields_for(record_name, record_object = nil, options = {}, &block) ⇒ Object
adding builder class for fields_for
97 98 99 100 |
# File 'lib/formatted_form/form_builder.rb', line 97 def fields_for(record_name, record_object = nil, = {}, &block) [:builder] ||= FormattedForm::FormBuilder super(record_name, record_object, , &block) end |
#radio_button(method, tag_value, options = {}) ⇒ Object
Radio button helper. Optionally it’s possible to specify multiple choices at once:
form. :role, 'admin'
form. :role, ['admin', 'regular']
form. :role, [['Admin', 1], ['Regular', 0]]
68 69 70 71 72 73 74 75 76 |
# File 'lib/formatted_form/form_builder.rb', line 68 def (method, tag_value, = {}) return super(method, tag_value, ) if .delete(:builder) == false tag_values = tag_value.is_a?(Array) ? tag_value : [tag_value] choices = tag_values.collect do |label, choice| label, choice = label, label if choice.nil? [nil, super(method, choice, ), label] end default_field(:radio_button, method, .merge(:choices => choices)) end |
#select(method, choices, options = {}, html_options = {}) ⇒ Object
Wrapper for the select field
36 37 38 39 40 |
# File 'lib/formatted_form/form_builder.rb', line 36 def select(method, choices, = {}, = {}) default_field(:select, method, ) do super(method, choices, , ) end end |
#submit(value = nil, options = {}, &block) ⇒ Object
Creates submit button element with appropriate bootstrap classes.
form.submit, :class => 'btn-danger'
80 81 82 83 84 |
# File 'lib/formatted_form/form_builder.rb', line 80 def submit(value = nil, = {}, &block) default_field(:submit, nil, ) do super(value, ) end end |