Permalink
Browse files

Merge pull request #3854 from exviva/validates_associated_marked_for_…

…destruction

Do not validate associated records marked for destruction
  • Loading branch information...
2 parents 4ded0dd + a8134ac commit cf6ccf0ebd632ae5a8d4fc3b30ba47eff9837eef @josevalim josevalim committed Dec 4, 2011
@@ -2,8 +2,9 @@ module ActiveRecord
module Validations
class AssociatedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
- return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all?
- record.errors.add(attribute, :invalid, options.merge(:value => value))
+ if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
+ record.errors.add(attribute, :invalid, options.merge(:value => value))
+ end
end
end
@@ -61,6 +61,16 @@ def test_validates_associated_one
assert r.valid?
end
+ def test_validates_associated_marked_for_destruction
+ Topic.validates_associated(:replies)
+ Reply.validates_presence_of(:content)
+ t = Topic.new
+ t.replies << Reply.new
+ assert t.invalid?
+ t.replies.first.mark_for_destruction
+ assert t.valid?
+ end
+
def test_validates_associated_with_custom_message_using_quotes
Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes"
Topic.validates_presence_of :content

0 comments on commit cf6ccf0

Please sign in to comment.