Skip to content

Commit

Permalink
Add proper delete and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
efoxepstein committed Jun 13, 2011
1 parent 75181e0 commit e76e94e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Bundler::GemHelper.install_tasks

task :test do
Dir['test/*_test.rb'].each do |testfile|
load testfile
load testfile
end
end
12 changes: 9 additions & 3 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ def only_deleted
end

def destroy
_run_destroy_callbacks
_run_destroy_callbacks { delete }
end

def delete
self.update_attribute(:deleted_at, Time.now) if !deleted? && persisted?
freeze
end
alias :delete :destroy

def restore!
update_attribute :deleted_at, nil
end

def destroyed?
!self[:deleted_at].nil?
!self.deleted_at.nil?
end
alias :deleted? :destroyed?
end
Expand Down
53 changes: 45 additions & 8 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 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
def test_plain_model_class_is_not_paranoid
Expand All @@ -29,12 +30,12 @@ def test_paranoid_models_are_paranoid
assert_equal true, ParanoidModel.new.paranoid?
end

def test_delete_behavior_for_plain_models
def test_destroy_behavior_for_plain_models
model = PlainModel.new
assert_equal 0, model.class.count
model.save!
assert_equal 1, model.class.count
model.delete
model.destroy

assert_equal true, model.deleted_at.nil?
assert model.frozen?
Expand All @@ -43,12 +44,12 @@ def test_delete_behavior_for_plain_models
assert_equal 0, model.class.unscoped.count
end

def test_delete_behavior_for_paranoid_models
def test_destroy_behavior_for_paranoid_models
model = ParanoidModel.new
assert_equal 0, model.class.count
model.save!
assert_equal 1, model.class.count
model.delete
model.destroy

assert_equal false, model.deleted_at.nil?
assert model.frozen?
Expand All @@ -58,29 +59,60 @@ def test_delete_behavior_for_paranoid_models

end

def test_delete_behavior_for_featureful_paranoid_models
def test_destroy_behavior_for_featureful_paranoid_models
model = get_featureful_model
assert_equal 0, model.class.count
model.save!
assert_equal 1, model.class.count
model.delete
model.destroy

assert_equal false, model.deleted_at.nil?

assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
end

def test_only_deleted_scope_for_paranoid_models
def test_only_destroyed_scope_for_paranoid_models
model = ParanoidModel.new
model.save
model.delete
model.destroy
model2 = ParanoidModel.new
model2.save

assert_equal model, ParanoidModel.only_deleted.last
assert_equal false, ParanoidModel.only_deleted.include?(model2)
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
def get_featureful_model
Expand All @@ -101,3 +133,8 @@ class FeaturefulModel < ActiveRecord::Base

class PlainModel < ActiveRecord::Base
end

class CallbackModel < ActiveRecord::Base
acts_as_paranoid
before_destroy {|model| model.instance_variable_set :@callback_called, true }
end

0 comments on commit e76e94e

Please sign in to comment.