Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replace any existing deleted entry when deleting a model.

This should rarely occur. It can happen if test fixtures get reloaded into a
database after something has been deleted because reloading the fixtures doesn't
clear the deleted tables.
  • Loading branch information...
commit 23c63038b23ab0dedc019183e8172b66fd9f20ce 1 parent bcf7bdb
@ajh authored
View
4 lib/acts_as_soft_deletable.rb
@@ -144,7 +144,7 @@ def deleted_class
def find_with_deleted(*args)
if args.last.is_a?(Hash)
[:order, :limit, :offset].each do |option|
- raise ArgumentError.new "#{option} option is not supported" if args.last.key?(option)
+ raise ArgumentError.new("#{option} option is not supported") if args.last.key?(option)
end
end
@@ -198,6 +198,8 @@ def self.included(base)
# exception.
def destroy_with_soft_delete
self.class.transaction do
+ self.class.deleted_class.delete self.id
+
deleted = self.class.deleted_class.new
self.attributes.keys.each do |key|
deleted.send("#{key}=", self.send(key))
View
7 test/fixtures/decimals.yml
@@ -1,4 +1,11 @@
one:
+ id: 1
+ rational_number: <%= 2.0/3.0 %>
+ irrational_number: <%= Math::PI %>
+ short_real_number: <%= Math::PI %>
+
+already_deleted:
+ id: 38383
rational_number: <%= 2.0/3.0 %>
irrational_number: <%= Math::PI %>
short_real_number: <%= Math::PI %>
View
5 test/fixtures/deleted_decimals.yml
@@ -0,0 +1,5 @@
+already_deleted:
+ id: 38383
+ rational_number: <%= 2.0/3.0 %>
+ irrational_number: <%= Math::PI %>
+ short_real_number: <%= Math::PI %>
View
10 test/test_acts_as_soft_deletable.rb
@@ -22,8 +22,7 @@ def test_deleted_model_should_be_able_to_undestroy
end
def test_should_copy_decimals_correctly
- assert_equal [1, 0], [Decimal.count, Decimal::Deleted.count]
- decimal = Decimal.find :first
+ decimal = Decimal.find 1
assert_difference("Decimal.count", -1) do
assert_difference("Decimal::Deleted.count") do
@@ -45,4 +44,11 @@ def test_should_copy_decimals_correctly
def test_helper_should_work
assert_model_soft_deletes Artist.find_by_name('Chick Corea')
end
+
+ def test_should_replace_any_existing_deleted_entry
+ assert (d = Decimal.find(38383))
+ assert Decimal::Deleted.find(38383)
+
+ assert_nothing_raised { d.destroy }
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.