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 # There could be a presence validation on the foreign key in the associated model, 58 # which will fail if we validate before saving the current object. If there is 59 # no value for the foreign key, set it to the current primary key value, or a dummy 60 # value of 0 if we haven't saved the current object. 61 p_key = pk unless pk.is_a?(Array) 62 obj.values[key] = p_key || 0 63 key = nil if p_key 64 end 65 obj.errors.full_messages.each{|m| errors.add(association, m)} unless obj.valid? 66 if key && !pk_val 67 # If we used a dummy value of 0, remove it so it doesn't accidently remain. 68 obj.values.delete(key) 69 end 70 end