Commit 5058797e by Nikolay Norkin

real_destroy callback

parent df42daa3
...@@ -47,7 +47,7 @@ module Paranoia ...@@ -47,7 +47,7 @@ module Paranoia
module Callbacks module Callbacks
def self.extended(klazz) def self.extended(klazz)
klazz.define_callbacks :restore klazz.define_callbacks :restore, :real_destroy
klazz.define_singleton_method("before_restore") do |*args, &block| klazz.define_singleton_method("before_restore") do |*args, &block|
set_callback(:restore, :before, *args, &block) set_callback(:restore, :before, *args, &block)
...@@ -172,25 +172,29 @@ class ActiveRecord::Base ...@@ -172,25 +172,29 @@ class ActiveRecord::Base
alias :destroy_without_paranoia :destroy alias :destroy_without_paranoia :destroy
def really_destroy! def really_destroy!
dependent_reflections = self.class.reflections.select do |name, reflection| transaction do
reflection.options[:dependent] == :destroy run_callbacks(:real_destroy) do
end dependent_reflections = self.class.reflections.select do |name, reflection|
if dependent_reflections.any? reflection.options[:dependent] == :destroy
dependent_reflections.each do |name, reflection| end
association_data = self.send(name) if dependent_reflections.any?
# has_one association can return nil dependent_reflections.each do |name, reflection|
# .paranoid? will work for both instances and classes association_data = self.send(name)
if association_data && association_data.paranoid? # has_one association can return nil
if reflection.collection? # .paranoid? will work for both instances and classes
association_data.with_deleted.each(&:really_destroy!) if association_data && association_data.paranoid?
else if reflection.collection?
association_data.really_destroy! association_data.with_deleted.each(&:really_destroy!)
else
association_data.really_destroy!
end
end
end end
end end
write_attribute(paranoia_column, current_time_from_proper_timezone)
destroy_without_paranoia
end end
end end
write_attribute(paranoia_column, current_time_from_proper_timezone)
destroy_without_paranoia
end end
include Paranoia include Paranoia
......
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