mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #3854 from exviva/validates_associated_marked_for_destruction
Do not validate associated records marked for destruction
This commit is contained in:
commit
cf6ccf0ebd
2 changed files with 13 additions and 2 deletions
|
@ -2,8 +2,9 @@ module ActiveRecord
|
||||||
module Validations
|
module Validations
|
||||||
class AssociatedValidator < ActiveModel::EachValidator
|
class AssociatedValidator < ActiveModel::EachValidator
|
||||||
def validate_each(record, attribute, value)
|
def validate_each(record, attribute, value)
|
||||||
return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all?
|
if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
|
||||||
record.errors.add(attribute, :invalid, options.merge(:value => value))
|
record.errors.add(attribute, :invalid, options.merge(:value => value))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,16 @@ class AssociationValidationTest < ActiveRecord::TestCase
|
||||||
assert r.valid?
|
assert r.valid?
|
||||||
end
|
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
|
def test_validates_associated_with_custom_message_using_quotes
|
||||||
Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes"
|
Reply.validates_associated :topic, :message=> "This string contains 'single' and \"double\" quotes"
|
||||||
Topic.validates_presence_of :content
|
Topic.validates_presence_of :content
|
||||||
|
|
Loading…
Reference in a new issue