Class: Fluffery::Forms::Validation::Base
- Inherits:
-
Object
- Object
- Fluffery::Forms::Validation::Base
- Defined in:
- lib/fluffery/forms/validation/base.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
- #add_html_attributes(attribute, options) ⇒ Object
-
#attribute_format_validator(attribute) ⇒ Object
Checks to see if a particular attribute contains a Regex format validator.
-
#attribute_required?(attribute, options = nil) ⇒ Boolean
Checks to see if the particular attribute is required, used primarily on labels.
-
#default_messages_for(attribute) ⇒ Object
Looks up the default error message so it may be used in our data-message attribute.
-
#errors_for?(method) ⇒ Boolean
Checks to see if the particular attribute has errors.
-
#initialize(form_object) ⇒ Base
constructor
A new instance of Base.
-
#options_require_validation?(options) ⇒ Boolean
Checks to see if the validation is required Courtesy Justin French and Formtastic.
-
#validators_for(attribute) ⇒ Object
Finds all existing validations for the current object and method.
- #validators_for?(method) ⇒ Boolean
Constructor Details
#initialize(form_object) ⇒ Base
Returns a new instance of Base.
11 12 13 |
# File 'lib/fluffery/forms/validation/base.rb', line 11 def initialize(form_object) @object = form_object end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
9 10 11 |
# File 'lib/fluffery/forms/validation/base.rb', line 9 def object @object end |
Instance Method Details
#add_html_attributes(attribute, options) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/fluffery/forms/validation/base.rb', line 15 def add_html_attributes(attribute, ) = Presence.create(attribute, ) if attribute_required?(attribute) matcher = attribute_format_validator(attribute) = Pattern.create(attribute, , matcher) unless matcher.nil? .reverse_merge!((attribute)) end |
#attribute_format_validator(attribute) ⇒ Object
Checks to see if a particular attribute contains a Regex format validator
39 40 41 42 43 44 |
# File 'lib/fluffery/forms/validation/base.rb', line 39 def attribute_format_validator(attribute) format_validator = validators_for(attribute).detect{ |v| v.kind == :format } return nil unless !format_validator.nil? return nil unless format_validator..has_key?(:with) && format_validator.[:with].is_a?(Regexp) matcher = format_validator.[:with] end |
#attribute_required?(attribute, options = nil) ⇒ Boolean
Checks to see if the particular attribute is required, used primarily on labels.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fluffery/forms/validation/base.rb', line 25 def attribute_required?(attribute, = nil) .stringify_keys! if .is_a?(Hash) unless .nil? return true if .has_key?('required') and ['required'] === true end valid_items = validators_for(attribute).find do |validator| ([:presence, :inclusion].include?(validator.kind)) && (validator..present? ? (validator.) : true) end !valid_items.nil? end |
#default_messages_for(attribute) ⇒ Object
Looks up the default error message so it may be used in our data-message attribute
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fluffery/forms/validation/base.rb', line 48 def (attribute) validators = validators_for(attribute) return {} if validators.empty? = validators.inject({}) do |hash, validator| = validator..has_key?(:message) ? validator.[:message] : MessageBuilder.(@object, attribute, validator) hash.merge!(validator.kind.to_s => ) end attr_hash = {'data-validation-messages' => CGI::escape(.to_json) } # Create a list of data-validates-* attrs on the field so we can catch them with javascript # Skip presence and format because they have their own valid HTML5 attrs. # validators.reject{ |v| v.kind.to_s.match(/(presence|format)/i) }.each{ |v| attr_hash.merge!("data-validates-#{v.kind.to_s}" => 'true') } attr_hash end |
#errors_for?(method) ⇒ Boolean
Checks to see if the particular attribute has errors
68 69 70 |
# File 'lib/fluffery/forms/validation/base.rb', line 68 def errors_for?(method) !(@object.nil? || @object.errors.empty? || !@object.errors.key?(method.to_sym) || [@object.errors[method.to_sym]].flatten.empty?) end |
#options_require_validation?(options) ⇒ Boolean
Checks to see if the validation is required Courtesy Justin French and Formtastic
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fluffery/forms/validation/base.rb', line 75 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 |
#validators_for(attribute) ⇒ Object
Finds all existing validations for the current object and method
94 95 96 97 98 |
# File 'lib/fluffery/forms/validation/base.rb', line 94 def validators_for(attribute) return [] unless !@object.nil? and @object.class.respond_to?(:validators_on) attribute = attribute.to_s.sub(/_id$/, '').to_sym validators = @object.class.validators_on(attribute).uniq end |
#validators_for?(method) ⇒ Boolean
100 101 102 |
# File 'lib/fluffery/forms/validation/base.rb', line 100 def validators_for?(method) !validators_for(method).empty? end |