Class: Sequent::Core::Helpers::AssociationValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- Sequent::Core::Helpers::AssociationValidator
- Defined in:
- lib/sequent/core/helpers/association_validator.rb
Overview
Validator for associations. Typically used in Sequent::Core::Command, Sequent::Core::Event and Sequent::Core::ValueObjects.
Example:
class RegisterForTrainingCommand < UpdateCommand
attrs trainee: Person
validates_presence_of :trainee
validates_with Sequent::Core::Helpers::AssociationValidator, associations: [:trainee]
end
Instance Method Summary collapse
Instance Method Details
#validate(record) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sequent/core/helpers/association_validator.rb', line 20 def validate(record) associations = [:associations] associations = [associations] unless associations.instance_of?(Array) associations.each do |association| next unless association # since ruby 2.0...? value = record.instance_variable_get("@#{association.to_s}") if value && incorrect_type?(value, record, association) record.errors[association] = "is not of type #{record.attributes[association]}" elsif value && value.kind_of?(Array) item_type = record.class.type_for(association).item_type record.errors[association] = "is invalid" if all_valid?(value, item_type) else record.errors[association] = "is invalid" if value && value.invalid? end end end |