Commit a5762de0 by Vincent Bonmalais Committed by Ryan Bigg

Fix destroy return value.

Rails normally returns the model on successful destroy and false if any
callback fails.

Fixes #104
parent 74c0e8bf
......@@ -48,7 +48,8 @@ module Paranoia
end
def destroy
run_callbacks(:destroy) { touch_paranoia_column(true) }
callbacks_result = run_callbacks(:destroy) { touch_paranoia_column(true) }
callbacks_result ? self : false
end
# As of Rails 4.1.0 +destroy!+ will no longer remove the record from the db
......
......@@ -15,6 +15,7 @@ ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER
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)'
ActiveRecord::Base.connection.execute 'CREATE TABLE fail_callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE related_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER NOT NULL, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE employers (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
......@@ -256,6 +257,20 @@ class ParanoiaTest < test_framework
assert_equal 1, ParanoidModel.unscoped.where(id: model.id).count
end
def test_destroy_return_value_on_success
model = ParanoidModel.create
return_value = model.destroy
assert_equal(return_value, model)
end
def test_destroy_return_value_on_failure
model = FailCallbackModel.create
return_value = model.destroy
assert_equal(return_value, false)
end
def test_restore_behavior_for_callbacks
model = CallbackModel.new
model.save
......@@ -388,6 +403,13 @@ class ParanoidModel < ActiveRecord::Base
acts_as_paranoid
end
class FailCallbackModel < ActiveRecord::Base
belongs_to :parent_model
acts_as_paranoid
before_destroy { |_| false }
end
class FeaturefulModel < ActiveRecord::Base
acts_as_paranoid
validates :name, :presence => true, :uniqueness => true
......
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