Module: Validations
- Included in:
- Model
- Defined in:
- lib/volt/models/validations/validations.rb,
lib/volt/models/validations/length.rb
Overview
Include in any class to get validation logic
Defined Under Namespace
Modules: ClassMethods Classes: Length
Class Method Summary collapse
Instance Method Summary collapse
- #errors(marked_only = false) ⇒ Object
-
#exclude_from_errors!(field_name) ⇒ Object
Sometimes we want to skip checking a field until some event has happened (usually a field has been typed in or blurred).
-
#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
Class Method Details
.included(base) ⇒ Object
17 18 19 |
# File 'lib/volt/models/validations/validations.rb', line 17 def self.included(base) base.send :extend, ClassMethods end |
Instance Method Details
#errors(marked_only = false) ⇒ Object
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 74 75 76 77 78 79 80 81 |
# File 'lib/volt/models/validations/validations.rb', line 47 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 && @marked_fields[field_name] end .each_pair do |validation, args| # Call the specific validator, then merge the results back # into one large errors hash. case validation when :length merge.call(Length.validate(self, field_name, args)) end end end end # puts "ERROR: #{errors.inspect}" return errors end |
#exclude_from_errors!(field_name) ⇒ Object
Sometimes we want to skip checking a field until some event has happened (usually a field has been typed in or blurred)
23 24 25 26 27 28 29 30 |
# File 'lib/volt/models/validations/validations.rb', line 23 def exclude_from_errors!(field_name) @exclude_from_errors ||= {} @exclude_from_errors[field_name] = true @include_in_errors.delete(field_name) if @include_in_errors trigger_for_methods!('changed', :errors, :marked_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.
34 35 36 37 38 39 40 41 |
# File 'lib/volt/models/validations/validations.rb', line 34 def mark_field!(field_name, trigger_changed=true) @marked_fields ||= {} @marked_fields[field_name] = true if trigger_changed trigger_for_methods!('changed', :errors, :marked_errors) end end |
#marked_errors ⇒ Object
43 44 45 |
# File 'lib/volt/models/validations/validations.rb', line 43 def marked_errors errors(true) end |