Don't cascade autosave validation to destroyed children. [#2761 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
This commit is contained in:
Lance Ivy 2009-07-11 15:09:09 +02:00 committed by Eloy Duran
parent bcd0ef710e
commit a44a1257d8
4 changed files with 20 additions and 0 deletions

View File

@ -245,6 +245,8 @@ module ActiveRecord
# the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
# enabled records if they're marked_for_destruction?.
def association_valid?(reflection, association)
return true if association.destroyed?
unless valid = association.valid?
if reflection.options[:autosave]
unless association.marked_for_destruction?

View File

@ -2813,6 +2813,11 @@ module ActiveRecord #:nodoc:
@attributes.frozen?
end
# Returns +true+ if the record has been destroyed.
def destroyed?
@destroyed
end
# Returns duplicated record with unfreezed attributes.
def dup
obj = super

View File

@ -563,6 +563,17 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
children.each { |child| child.mark_for_destruction }
assert_difference("#{association_name.classify}.count", -2) { @pirate.save! }
end
define_method("test_should_skip_validation_on_the_#{association_name}_association_if_destroyed") do
@pirate.send(association_name).create!(:name => "#{association_name}_1")
children = @pirate.send(association_name)
children.each { |child| child.name = '' }
assert !@pirate.valid?
children.each { |child| child.destroy }
assert @pirate.valid?
end
define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }

View File

@ -529,6 +529,7 @@ class BasicsTest < ActiveRecord::TestCase
topic = Topic.find(1)
assert_equal topic, topic.delete, 'topic.delete did not return self'
assert topic.frozen?, 'topic not frozen after delete'
assert topic.destroyed?, 'topic not marked as being destroyed'
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
end
@ -541,6 +542,7 @@ class BasicsTest < ActiveRecord::TestCase
topic = Topic.find(1)
assert_equal topic, topic.destroy, 'topic.destroy did not return self'
assert topic.frozen?, 'topic not frozen after destroy'
assert topic.destroyed?, 'topic not marked as being destroyed'
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
end