Commit 2355f6d1 by Alex Koppel Committed by Ryan Bigg

Notify observers of paranoid restores

parent e04bba48
...@@ -89,6 +89,13 @@ class ActiveRecord::Base ...@@ -89,6 +89,13 @@ class ActiveRecord::Base
self.paranoia_column = options[:column] || :deleted_at self.paranoia_column = options[:column] || :deleted_at
default_scope { where(self.quoted_table_name + ".#{paranoia_column} IS NULL") } default_scope { where(self.quoted_table_name + ".#{paranoia_column} IS NULL") }
before_restore {
self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers)
}
after_restore {
self.class.notify_observers(:after_restore, self) if self.class.respond_to?(:notify_observers)
}
end end
def self.paranoid? ; false ; end def self.paranoid? ; false ; end
......
...@@ -305,6 +305,22 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -305,6 +305,22 @@ class ParanoiaTest < Test::Unit::TestCase
refute c.destroyed? refute c.destroyed?
end end
def test_observers_notified
a = ParanoidModelWithObservers.create
a.destroy
a.restore!
assert a.observers_notified.select {|args| args == [:before_restore, a]}
assert a.observers_notified.select {|args| args == [:after_restore, a]}
end
def test_observers_not_notified_if_not_supported
a = ParanoidModelWithObservers.create
a.destroy
a.restore!
# essentially, we're just ensuring that this doesn't crash
end
private private
def get_featureful_model def get_featureful_model
FeaturefulModel.new(:name => "not empty") FeaturefulModel.new(:name => "not empty")
...@@ -378,3 +394,17 @@ end ...@@ -378,3 +394,17 @@ end
class CustomColumnModel < ActiveRecord::Base class CustomColumnModel < ActiveRecord::Base
acts_as_paranoid column: :destroyed_at acts_as_paranoid column: :destroyed_at
end end
class ParanoidModelWithObservers < ParanoidModel
def observers_notified
@observers_notified ||= []
end
def self.notify_observer(*args)
observers_notified << args
end
end
class ParanoidModelWithoutObservers < ParanoidModel
self.class.send(remove_method :notify_observers) if method_defined?(:notify_observers)
end
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