Class: Motr::Forms::Base
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Motr::Forms::Base
- Defined in:
- lib/motr/forms/base.rb
Overview
Base class for creating form builders
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) current_field_type
Tracks the field currently being "processed".
-
- (Object) field_order
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.
-
- (Object) template
Access the template object.
Instance Method Summary (collapse)
-
- (Boolean) attribute_required?(attribute)
protected
Checks to see if a particular attribute is required, if so, a "required" attribute is added to the field.
-
- (Object) content_tag(tag, content, options = {}, escape = true, &block)
protected
Convenience method to use the content_tag method from our template.
-
- (Boolean) errors_on_attribute?(method)
protected
Checks to see if there are errors for the particular method or attribute.
-
- (Object) fields_for(record_or_name_or_array, *args, &block)
Overrides fields_for to make sure the form builder is set properly.
-
- (Boolean) options_require_validation?(options)
protected
Checks a passed validator to see if it is required 'borrowed' from Formtastic by Justin French (see github.com/justinfrench/formtastic).
-
- (String) render_field_as_custom(helper_method, method, *args)
protected
Wrapper method used by all form fields to customize the output.
-
- (Object) render_field_with_errors(method, html_tag, messages)
protected
Renders the passed html_tag with the custom error_template.
-
- (Boolean) required_by_option?(options)
protected
Checks an options hash to determine if a required method/attribute was overridden manually.
-
- (Boolean) validates_inclusion?(attribute)
protected
Checks for inclusion validation on a particular attribute.
-
- (Boolean) validates_presence?(attribute)
protected
Checks for a presence validation on a particular attribute.
-
- (Boolean) validates_uniqueness?(attribute)
protected
Checks for a uniqueness validation on a particular attribute.
-
- (Object) validators_for(attribute)
protected
Compiles an array of all validators for a particular attribute.
-
- (Boolean) validators_for?(attribute)
protected
Convenience method to see if a particular attribute has validators.
Instance Attribute Details
- (Object) current_field_type
Tracks the field currently being "processed"
16 17 18 |
# File 'lib/motr/forms/base.rb', line 16 def current_field_type @current_field_type end |
- (Object) field_order
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.
14 15 16 |
# File 'lib/motr/forms/base.rb', line 14 def field_order @field_order end |
- (Object) template
Access the template object
11 12 13 |
# File 'lib/motr/forms/base.rb', line 11 def template @template end |
Instance Method Details
- (Boolean) attribute_required?(attribute) (protected)
Checks to see if a particular attribute is required, if so, a "required" attribute is added to the field.
40 41 42 |
# File 'lib/motr/forms/base.rb', line 40 def attribute_required?(attribute) validates_presence?(attribute) || validates_inclusion?(attribute) end |
- (Object) content_tag(tag, content, options = {}, escape = true, &block) (protected)
Convenience method to use the content_tag method from our template
46 47 48 |
# File 'lib/motr/forms/base.rb', line 46 def content_tag(tag, content, = {}, escape = true, &block) #:nodoc: @template.content_tag(tag, content, , escape, &block) end |
- (Boolean) errors_on_attribute?(method) (protected)
Checks to see if there are errors for the particular method or attribute
57 58 59 60 |
# File 'lib/motr/forms/base.rb', line 57 def errors_on_attribute?(method) return false if @object.nil? !(@object.errors.empty? || !@object.errors[method.to_sym].present? || [@object.errors[method.to_sym]].flatten.empty?) end |
- (Object) fields_for(record_or_name_or_array, *args, &block)
Overrides fields_for to make sure the form builder is set properly
21 22 23 24 25 26 27 28 |
# File 'lib/motr/forms/base.rb', line 21 def fields_for(record_or_name_or_array, *args, &block) #:nodoc: opts = args. opts[:builder] ||= Motr::Forms.default_builder args.push(opts) Motr::Forms.with_custom_error_proc do super(record_or_name_or_array, *args, &block) end end |
- (Boolean) options_require_validation?(options) (protected)
Checks a passed validator to see if it is required 'borrowed' from Formtastic by Justin French (see github.com/justinfrench/formtastic)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/motr/forms/base.rb', line 69 def () allow_blank = [:allow_blank] return !allow_blank unless allow_blank.nil? if_condition = ![:if].nil? condition = if_condition ? [:if] : [:unless] condition = if condition.respond_to?(:call) condition.call(@object) elsif condition.is_a?(::Symbol) && @object.respond_to?(condition) @object.send(condition) else condition end if_condition ? !!condition : !condition end |
- (String) render_field_as_custom(helper_method, method, *args) (protected)
Wrapper method used by all form fields to customize the output
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/motr/forms/base.rb', line 107 def render_field_as_custom(helper_method, method, *args) @current_field_type = helper_method = args. (@field_order ||= []) << method # Add an error class to the field if it has errors # if errors_on_attribute?(method) klasses = (.delete(:class) || "").split(" ") klasses << Motr::Forms.error_class [:class] = klasses.join(" ") end # Add a required attribute to the field if it is required # Skip if false was passed as the required option # [:required] = "required" if attribute_required?(method) && .delete(:required).to_s != 'false' ['data-validates-uniqueness'] = "true" if validates_uniqueness?(method) result = send(:_super_#{helper_method}", method, *(args << )) = @object.nil? ? [] : @object.errors[method] render_field_with_errors(method, result, ) end |
- (Object) render_field_with_errors(method, html_tag, messages) (protected)
Renders the passed html_tag with the custom error_template
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/motr/forms/base.rb', line 143 def render_field_with_errors(method, html_tag, ) return html_tag unless errors_on_attribute?(method) error_class = Motr::Forms.error_class = Motr::Forms. render_binding = binding renderer = ERB.new(Motr::Forms.error_template) renderer.result(render_binding).to_s.html_safe end |
- (Boolean) required_by_option?(options) (protected)
Checks an options hash to determine if a required method/attribute was overridden manually
92 93 94 95 |
# File 'lib/motr/forms/base.rb', line 92 def required_by_option?() req = (.is_a?(Hash) ? .stringify_keys[:required] : ) !(req.to_s === 'false' || req.nil?) end |
- (Boolean) validates_inclusion?(attribute) (protected)
Checks for inclusion validation on a particular attribute
203 204 205 |
# File 'lib/motr/forms/base.rb', line 203 def validates_inclusion?(attribute) validator_of_type_exists?(validators_for(attribute), :inclusion) end |
- (Boolean) validates_presence?(attribute) (protected)
Checks for a presence validation on a particular attribute
185 186 187 |
# File 'lib/motr/forms/base.rb', line 185 def validates_presence?(attribute) validator_of_type_exists?(validators_for(attribute), :presence) end |
- (Boolean) validates_uniqueness?(attribute) (protected)
Checks for a uniqueness validation on a particular attribute
194 195 196 |
# File 'lib/motr/forms/base.rb', line 194 def validates_uniqueness?(attribute) validator_of_type_exists?(validators_for(attribute), :uniqueness, false) end |
- (Object) validators_for(attribute) (protected)
Compiles an array of all validators for a particular attribute
161 162 163 164 165 166 167 168 169 |
# File 'lib/motr/forms/base.rb', line 161 def validators_for(attribute) return [] if @object.nil? return [] unless @object.class.respond_to?(:validators_on) attribute = attribute.to_s.sub(/_id$/, '').to_sym @object.class.validators_on(attribute).uniq end |
- (Boolean) validators_for?(attribute) (protected)
Convenience method to see if a particular attribute has validators
176 177 178 |
# File 'lib/motr/forms/base.rb', line 176 def validators_for?(attribute) !validators_for(attribute).empty? end |