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

Class Method Details

.included(base) ⇒ Object



29
30
31
32
# File 'lib/volt/models/validations.rb', line 29

def self.included(base)
  base.send :extend, ClassMethods
  base.class_attribute(:custom_validations, :validations)
end

Instance Method Details

#clear_server_errors(key) ⇒ Object

When a field is changed, we want to clear any errors from the server



66
67
68
# File 'lib/volt/models/validations.rb', line 66

def clear_server_errors(key)
  @server_errors.delete(key)
end

#error_in_changed_attributes?Boolean

Returns true if any of the changed fields now has an error

Returns:

  • (Boolean)

    true if one of the changed fields has an error.



102
103
104
105
106
107
108
109
110
111
# File 'lib/volt/models/validations.rb', line 102

def error_in_changed_attributes?
  errs = errors

  changed_attributes.each_pair do |key, _|
    # If any of the fields with errors are also the ones that were
    return true if errs[key]
  end

  return false
end

#errors(marked_only = false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/volt/models/validations.rb', line 70

def errors(marked_only=false)
  @errors ||= Errors.new

  if marked_only
    # Only return the fields that have been marked
    @errors.to_h.select {|key,_| marked_fields[key] }
  else
    @errors
  end
end

#mark_all_fields!Object

Marks all fields, useful for when a model saves.



45
46
47
48
49
50
51
52
# File 'lib/volt/models/validations.rb', line 45

def mark_all_fields!
  validations = self.class.validations
  if validations
    validations.each_key do |key|
      mark_field!(key.to_sym)
    end
  end
end

#mark_field!(field_name) ⇒ Object

Once a field is ready, we can use include_in_errors! to start showing its errors.



36
37
38
# File 'lib/volt/models/validations.rb', line 36

def mark_field!(field_name)
  marked_fields[field_name] = true
end

#marked_errorsObject



54
55
56
# File 'lib/volt/models/validations.rb', line 54

def marked_errors
  errors(true)
end

#marked_fieldsObject



40
41
42
# File 'lib/volt/models/validations.rb', line 40

def marked_fields
  @marked_fields ||= ReactiveHash.new
end

#server_errorsObject

server errors are errors that come back from the server when we save! Any changes to the associated fields will clear the error until another save!



61
62
63
# File 'lib/volt/models/validations.rb', line 61

def server_errors
  @server_errors ||= ReactiveHash.new
end

#validate!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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/volt/models/validations.rb', line 83

def validate!
  errors.clear

  run_validations.then do

    # See if any server errors are in place and merge them in if they are
    if Volt.client?
      errors.merge!(server_errors.to_h)
    end
  end.then do
    run_custom_validations
  end.then do
    # Return the errors object
    errors
  end
end