Commit e04bba48 by alfa-jpn Committed by Ryan Bigg

add tests for plain models callback

Fixes #81
Fixes #84

Conflicts:
	lib/paranoia.rb
parent 8572265e
......@@ -44,12 +44,12 @@ module Paranoia
end
def destroy
run_callbacks(:destroy) { delete }
run_callbacks(:destroy) { delete_or_soft_delete(true) }
end
def delete
return if new_record?
destroyed? ? destroy! : update_column(paranoia_column, Time.now)
delete_or_soft_delete
end
def restore!
......@@ -60,6 +60,24 @@ module Paranoia
!!send(paranoia_column)
end
alias :deleted? :destroyed?
private
# select and exec delete or soft-delete.
# @param with_transaction [Boolean] exec with ActiveRecord Transactions, when soft-delete.
def delete_or_soft_delete(with_transaction=false)
destroyed? ? destroy! : touch_paranoia_column(with_transaction)
end
# touch paranoia column.
# insert time to paranoia column.
# @param with_transaction [Boolean] exec with ActiveRecord Transactions.
def touch_paranoia_column(with_transaction=false)
if with_transaction
with_transaction_returning_status { touch(paranoia_column) }
else
touch(paranoia_column)
end
end
end
class ActiveRecord::Base
......
......@@ -63,9 +63,32 @@ class ParanoiaTest < Test::Unit::TestCase
# Anti-regression test for #81, which would've introduced a bug to break this test.
def test_destroy_behavior_for_plain_models_callbacks
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
model.destroy
assert_equal nil, model.instance_variable_get(:@update_callback_called)
assert_equal nil, model.instance_variable_get(:@save_callback_called)
assert_equal nil, model.instance_variable_get(:@validate_called)
assert model.instance_variable_get(:@destroy_callback_called)
assert model.instance_variable_get(:@after_destroy_callback_called)
assert model.instance_variable_get(:@after_commit_callback_called)
end
def test_delete_behavior_for_plain_models_callbacks
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
model.delete
assert_equal nil, model.instance_variable_get(:@update_callback_called)
assert_equal nil, model.instance_variable_get(:@save_callback_called)
assert_equal nil, model.instance_variable_get(:@validate_called)
assert_equal nil, model.instance_variable_get(:@destroy_callback_called)
assert_equal nil, model.instance_variable_get(:@after_destroy_callback_called)
assert_equal nil, model.instance_variable_get(:@after_commit_callback_called)
end
def test_destroy_behavior_for_paranoid_models
......@@ -313,6 +336,15 @@ class CallbackModel < ActiveRecord::Base
before_restore {|model| model.instance_variable_set :@restore_callback_called, true }
before_update {|model| model.instance_variable_set :@update_callback_called, true }
before_save {|model| model.instance_variable_set :@save_callback_called, true}
after_destroy {|model| model.instance_variable_set :@after_destroy_callback_called, true }
after_commit {|model| model.instance_variable_set :@after_commit_callback_called, true }
validate {|model| model.instance_variable_set :@validate_called, true }
def remove_called_variables
instance_variables.each {|name| (name.to_s.end_with?('_called')) ? remove_instance_variable(name) : nil}
end
end
class ParentModel < ActiveRecord::Base
......
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