Module: Volt::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
17 18 19 |
# File 'lib/volt/models/validations.rb', line 17 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.
37 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 |
# File 'lib/volt/models/validations.rb', line 37 def errors(marked_only = false) errors = {} validations = self.class.validations if validations # Merge into errors, combining any error arrays merge = proc 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 fail "validation type #{validation} is not specified." end end end end 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.
23 24 25 |
# File 'lib/volt/models/validations.rb', line 23 def mark_field!(field_name, trigger_changed = true) marked_fields[field_name] = true end |
#marked_errors ⇒ Object
31 32 33 |
# File 'lib/volt/models/validations.rb', line 31 def marked_errors errors(true) end |
#marked_fields ⇒ Object
27 28 29 |
# File 'lib/volt/models/validations.rb', line 27 def marked_fields @marked_fields ||= ReactiveHash.new end |