Class: Skyline::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Skyline::FormBuilder
- Defined in:
- lib/skyline/form_builder.rb
Overview
Provides error wrapping and some extra helper methods to working with labels and translations easier.
Apart from adding new helper methods, this class overwrites the standard *_field and *_select helpers. If one of the fields has an error, the validation errors for that method are added just after the field. See #wrap_with_error for more information on what get’s added.
Instance Method Summary collapse
-
#dom_id(attribute, options = {}) ⇒ String
The ID that a field for a certain field would get.
-
#fieldset_errors(attribute) ⇒ String?
Generates a list of errors.
-
#has_error?(attribute) ⇒ Boolean
Does the object have any errors set on an attribute.
-
#label(method, text = nil, options = {}) ⇒ String
An improved version of the standard label helper it will append the class “invalid”.
-
#label_with_text(method, options = {}) ⇒ String
Same as label but automatically translates the field name.
-
#object_name_with_index ⇒ String
Add the index of the current scope to the object_name if an index is used.
-
#positioning_field ⇒ Object
Special field to handle automated reordering of elements ( with help of NestedAttributesPositioning module).
-
#t(attribute, options = {}) ⇒ String
Translation for an attribute of that’s used in this formbuilder.
-
#wrap_with_error(method, *parms) {|parameters| ... } ⇒ String
Wrap an input element with errors, also appends “invalid” to the helpers class.
Instance Method Details
#dom_id(attribute, options = {}) ⇒ String
The ID that a field for a certain field would get.
162 163 164 |
# File 'lib/skyline/form_builder.rb', line 162 def dom_id(attribute,={}) CustomInstanceTag.new(@object_name, attribute, @template, ()).to_id(@options) end |
#fieldset_errors(attribute) ⇒ String?
Generates a list of errors. Each error is wrapped in a
<div class="error">...</div>
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/skyline/form_builder.rb', line 146 def fieldset_errors(attribute) return unless @object.errors[attribute].present? out = [] if (errs = @object.errors[attribute]).present? errs = [errs] if self.(errs) out = errs.map do |err| @template.content_tag("div",err,:class => "error") end end out.join("\n").html_safe end |
#has_error?(attribute) ⇒ Boolean
Does the object have any errors set on an attribute.
90 91 92 |
# File 'lib/skyline/form_builder.rb', line 90 def has_error?(attribute) @object.errors[attribute].present? end |
#label(method, text = nil, options = {}) ⇒ String
An improved version of the standard label helper it will append the class “invalid”
43 44 45 46 47 48 49 |
# File 'lib/skyline/form_builder.rb', line 43 def label(method, text = nil, = {}) if @object.errors[method].present? super(method,text,.merge(:class => "invalid #{[:class]}".strip)) else super end end |
#label_with_text(method, options = {}) ⇒ String
Same as label but automatically translates the field name
56 57 58 |
# File 'lib/skyline/form_builder.rb', line 56 def label_with_text(method, = {}) self.label(method, self.t(method), ) end |
#object_name_with_index ⇒ String
Add the index of the current scope to the object_name if an index is used.
97 98 99 100 101 102 103 |
# File 'lib/skyline/form_builder.rb', line 97 def object_name_with_index if [:index].present? self.object_name + "[#{[:index]}]" else self.object_name end end |
#positioning_field ⇒ Object
Special field to handle automated reordering of elements ( with help of NestedAttributesPositioning module)
76 77 78 79 80 81 82 83 84 |
# File 'lib/skyline/form_builder.rb', line 76 def positioning_field name = self.object_name.sub(/\[([^\]]*)_attributes\]$/, "[\\1_position]") + "[]" value = [:index] raise "No options[:index] defined, need one to use for ordering correctly." unless value tag_id = name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "") + value.to_s @template.hidden_field_tag name, value, :id => tag_id end |
#t(attribute, options = {}) ⇒ String
Translation for an attribute of that’s used in this formbuilder. Uses ActiveRecord::Base#human_attribute_name
172 173 174 |
# File 'lib/skyline/form_builder.rb', line 172 def t(attribute, = {}) self.object.class.human_attribute_name(attribute.to_s, ) end |
#wrap_with_error(method, *parms) {|parameters| ... } ⇒ String
Wrap an input element with errors, also appends “invalid” to the helpers class. See #fieldset_errors on how the errors are added.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/skyline/form_builder.rb', line 120 def wrap_with_error(method,*parms,&block) = parms. = .except(:text_suffix) if @object.errors[method].present? [:class] ||= "" [:class] << " invalid" end parms << if .present? html = yield(parms) html << [:text_suffix] if [:text_suffix] if @object.errors[method].present? html + fieldset_errors(method) else html end end |