Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'danielsdeleo/errors_from_children'

  • Loading branch information...
commit 753e26faa323793009d9fbe730e541229b180d9e 2 parents ffe09d9 + b927900
@jnunemaker authored
Showing with 59 additions and 11 deletions.
  1. +13 −11 lib/validatable_class_methods.rb
  2. +46 −0 test/functional/test_validatable.rb
View
24 lib/validatable_class_methods.rb
@@ -4,18 +4,20 @@ module ClassMethods #:nodoc:
def validate_children(instance, group)
self.children_to_validate.each do |child_validation|
next unless child_validation.should_validate?(instance)
- child = instance.send child_validation.attribute
- if (child.respond_to?(:valid_for_group?))
- child.valid_for_group?(group)
- else
- child.valid?
- end
- child.errors.each do |attribute, messages|
- if messages.is_a?(String)
- add_error(instance, child_validation.map[attribute.to_sym] || attribute, messages)
+ child_or_children = instance.send child_validation.attribute
+ [child_or_children].flatten.each do |child|
+ if (child.respond_to?(:valid_for_group?))
+ child.valid_for_group?(group)
else
- messages.each do |message|
- add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
+ child.valid?
+ end
+ child.errors.each do |attribute, messages|
+ if messages.is_a?(String)
+ add_error(instance, child_validation.map[attribute.to_sym] || attribute, messages)
+ else
+ messages.each do |message|
+ add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
+ end
end
end
end
View
46 test/functional/test_validatable.rb
@@ -138,7 +138,53 @@
instance.valid?
instance.errors.on(:address)
end
+
+ expect "is invalid" do
+ child_class = Class.new do
+ include Validatable
+ attr_accessor :name, :address
+ validates_presence_of :name
+ validates_format_of :address, :with => /.+/
+ end
+ klass = Class.new do
+ include Validatable
+ include_errors_from :child
+ define_method :child do
+ valid_child = child_class.new
+ valid_child.name = "nom de plume"
+ valid_child.address = "nowhere"
+ [valid_child, child_class.new]
+ end
+ end
+ instance = klass.new
+ instance.valid?
+ instance.errors.on(:address)
+ end
+ expect true do
+ child_class = Class.new do
+ include Validatable
+ attr_accessor :name, :address
+ validates_presence_of :name
+ validates_format_of :address, :with => /.+/
+ end
+ klass = Class.new do
+ include Validatable
+ include_errors_from :child
+ define_method :child do
+ valid_child = child_class.new
+ valid_child.name = "nom de plume"
+ valid_child.address = "nowhere"
+ also_valid = child_class.new
+ also_valid.name = "nom de guerre"
+ also_valid.address = "somewhere else"
+ [valid_child, valid_child]
+ end
+ end
+ instance = klass.new
+ instance.valid?
+ end
+
expect "can't be empty" do
child_class = Class.new do
include Validatable
Please sign in to comment.
Something went wrong with that request. Please try again.