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, &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']]
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/formatted_form/form_builder.rb', line 37 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") 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 = nil if !is_array [super(method, , checked, unchecked), label, [:class].to_s =~ /inline/] end default_field(:check_box, method, .merge(:choices => choices)) end |
#element(label = false, value = nil, &block) ⇒ Object
Form helper to render generic content as a form field. For example:
form.element 'Label', 'Content'
form.element 'Label do
Content
end
83 84 85 86 |
# File 'lib/formatted_form/form_builder.rb', line 83 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
89 90 91 92 |
# File 'lib/formatted_form/form_builder.rb', line 89 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]]
54 55 56 57 58 59 60 61 |
# File 'lib/formatted_form/form_builder.rb', line 54 def (method, tag_value, = {}) tag_values = tag_value.is_a?(Array) ? tag_value : [tag_value] choices = tag_values.collect do |label, choice| label, choice = label, label if !choice [super(method, choice, ), label, [:class].to_s =~ /inline/] end default_field(:radio_button, method, .merge(:choices => choices)) end |
#select(method, choices, options = {}, html_options = {}) ⇒ Object
Wrapper for the select field
26 27 28 29 30 |
# File 'lib/formatted_form/form_builder.rb', line 26 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'
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/formatted_form/form_builder.rb', line 65 def submit(value = nil, = {}, &block) value, = nil, value if value.is_a?(Hash) value ||= submit_default_value # Add specific bootstrap class [:class] = "#{[:class]} btn".strip [:class] = "#{[:class]} btn-primary" unless [:class] =~ /btn-/ default_field(:submit, nil, ) do super(value, ) end end |