Module: Volt::Validations

Included in:
Model
Defined in:
lib/volt/models/validations/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



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

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

Instance Method Details

#clear_server_errors(key) ⇒ Object

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



109
110
111
# File 'lib/volt/models/validations/validations.rb', line 109

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.



142
143
144
145
146
147
148
149
150
151
# File 'lib/volt/models/validations/validations.rb', line 142

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

  false
end

#errors(marked_only = false) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/volt/models/validations/validations.rb', line 113

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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/volt/models/validations/validations.rb', line 78

def mark_all_fields!
  # TODO: We can use a Set here, but set was having issues.  Check in a
  # later version of opal.
  fields_to_mark = []

  # Look at each validation
  validations = self.class.validations_to_run
  if validations
    fields_to_mark += validations.keys
  end

  # Also include any current fields
  fields_to_mark += attributes.keys

  fields_to_mark.each do |key|
    mark_field!(key.to_sym)
  end
end

#mark_field!(field_name) ⇒ Object

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



69
70
71
# File 'lib/volt/models/validations/validations.rb', line 69

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

#marked_errorsObject



97
98
99
# File 'lib/volt/models/validations/validations.rb', line 97

def marked_errors
  errors(true)
end

#marked_fieldsObject



73
74
75
# File 'lib/volt/models/validations/validations.rb', line 73

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!



104
105
106
# File 'lib/volt/models/validations/validations.rb', line 104

def server_errors
  @server_errors ||= ReactiveHash.new
end

#validate(field_name = nil, options = nil) ⇒ Object



57
58
59
60
# File 'lib/volt/models/validations/validations.rb', line 57

def validate(field_name = nil, options = nil)
  @instance_validations[field_name] ||= {}
  @instance_validations[field_name].merge!(options)
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.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/volt/models/validations/validations.rb', line 126

def validate!
  errors.clear

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