Module: Validations
- Included in:
- Model
- Defined in:
- lib/volt/models/validations.rb
Overview
Include in any class to get validation logic
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#errors(marked_only = false) ⇒ Object
TODO: Errors is being called for any validation change.
-
#mark_field!(field_name, trigger_changed = true) ⇒ Object
Once a field is ready, we can use include_in_errors! to start showing its errors.
- #marked_errors ⇒ Object
- #marked_fields ⇒ Object
Class Method Details
.included(base) ⇒ Object
18 19 20 |
# File 'lib/volt/models/validations.rb', line 18 def self.included(base) base.send :extend, ClassMethods end |
Instance Method Details
#errors(marked_only = false) ⇒ Object
TODO: Errors is being called for any validation change. We should have errors return a hash like object that only calls the validation for each one.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/volt/models/validations.rb', line 38 def errors(marked_only=false) errors = {} validations = self.class.validations if validations # Merge into errors, combining any error arrays merge = Proc.new do |new_errors| errors.merge!(new_errors) do |key, new_val, old_val| new_val + old_val end end # Run through each validation validations.each_pair do |field_name, | if marked_only # When marked only, skip any validations on non-marked fields next unless marked_fields[field_name] end .each_pair do |validation, args| # Call the specific validator, then merge the results back # into one large errors hash. klass = validation_class(validation, args) if klass validate_with(merge, klass, field_name, args) else raise "validation type #{validation} is not specified." end end end end return errors end |
#mark_field!(field_name, trigger_changed = true) ⇒ Object
Once a field is ready, we can use include_in_errors! to start showing its errors.
24 25 26 |
# File 'lib/volt/models/validations.rb', line 24 def mark_field!(field_name, trigger_changed=true) marked_fields[field_name] = true end |
#marked_errors ⇒ Object
32 33 34 |
# File 'lib/volt/models/validations.rb', line 32 def marked_errors errors(true) end |
#marked_fields ⇒ Object
28 29 30 |
# File 'lib/volt/models/validations.rb', line 28 def marked_fields @marked_fields ||= ReactiveHash.new end |