From 48df95c32da347f0648d05264afff18fa7604fa8 Mon Sep 17 00:00:00 2001 From: Mike Ragalie Date: Thu, 2 Dec 2010 21:50:55 -0800 Subject: [PATCH] Added an unmark_for_destruction method to ActiveRecord::AutosaveAssociation --- .../lib/active_record/autosave_association.rb | 19 +++++++++++++++++-- .../test/cases/autosave_association_test.rb | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index c3dda29..a7f321d 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -4,7 +4,7 @@ module ActiveRecord # = Active Record Autosave Association # # +AutosaveAssociation+ is a module that takes care of automatically saving - # associacted records when their parent is saved. In addition to saving, it + # associated records when their parent is saved. In addition to saving, it # also destroys any associated records that were marked for destruction. # (See +mark_for_destruction+ and marked_for_destruction?). # @@ -60,6 +60,11 @@ module ActiveRecord # # Author.find_by_id(id).nil? # => true # + # If the model is mistakenly marked for destruction, it can be unmarked: + # + # post.author.unmark_for_destruction + # post.author.marked_for_destruction? # => false + # # === One-to-many Example # # When :autosave is not declared new children are saved when their parent is saved: @@ -110,6 +115,11 @@ module ActiveRecord # # Comment.find_by_id(id).nil? # => true # + # If the model is mistakenly marked for destruction, it can be unmarked: + # + # post.comments.last.unmark_for_destruction + # post.comments.last.marked_for_destruction? # => false + # # === Validation # # Children records are validated unless :validate is +false+. @@ -194,7 +204,7 @@ module ActiveRecord # Reloads the attributes of the object as usual and clears marked_for_destruction flag. def reload(options = nil) - @marked_for_destruction = false + unmark_for_destruction super end @@ -207,6 +217,11 @@ module ActiveRecord @marked_for_destruction = true end + # Clears marked_for_destruction flag. + def unmark_for_destruction + @marked_for_destruction = false + end + # Returns whether or not this record will be destroyed as part of the parents save transaction. # # Only useful if the :autosave option on the parent is enabled for this associated model. diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index fbf7121..49ce147 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -572,6 +572,14 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning') end + # unmark_for_destruction + def test_a_marked_for_destruction_record_should_not_be_marked_after_unmark + @pirate.ship.mark_for_destruction + @pirate.ship.unmark_for_destruction + + assert !@pirate.ship.marked_for_destruction? + end + # reload def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload @pirate.mark_for_destruction -- 1.7.1