From 17acf4501e954c072cecfa9ae6f98cea4cbcf0bb Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Fri, 29 Jan 2016 15:57:11 -0800 Subject: [PATCH 1/4] added without_default_scope option --- lib/paranoia.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 3833f5d9..2ea67bac 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -219,7 +219,10 @@ def self.acts_as_paranoid(options={}) def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) end - default_scope { paranoia_scope } + + unless options[:without_default_scope] + default_scope { paranoia_scope } + end before_restore { self.class.notify_observers(:before_restore, self) if self.class.respond_to?(:notify_observers) From 3d65a93475318dc8b80d64f0b1428ee850e0afe3 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Fri, 29 Jan 2016 15:59:40 -0800 Subject: [PATCH 2/4] added without_default_scope option --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 3f4f6b66..98ff5f42 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,17 @@ class Client < ActiveRecord::Base end ``` + +If you want to skip adding the default scope + +``` ruby +class Client < ActiveRecord::Base + acts_as_paranoid without_default_scope: true + + ... +end +``` + If you want to access soft-deleted associations, override the getter method: ``` ruby From 90471cdbbdd86abbf8a24a3d0332f309a5ead61c Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Sun, 31 Jan 2016 21:06:17 -0800 Subject: [PATCH 3/4] added_with_default_scope_option --- test/paranoia_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 6bcac7fe..2dbd552a 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -204,6 +204,13 @@ def test_default_sentinel_value assert_equal nil, ParanoidModel.paranoia_sentinel_value end + def test_without_default_scope_option + model = WithoutDefaultScopeModel.create!(name: "A", deleted_at: Time.now) + assert_equal 1, model.class.count + assert_equal 1, model.class.only_deleted.count + assert_equal 0, model.class.without_deleted.count + end + def test_active_column_model model = ActiveColumnModel.new assert_equal 0, model.class.count @@ -1042,6 +1049,10 @@ class CustomSentinelModel < ActiveRecord::Base acts_as_paranoid sentinel_value: DateTime.new(0) end +class WithoutDefaultScopeModel < ActiveRecord::Base + acts_as_paranoid without_default_scope: true +end + class ActiveColumnModel < ActiveRecord::Base acts_as_paranoid column: :active, sentinel_value: true From 6d90b426d3ade369b65670ec4b24b41350523b7e Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Mon, 1 Feb 2016 08:32:33 -0800 Subject: [PATCH 4/4] added without_default_scope option --- test/paranoia_test.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 2dbd552a..4995e7c9 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -40,7 +40,8 @@ def setup! 'namespaced_paranoid_belongs_tos' => 'deleted_at DATETIME, paranoid_has_one_id INTEGER', 'unparanoid_unique_models' => 'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER', 'active_column_models' => 'deleted_at DATETIME, active BOOLEAN', - 'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN' + 'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN', + 'without_default_scope_models' => 'deleted_at DATETIME' }.each do |table_name, columns_as_sql_string| ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})" end @@ -205,10 +206,11 @@ def test_default_sentinel_value end def test_without_default_scope_option - model = WithoutDefaultScopeModel.create!(name: "A", deleted_at: Time.now) + model = WithoutDefaultScopeModel.create + model.destroy assert_equal 1, model.class.count assert_equal 1, model.class.only_deleted.count - assert_equal 0, model.class.without_deleted.count + assert_equal 0, model.class.where(deleted_at: nil).count end def test_active_column_model