Class: Fluffery::Forms::Builder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Fluffery::Forms::Builder
- Includes:
- Utilities
- Defined in:
- lib/fluffery/forms/builder.rb
Instance Attribute Summary collapse
-
#field_order ⇒ Object
Access the template object, and create an accessor which will track the order in which our fields are used in the form.
-
#template ⇒ Object
Access the template object, and create an accessor which will track the order in which our fields are used in the form.
Instance Method Summary collapse
-
#button(value = nil, options = {}) ⇒ Object
Creates a html button tag for use in forms instead of the default input submit.
-
#date_select(method, options = {}, html_options = {}) ⇒ Object
Renders the default date_select but with an error wrapping if the method in question has errors.
- #email_field(method, options = {}) ⇒ Object
-
#initialize(object_name, object, template, options, proc) ⇒ Builder
constructor
Sets up options custom to our form builder.
-
#label(method, text = nil, options = {}, &block) ⇒ Object
Custom label tag including an asterisk if the field is required.
-
#number_field(method, range, options = {}) ⇒ Object
HTML5 Number field, again, falls back to text in unsupportive browsers.
- #password_field(method, options = {}) ⇒ Object
- #select(method, choices, options = {}, html_options = {}) ⇒ Object
- #state_select(method, options = {}, html_options = {}) ⇒ Object
- #text_area(method, options = {}) ⇒ Object
- #text_field(method, options = {}) ⇒ Object
- #url_field(method, options = {}) ⇒ Object
Methods included from Utilities
#confirm_or_disable, #content_tag, #error_console, #option_exists?, #render_with_fluff, #validator, #without_error_proc
Constructor Details
#initialize(object_name, object, template, options, proc) ⇒ Builder
Sets up options custom to our form builder. It also overrides the default error proc so we can use something more custom.
15 16 17 18 19 20 |
# File 'lib/fluffery/forms/builder.rb', line 15 def initialize(object_name, object, template, , proc) @field_order ||= [] without_error_proc do super(object_name, object, template, , proc) end end |
Instance Attribute Details
#field_order ⇒ Object
Access the template object, and create an accessor which will track the order in which our fields are used in the form. This way we have the ability to capture that state for re-use in emails etc.
10 11 12 |
# File 'lib/fluffery/forms/builder.rb', line 10 def field_order @field_order end |
#template ⇒ Object
Access the template object, and create an accessor which will track the order in which our fields are used in the form. This way we have the ability to capture that state for re-use in emails etc.
10 11 12 |
# File 'lib/fluffery/forms/builder.rb', line 10 def template @template end |
Instance Method Details
#button(value = nil, options = {}) ⇒ Object
Creates a html button tag for use in forms instead of the default input submit. Uses a span to wrap the content so it can be styled with css.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fluffery/forms/builder.rb', line 25 def (value = nil, = {}) value, = nil, value if value.is_a?(Hash) value ||= submit_default_value = confirm_or_disable() = Fluffery::Utils::Internal.merge_html_classes(, 'button') content_tag(:button, .reverse_merge!({ "type" => "submit", "name" => "commit" })) do content_tag(:span, value.to_s) end end |
#date_select(method, options = {}, html_options = {}) ⇒ Object
Renders the default date_select but with an error wrapping if the method in question has errors
66 67 68 69 70 |
# File 'lib/fluffery/forms/builder.rb', line 66 def date_select(method, = {}, = {}) render_with_fluff(method, , ) do super(method, , ) end end |
#email_field(method, options = {}) ⇒ Object
57 58 59 60 61 |
# File 'lib/fluffery/forms/builder.rb', line 57 def email_field(method, = {}) render_with_fluff(method, ) do super(method, .merge!('type' => 'email')) end end |
#label(method, text = nil, options = {}, &block) ⇒ Object
Custom label tag including an asterisk if the field is required.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fluffery/forms/builder.rb', line 40 def label(method, text = nil, = {}, &block) , text = text, nil if text.is_a?(Hash) text ||= method.to_s.humanize .stringify_keys! .reverse_merge!('transform' => :titleize) unless option_exists?(['transform']) text.send(['transform'].to_sym) .delete('transform') # Check to see if :required is set on a label, or if the attribute/method has a validator that require it exists. # If so, add a * to the label. text = "#{text} <abbr title='Required'>*</abbr>".html_safe if validator.attribute_required?(method, ) super(method, text, , &block) end |
#number_field(method, range, options = {}) ⇒ Object
HTML5 Number field, again, falls back to text in unsupportive browsers.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fluffery/forms/builder.rb', line 74 def number_field(method, range, = {}) , range = range, nil if range.is_a?(Hash) .stringify_keys! unless range.nil? range = (range.is_a?(Range)) ? range.to_a : range .merge!('min' => range.first, 'max' => range.last) end render_with_fluff(method, ) do super(method, .merge!('type' => 'number')) end end |
#password_field(method, options = {}) ⇒ Object
88 89 90 91 92 |
# File 'lib/fluffery/forms/builder.rb', line 88 def password_field(method, = {}) render_with_fluff(method, ) do super(method, ) end end |
#select(method, choices, options = {}, html_options = {}) ⇒ Object
94 95 96 97 98 |
# File 'lib/fluffery/forms/builder.rb', line 94 def select(method, choices, = {}, = {}) render_with_fluff(method, , ) do @template.select(@object_name, method, choices, , ) end end |
#state_select(method, options = {}, html_options = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/fluffery/forms/builder.rb', line 100 def state_select(method, = {}, = {}) = [['Please Select',nil]] << ['International', (int || 'International')] if int = .delete(:international) [ ['Alabama', "AL"],['Alaska', "AK"],['Arizona', "AZ"],['Arkansas', "AR"],['California', "CA"],['Colorado', "CO"], ['Connecticut', "CT"],['District of Columbia', "DC"],['Delaware', "DE"],['Florida', "FL"],['Georgia', "GA"], ['Hawaii', "HI"],['Idaho', "ID"],['Illinois', "IL"],['Indiana', "IN"],['Iowa', "IA"],['Kansas', "KS"],['Kentucky', "KY"], ['Louisiana', "LA"],['Maine', "ME"],['Maryland', "MD"],['Massachusetts', "MA"],['Michigan', "MI"],['Minnesota', "MN"], ['Mississippi', "MS"],['Missouri', "MO"],['Montana', "MT"],['Nebraska', "NE"],['Nevada', "NV"],['New Hampshire', "NH"], ['New Jersey', "NJ"],['New Mexico', "NM"],['New York', "NY"],['North Carolina', "NC"],['North Dakota', "ND"], ['Ohio', "OH"],['Oklahoma', "OK"],['Oregon', "OR"],['Pennsylvania', "PA"],['Rhode Island', "RI"],['South Carolina', "SC"], ['South Dakota', "SD"],['Tennessee', "TN"],['Texas', "TX"],['Utah', "UT"],['Vermont', "VT"],['Virginia', "VA"],['Washington', "WA"], ['West Virginia', "WV"],['Wisconsin', "WI"],['Wyoming', "WY"] ].each do |state| should_abbr = .delete(:abbreviate) << (should_abbr ? state : [state.first, state.first]) end select(method, @template.(, @object.try(:state)), , ) end |
#text_area(method, options = {}) ⇒ Object
129 130 131 132 133 |
# File 'lib/fluffery/forms/builder.rb', line 129 def text_area(method, = {}) render_with_fluff(method, ) do super(method, ) end end |
#text_field(method, options = {}) ⇒ Object
123 124 125 126 127 |
# File 'lib/fluffery/forms/builder.rb', line 123 def text_field(method, = {}) render_with_fluff(method, ) do super(method, ) end end |
#url_field(method, options = {}) ⇒ Object
135 136 137 138 139 |
# File 'lib/fluffery/forms/builder.rb', line 135 def url_field(method, = {}) render_with_fluff(method, ) do super(method, .merge!('type' => 'url')) end end |