module Sequel::Plugins::ValidateAssociated::InstanceMethods

Private Instance Methods

associated_object_valid?(reflection, obj) click to toggle source

Whether the associated object is valid. By default, the same as calling valid? on the object, but exists so it can be overridden.

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