Commit 4f268326 by Ryan Bigg

really_destroy! will now really destroy dependent = :destroy associated records

Fixes #117
parent e2ef9604
...@@ -90,12 +90,17 @@ module Paranoia ...@@ -90,12 +90,17 @@ module Paranoia
# insert time to paranoia column. # insert time to paranoia column.
# @param with_transaction [Boolean] exec with ActiveRecord Transactions. # @param with_transaction [Boolean] exec with ActiveRecord Transactions.
def touch_paranoia_column(with_transaction=false) def touch_paranoia_column(with_transaction=false)
# This method is (potentially) called from really_destroy
# The object the method is being called on may be frozen
# Let's not touch it if it's frozen.
unless self.frozen?
if with_transaction if with_transaction
with_transaction_returning_status { touch(paranoia_column) } with_transaction_returning_status { touch(paranoia_column) }
else else
touch(paranoia_column) touch(paranoia_column)
end end
end end
end
# restore associated records that have been soft deleted when # restore associated records that have been soft deleted when
# we called #destroy # we called #destroy
...@@ -122,9 +127,20 @@ end ...@@ -122,9 +127,20 @@ end
class ActiveRecord::Base class ActiveRecord::Base
def self.acts_as_paranoid(options={}) def self.acts_as_paranoid(options={})
alias :really_destroy! :destroy
alias :destroy! :destroy alias :destroy! :destroy
alias :delete! :delete alias :delete! :delete
def really_destroy!
dependent_reflections = self.reflections.select do |name, reflection|
reflection.options[:dependent] == :destroy
end
if dependent_reflections.any?
dependent_reflections.each do |name, _|
self.send(name).each(&:really_destroy!)
end
end
destroy!
end
include Paranoia include Paranoia
class_attribute :paranoia_column class_attribute :paranoia_column
......
...@@ -287,13 +287,20 @@ class ParanoiaTest < test_framework ...@@ -287,13 +287,20 @@ class ParanoiaTest < test_framework
assert model.instance_variable_get(:@restore_callback_called) assert model.instance_variable_get(:@restore_callback_called)
end end
def test_real_destroy def test_really_destroy
model = ParanoidModel.new model = ParanoidModel.new
model.save model.save
model.really_destroy! model.really_destroy!
refute ParanoidModel.unscoped.exists?(model.id) refute ParanoidModel.unscoped.exists?(model.id)
end end
def test_real_destroy_dependent_destroy
parent = ParentModel.create
child = parent.very_related_models.create
parent.really_destroy!
refute RelatedModel.unscoped.exists?(child.id)
end
if ActiveRecord::VERSION::STRING < "4.1" if ActiveRecord::VERSION::STRING < "4.1"
def test_real_destroy def test_real_destroy
model = ParanoidModel.new model = ParanoidModel.new
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment