Class: Transit::Builders::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Transit::Builders::FormBuilder
- Defined in:
- lib/transit/builders/form_builder.rb
Overview
Base class for creating form builders
Instance Attribute Summary collapse
-
#current_field_type ⇒ Object
Tracks the field currently being “processed”.
-
#field_order ⇒ Object
Tracks the order in which fields are used in the form, this allows the easy rebuilding of the submitted form data when submitted since normally the hash isn’t ordered.
-
#template ⇒ Object
Access the template object.
Instance Method Summary collapse
-
#button(value = nil, options = {}) ⇒ Object
Creates a button tag to be used in a form instead of the default input to help make CSS styling easier.
-
#fields_for(record_or_name_or_array, *args, &block) ⇒ Object
Overrides fields_for to make sure the form builder is set properly.
-
#label(method, text = nil, options = {}, &block) ⇒ Object
Modified label tag to support adding a ‘required’ asterisk to the end of the label.
-
#state_select(method, options = {}, html_options = {}) ⇒ String
Generate a select tag with the 50 US states as options.
Instance Attribute Details
#current_field_type ⇒ Object
Tracks the field currently being “processed”
15 16 17 |
# File 'lib/transit/builders/form_builder.rb', line 15 def current_field_type @current_field_type end |
#field_order ⇒ Object
Tracks the order in which fields are used in the form, this allows the easy rebuilding of the submitted form data when submitted since normally the hash isn’t ordered.
13 14 15 |
# File 'lib/transit/builders/form_builder.rb', line 13 def field_order @field_order end |
#template ⇒ Object
Access the template object
10 11 12 |
# File 'lib/transit/builders/form_builder.rb', line 10 def template @template end |
Instance Method Details
#button(value = nil, options = {}) ⇒ Object
Creates a button tag to be used in a form instead of the default input to help make CSS styling easier
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/transit/builders/form_builder.rb', line 57 def (value = nil, = {}) value, = nil, value if value.is_a?(Hash) value ||= submit_default_value value = [image_tag(icon, :class => 'icon'), value].join(' ') if icon = .delete(:icon) klasses = (.delete(:class) || "").split(" ") klasses << "button" ['class'] = klasses.join(" ") content_tag(:button, value.to_s.html_safe, .reverse_merge!({ "type" => "submit", "name" => "commit" })) end |
#fields_for(record_or_name_or_array, *args, &block) ⇒ Object
Overrides fields_for to make sure the form builder is set properly
72 73 74 75 76 77 |
# File 'lib/transit/builders/form_builder.rb', line 72 def fields_for(record_or_name_or_array, *args, &block) #:nodoc: opts = args. opts.merge!(:builder => Transit::Builders::FormBuilder) args.push(opts) super(record_or_name_or_array, *args, &block) end |
#label(method, text = nil, options = {}, &block) ⇒ Object
Modified label tag to support adding a ‘required’ asterisk to the end of the label. Same params as the original implementation
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/transit/builders/form_builder.rb', line 33 def label(method, text = nil, = {}, &block) #:nodoc: , text = text, nil if text.is_a?(Hash) text ||= method.to_s.humanize .stringify_keys! klasses = (.delete(['class']) || "").split(" ") klasses << 'field_with_errors' if errors_on_attribute?(method) ['class'] = klasses.join(" ") unless klasses.compact.empty? text = "#{text} <abbr title='Required'>*</abbr>".html_safe if attribute_required?(method) || required_by_option?(.delete('required')) super(method, text, , &block) end |
#state_select(method, options = {}, html_options = {}) ⇒ String
Generate a select tag with the 50 US states as options
92 93 94 95 96 |
# File 'lib/transit/builders/form_builder.rb', line 92 def state_select(method, = {}, = {}) abbr = .delete(:abbreviate) abbr = !(abbr.nil? || abbr === false) select(method, @template.((abbr, .delete(:international)), @object.try(:state)), , ) end |