From 43ad8c36cb4d97a49a5569bf66d6535de1f6a52e Mon Sep 17 00:00:00 2001 From: Pascal Lindelauf Date: Wed, 12 Feb 2014 08:28:09 +0100 Subject: [PATCH 1/2] Added test for failing destroy on parent model destroying very related models anyway --- test/paranoia_test.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 778f0f23..5e9fb0a5 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -8,7 +8,7 @@ FileUtils.rm_f DB_FILE ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE -ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' +ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, type VARCHAR(50), deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, 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)' @@ -78,6 +78,15 @@ def test_destroy_behavior_for_plain_models_callbacks assert model.instance_variable_get(:@after_commit_callback_called) end + def test_failing_destroy_on_parent_model_does_not_destroy_very_related_model + model = FailDestroyParentModel.new + very_related_model = RelatedModel.new + model.very_related_models << very_related_model + model.save + model.destroy + assert !model.destroyed? + assert !very_related_model.destroyed? + end def test_delete_behavior_for_plain_models_callbacks model = CallbackModel.new @@ -427,6 +436,11 @@ class RelatedModel < ActiveRecord::Base belongs_to :parent_model end +class FailDestroyParentModel < ParentModel + acts_as_paranoid + before_destroy { |_| false } +end + class Employer < ActiveRecord::Base acts_as_paranoid has_many :jobs From 34448a6edcab285d015ce2a4054ffdef5ce33dea Mon Sep 17 00:00:00 2001 From: Pascal Lindelauf Date: Wed, 12 Feb 2014 15:17:13 +0100 Subject: [PATCH 2/2] Added test for acts_as_paranoia being applied to both models in a query containing a join --- test/paranoia_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 5e9fb0a5..58050ebc 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -87,6 +87,19 @@ def test_failing_destroy_on_parent_model_does_not_destroy_very_related_model assert !model.destroyed? assert !very_related_model.destroyed? end + + def test_join_applies_paranoid_behavior_to_both_models + ParentModel.unscoped.delete_all + assert_equal 0, ParentModel.count + model = ParentModel.create + model.related_models.create + model.related_models.create + first_related_model_id = model.related_models.first.id + + assert_equal 1, ParentModel.joins(:related_models).where(related_models: {id: first_related_model_id}).count + model.related_models.first.destroy + assert_equal 0, ParentModel.joins(:related_models).where(related_models: {id: first_related_model_id}).count + end def test_delete_behavior_for_plain_models_callbacks model = CallbackModel.new