valid? now recursively checks casted models. Added better validation spec coverage.

This commit is contained in:
Peter Gumeson 2009-05-28 12:18:23 -07:00
parent 4a4cae0d95
commit 9a026997dd
4 changed files with 106 additions and 26 deletions

View file

@ -115,8 +115,7 @@ module CouchRest
# Check if a resource is valid in a given context
#
def valid?(context = :default)
result = self.class.validators.execute(context, self)
result && validate_casted_arrays
recursive_valid?(self, context, true)
end
# checking on casted objects
@ -133,29 +132,22 @@ module CouchRest
result
end
# Begin a recursive walk of the model checking validity
#
def all_valid?(context = :default)
recursive_valid?(self, context, true)
end
# Do recursive validity checking
#
def recursive_valid?(target, context, state)
valid = state
target.instance_variables.each do |ivar|
ivar_value = target.instance_variable_get(ivar)
if ivar_value.validatable?
valid = valid && recursive_valid?(ivar_value, context, valid)
elsif ivar_value.respond_to?(:each)
ivar_value.each do |item|
target.each do |key, prop|
if prop.is_a?(Array)
prop.each do |item|
if item.validatable?
valid = valid && recursive_valid?(item, context, valid)
valid = recursive_valid?(item, context, valid) && valid
end
end
elsif prop.validatable?
valid = recursive_valid?(prop, context, valid) && valid
end
end
return valid && target.valid?
target.class.validators.execute(context, target) && valid
end
@ -218,15 +210,6 @@ module CouchRest
end # end
EOS
end
all = "all_valid_for_#{context.to_s}?" # all_valid_for_signup?
if !self.instance_methods.include?(all)
class_eval <<-EOS, __FILE__, __LINE__
def #{all} # def all_valid_for_signup?
all_valid?('#{context.to_s}'.to_sym) # all_valid?('signup'.to_sym)
end # end
EOS
end
end
# Create a new validator of the given klazz and push it onto the