Skip to content
This repository
Browse code

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

…:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information...
commit a44a1257d879311d88c2d10c366ab0d6561f903a 1 parent bcd0ef7
Lance Ivy authored July 11, 2009 alloy committed September 12, 2009
2  activerecord/lib/active_record/autosave_association.rb
@@ -245,6 +245,8 @@ def validate_collection_association(reflection)
245 245
     # the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
246 246
     # enabled records if they're marked_for_destruction?.
247 247
     def association_valid?(reflection, association)
  248
+      return true if association.destroyed?
  249
+
248 250
       unless valid = association.valid?
249 251
         if reflection.options[:autosave]
250 252
           unless association.marked_for_destruction?
5  activerecord/lib/active_record/base.rb
@@ -2813,6 +2813,11 @@ def frozen?
2813 2813
         @attributes.frozen?
2814 2814
       end
2815 2815
 
  2816
+      # Returns +true+ if the record has been destroyed.
  2817
+      def destroyed?
  2818
+        @destroyed
  2819
+      end
  2820
+
2816 2821
       # Returns duplicated record with unfreezed attributes.
2817 2822
       def dup
2818 2823
         obj = super
11  activerecord/test/cases/autosave_association_test.rb
@@ -563,6 +563,17 @@ def save(*args)
563 563
       children.each { |child| child.mark_for_destruction }
564 564
       assert_difference("#{association_name.classify}.count", -2) { @pirate.save! }
565 565
     end
  566
+    
  567
+    define_method("test_should_skip_validation_on_the_#{association_name}_association_if_destroyed") do
  568
+      @pirate.send(association_name).create!(:name => "#{association_name}_1")
  569
+      children = @pirate.send(association_name)
  570
+
  571
+      children.each { |child| child.name = '' }
  572
+      assert !@pirate.valid?
  573
+
  574
+      children.each { |child| child.destroy }
  575
+      assert @pirate.valid?
  576
+    end
566 577
 
567 578
     define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
568 579
       2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
2  activerecord/test/cases/base_test.rb
@@ -529,6 +529,7 @@ def test_delete
529 529
     topic = Topic.find(1)
530 530
     assert_equal topic, topic.delete, 'topic.delete did not return self'
531 531
     assert topic.frozen?, 'topic not frozen after delete'
  532
+    assert topic.destroyed?, 'topic not marked as being destroyed'
532 533
     assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
533 534
   end
534 535
 
@@ -541,6 +542,7 @@ def test_destroy
541 542
     topic = Topic.find(1)
542 543
     assert_equal topic, topic.destroy, 'topic.destroy did not return self'
543 544
     assert topic.frozen?, 'topic not frozen after destroy'
  545
+    assert topic.destroyed?, 'topic not marked as being destroyed'
544 546
     assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
545 547
   end
546 548
 

0 notes on commit a44a125

Please sign in to comment.
Something went wrong with that request. Please try again.