Permalink
Browse files

Don't cascade autosave validation to destroyed children. [#2761 state…

…:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information...
cainlevy authored and alloy committed Jul 11, 2009
1 parent 52a50db commit 7c1e4ef64bd26927708da641584a6a13d9b1391c
@@ -246,6 +246,8 @@ def validate_collection_association(reflection)
# 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?
@@ -2567,6 +2567,7 @@ def save!
# options, use <tt>#destroy</tt>.
def delete
self.class.delete(id) unless new_record?
+ @destroyed = true
freeze
end
@@ -2581,6 +2582,7 @@ def destroy
)
end
+ @destroyed = true
freeze
end
@@ -2840,6 +2842,11 @@ def frozen?
@attributes.frozen?
end
+ # Returns +true+ if the record has been destroyed.
+ def destroyed?
+ @destroyed
+ end
+
# Returns +true+ if the record is read only. Records loaded through joins with piggy-back
# attributes will be marked as read only since they cannot be saved.
def readonly?
@@ -556,6 +556,17 @@ def save(*args)
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}") }
@@ -474,6 +474,7 @@ def test_delete
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
@@ -486,6 +487,7 @@ def test_destroy
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

0 comments on commit 7c1e4ef

Please sign in to comment.