Commit e76e94e4 by Eli

Add proper delete and restore

parent 75181e0e
...@@ -14,14 +14,20 @@ module Paranoia ...@@ -14,14 +14,20 @@ module Paranoia
end end
def destroy def destroy
_run_destroy_callbacks _run_destroy_callbacks { delete }
end
def delete
self.update_attribute(:deleted_at, Time.now) if !deleted? && persisted? self.update_attribute(:deleted_at, Time.now) if !deleted? && persisted?
freeze freeze
end end
alias :delete :destroy
def restore!
update_attribute :deleted_at, nil
end
def destroyed? def destroyed?
!self[:deleted_at].nil? !self.deleted_at.nil?
end end
alias :deleted? :destroyed? alias :deleted? :destroyed?
end end
......
...@@ -11,6 +11,7 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_F ...@@ -11,6 +11,7 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_F
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))' ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))'
ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
class ParanoiaTest < Test::Unit::TestCase class ParanoiaTest < Test::Unit::TestCase
def test_plain_model_class_is_not_paranoid def test_plain_model_class_is_not_paranoid
...@@ -29,12 +30,12 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -29,12 +30,12 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal true, ParanoidModel.new.paranoid? assert_equal true, ParanoidModel.new.paranoid?
end end
def test_delete_behavior_for_plain_models def test_destroy_behavior_for_plain_models
model = PlainModel.new model = PlainModel.new
assert_equal 0, model.class.count assert_equal 0, model.class.count
model.save! model.save!
assert_equal 1, model.class.count assert_equal 1, model.class.count
model.delete model.destroy
assert_equal true, model.deleted_at.nil? assert_equal true, model.deleted_at.nil?
assert model.frozen? assert model.frozen?
...@@ -43,12 +44,12 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -43,12 +44,12 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal 0, model.class.unscoped.count assert_equal 0, model.class.unscoped.count
end end
def test_delete_behavior_for_paranoid_models def test_destroy_behavior_for_paranoid_models
model = ParanoidModel.new model = ParanoidModel.new
assert_equal 0, model.class.count assert_equal 0, model.class.count
model.save! model.save!
assert_equal 1, model.class.count assert_equal 1, model.class.count
model.delete model.destroy
assert_equal false, model.deleted_at.nil? assert_equal false, model.deleted_at.nil?
assert model.frozen? assert model.frozen?
...@@ -58,12 +59,12 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -58,12 +59,12 @@ class ParanoiaTest < Test::Unit::TestCase
end end
def test_delete_behavior_for_featureful_paranoid_models def test_destroy_behavior_for_featureful_paranoid_models
model = get_featureful_model model = get_featureful_model
assert_equal 0, model.class.count assert_equal 0, model.class.count
model.save! model.save!
assert_equal 1, model.class.count assert_equal 1, model.class.count
model.delete model.destroy
assert_equal false, model.deleted_at.nil? assert_equal false, model.deleted_at.nil?
...@@ -71,10 +72,10 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -71,10 +72,10 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal 1, model.class.unscoped.count assert_equal 1, model.class.unscoped.count
end end
def test_only_deleted_scope_for_paranoid_models def test_only_destroyed_scope_for_paranoid_models
model = ParanoidModel.new model = ParanoidModel.new
model.save model.save
model.delete model.destroy
model2 = ParanoidModel.new model2 = ParanoidModel.new
model2.save model2.save
...@@ -82,6 +83,37 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -82,6 +83,37 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal false, ParanoidModel.only_deleted.include?(model2) assert_equal false, ParanoidModel.only_deleted.include?(model2)
end end
def test_delete_behavior_for_callbacks
model = CallbackModel.new
model.save
model.delete
assert_equal nil, model.instance_variable_get(:@callback_called)
end
def test_destroy_behavior_for_callbacks
model = CallbackModel.new
model.save
model.destroy
assert model.instance_variable_get(:@callback_called)
end
def test_restore
model = ParanoidModel.new
model.save
id = model.id
model.destroy
puts model.inspect
puts ParanoidModel.all.inspect
assert model.destroyed?
model = ParanoidModel.only_deleted.find(id)
model.restore!
assert_equal false, model.destroyed?
end
private private
def get_featureful_model def get_featureful_model
FeaturefulModel.new(:name => "not empty") FeaturefulModel.new(:name => "not empty")
...@@ -101,3 +133,8 @@ end ...@@ -101,3 +133,8 @@ end
class PlainModel < ActiveRecord::Base class PlainModel < ActiveRecord::Base
end end
class CallbackModel < ActiveRecord::Base
acts_as_paranoid
before_destroy {|model| model.instance_variable_set :@callback_called, true }
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