From bc515d19016524c46b484dd3abef4d463520e695 Mon Sep 17 00:00:00 2001 From: Hannes Fostie Date: Wed, 3 Jun 2015 16:07:40 +0200 Subject: [PATCH 1/2] Adds a way to prevent the default_scope when desired --- lib/paranoia.rb | 2 +- test/paranoia_test.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 1336d6eb..174b9e9b 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -201,7 +201,7 @@ def really_destroy! def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) end - default_scope { paranoia_scope } + default_scope { paranoia_scope } unless options[:default_scope] == false before_restore { self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 1a5e4267..480faffa 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -16,6 +16,8 @@ def setup! 'parent_model_with_counter_cache_columns' => 'related_models_count INTEGER DEFAULT 0', 'parent_models' => 'deleted_at DATETIME', 'paranoid_models' => 'parent_model_id INTEGER, deleted_at DATETIME', + 'default_scope_true_models' => 'deleted_at DATETIME', + 'default_scope_false_models' => 'deleted_at DATETIME', 'paranoid_model_with_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER', 'paranoid_model_with_build_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_and_build_id INTEGER, name VARCHAR(32)', 'paranoid_model_with_anthor_class_name_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER', @@ -244,6 +246,22 @@ def test_only_destroyed_scope_for_paranoid_models assert_equal false, ParanoidModel.only_deleted.include?(model2) end + def test_omittance_of_default_scope + model = DefaultScopeFalseModel.new + model.save + model.destroy + + assert_equal true, DefaultScopeFalseModel.all.include?(model) + end + + def test_default_scope + model = DefaultScopeTrueModel.new + model.save + model.destroy + + assert_equal false, DefaultScopeTrueModel.all.include?(model) + end + def test_default_scope_for_has_many_relationships parent = ParentModel.create assert_equal 0, parent.related_models.count @@ -918,6 +936,14 @@ class CustomSentinelModel < ActiveRecord::Base acts_as_paranoid sentinel_value: DateTime.new(0) end +class DefaultScopeTrueModel < ActiveRecord::Base + acts_as_paranoid default_scope: true +end + +class DefaultScopeFalseModel < ActiveRecord::Base + acts_as_paranoid default_scope: false +end + class NonParanoidModel < ActiveRecord::Base end From 386cacd86a9d911de4fb5af6274839110b03ab59 Mon Sep 17 00:00:00 2001 From: Hannes Fostie Date: Wed, 3 Jun 2015 16:42:34 +0200 Subject: [PATCH 2/2] WIP: Adds #without_deleted scope --- lib/paranoia.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 174b9e9b..437e67c0 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -33,6 +33,10 @@ def only_deleted end alias :deleted :only_deleted + def without_deleted + where(paranoia_column => nil) + end + def restore(id_or_ids, opts = {}) ids = Array(id_or_ids).flatten any_object_instead_of_id = ids.any? { |id| ActiveRecord::Base === id }