diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 1ba75be9..2c16e132 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -32,6 +32,7 @@ def setup! 'callback_models' => 'deleted_at DATETIME', 'after_commit_callback_models' => 'deleted_at DATETIME', 'fail_callback_models' => 'deleted_at DATETIME', + 'association_with_abort_models' => 'deleted_at DATETIME', 'related_models' => 'parent_model_id INTEGER, parent_model_with_counter_cache_column_id INTEGER, deleted_at DATETIME', 'asplode_models' => 'parent_model_id INTEGER, deleted_at DATETIME', 'employers' => 'name VARCHAR(32), deleted_at DATETIME', @@ -143,6 +144,35 @@ def test_destroy_behavior_for_plain_models_callbacks assert model.instance_variable_get(:@after_commit_callback_called) end + def test_destroy_behavior_for_association_with_abort + model = AssociationWithAbortModel.new + model.related_models.build + model.save + + assert_equal model.reload.related_models.count, 1 + + model = AssociationWithAbortModel.find(model.id) + return_value = model.destroy + + assert_equal return_value, false + assert_equal model.reload.related_models.count, 1 + end + + def test_destroy_bang_behavior_for_association_with_abort + model = AssociationWithAbortModel.new + model.related_models.build + model.save + + assert_equal model.reload.related_models.count, 1 + + model = AssociationWithAbortModel.find(model.id) + assert_raises ActiveRecord::RecordNotDestroyed do + model.destroy! + end + + assert_equal model.reload.related_models.count, 1 + end + def test_destroy_behavior_for_freshly_loaded_plain_models_callbacks model = CallbackModel.new model.save @@ -1211,6 +1241,18 @@ def remove_called_variables end end +class AssociationWithAbortModel < ActiveRecord::Base + acts_as_paranoid + has_many :related_models, class_name: 'RelatedModel', foreign_key: :parent_model_id, dependent: :destroy + before_destroy { |_| + if ActiveRecord::VERSION::MAJOR < 5 + false + else + throw :abort + end + } +end + class AfterCommitCallbackModel < ActiveRecord::Base acts_as_paranoid