module Sequel::Plugins::ValidateAssociated::InstanceMethods

Private Instance Methods

delay_validate_associated_object(reflection, obj) click to toggle source

Delay validating the associated object until validating the current object.

   # File lib/sequel/plugins/validate_associated.rb
47 def delay_validate_associated_object(reflection, obj)
48   after_validation_hook{validate_associated_object(reflection, obj)}
49 end
validate_associated_object(reflection, obj) click to toggle source

Validate the given associated object, adding any validation error messages from the given object to the parent object.

   # File lib/sequel/plugins/validate_associated.rb
53 def validate_associated_object(reflection, obj)
54   return if reflection[:validate] == false
55   association = reflection[:name]
56   if (reflection[:type] == :one_to_many || reflection[:type] == :one_to_one) && (key = reflection[:key]).is_a?(Symbol) && !(pk_val = obj.values[key])
57     p_key = pk unless pk.is_a?(Array)
58     if p_key
59       obj.values[key] = p_key
60     else
61       ignore_key_errors = true
62     end
63   end
64 
65   unless obj.valid?
66     if ignore_key_errors
67       # Ignore errors on the key column in the associated object. This column
68       # will be set when saving to a presumably valid value using a column
69       # in the current object (which may not be available until after the current
70       # object is saved).
71       obj.errors.delete(key)
72       obj.errors.delete_if{|k,| Array === k && k.include?(key)}
73     end
74 
75     obj.errors.full_messages.each do |m|
76       errors.add(association, m)
77     end
78   end
79 
80   nil
81 end