From 3af4d7461727ce34d6219d8699a278c02e3341ca Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 09:35:39 -0700 Subject: [PATCH 01/12] Add: add-default-recovery-window-option-to-acts-as-paranoid --- lib/paranoia.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index a2298953..4c9fc5e6 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -2,6 +2,7 @@ module Paranoia @@default_sentinel_value = nil + @@default_recovery_window = nil # Change default_sentinel_value in a rails initializer def self.default_sentinel_value=(val) @@ -12,6 +13,14 @@ def self.default_sentinel_value @@default_sentinel_value end + def self.default_recovery_window=(val) + @@default_recovery_window + end + + def self.default_recovery_window + @@default_recovery_window + end + def self.included(klazz) klazz.extend Query klazz.extend Callbacks @@ -132,6 +141,7 @@ def restore!(opts = {}) def get_recovery_window_range(opts) return opts[:recovery_window_range] if opts[:recovery_window_range] + opts[:recovery_window] = default_recovery_window if opts[:recovery_window].blank? && default_recovery_window.present? return unless opts[:recovery_window] (deleted_at - opts[:recovery_window]..deleted_at + opts[:recovery_window]) end @@ -250,6 +260,8 @@ def self.acts_as_paranoid(options={}) self.paranoia_column = (options[:column] || :deleted_at).to_s self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value } + Paranoia.default_recovery_window = options.fetch(:recovery_window) { Paranoia.default_recovery_window } + def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) end From 5736b020ac261e71a142aae5266e4ef9c35b2418 Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 09:46:23 -0700 Subject: [PATCH 02/12] Overwrite internal default_recovery_window and use paranoia_default_recovery_window instead --- lib/paranoia.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 4c9fc5e6..c6a9c2ea 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -141,7 +141,7 @@ def restore!(opts = {}) def get_recovery_window_range(opts) return opts[:recovery_window_range] if opts[:recovery_window_range] - opts[:recovery_window] = default_recovery_window if opts[:recovery_window].blank? && default_recovery_window.present? + opts[:recovery_window] = paranoia_default_recovery_window if opts[:recovery_window].blank? && paranoia_default_recovery_window.present? return unless opts[:recovery_window] (deleted_at - opts[:recovery_window]..deleted_at + opts[:recovery_window]) end @@ -256,11 +256,11 @@ def self.acts_as_paranoid(options={}) alias_method :destroy_without_paranoia, :destroy include Paranoia - class_attribute :paranoia_column, :paranoia_sentinel_value + class_attribute :paranoia_column, :paranoia_sentinel_value, :paranoia_default_recovery_window self.paranoia_column = (options[:column] || :deleted_at).to_s self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value } - Paranoia.default_recovery_window = options.fetch(:recovery_window) { Paranoia.default_recovery_window } + self.paranoia_default_recovery_window = options.fetch(:default_recovery_window) { Paranoia.default_recovery_window } def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) @@ -302,6 +302,10 @@ def paranoia_column def paranoia_sentinel_value self.class.paranoia_sentinel_value end + + def paranoia_default_recovery_window + self.class.paranoia_default_recovery_window + end end end From 23e9e1a9c37ac72fb12de68d3c9f2d742749ad38 Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 09:50:39 -0700 Subject: [PATCH 03/12] Add test_default_recovery_window to set default --- test/paranoia_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 8c4c2d3f..1d180cbf 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -228,6 +228,14 @@ def test_destroy_behavior_for_custom_column_models def test_default_sentinel_value assert_nil ParanoidModel.paranoia_sentinel_value + assert_equal 5, ParanoidModel.paranoia_sentinel_value = 5 + assert_nil ParanoidModel.paranoia_sentinel_value = nil + end + + def default_recovery_window + assert_nil ParanoidModel.default_recovery_window + assert_equal 120, ParanoidModel.default_recovery_window = 120 + assert_nil ParanoidModel.default_recovery_window = nil end def test_without_default_scope_option From e120e684cbe989bb4f6f3f8d47054a04fcef1fd3 Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 09:51:13 -0700 Subject: [PATCH 04/12] test_ --- test/paranoia_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 1d180cbf..d394530d 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -232,7 +232,7 @@ def test_default_sentinel_value assert_nil ParanoidModel.paranoia_sentinel_value = nil end - def default_recovery_window + def test_default_recovery_window assert_nil ParanoidModel.default_recovery_window assert_equal 120, ParanoidModel.default_recovery_window = 120 assert_nil ParanoidModel.default_recovery_window = nil From da2dcf2bbf98cbd99da906e9f4958e1783c0b98d Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 09:59:18 -0700 Subject: [PATCH 05/12] Drop the default --- lib/paranoia.rb | 10 +++++----- test/paranoia_test.rb | 8 -------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index c6a9c2ea..3ec5b92f 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -141,7 +141,7 @@ def restore!(opts = {}) def get_recovery_window_range(opts) return opts[:recovery_window_range] if opts[:recovery_window_range] - opts[:recovery_window] = paranoia_default_recovery_window if opts[:recovery_window].blank? && paranoia_default_recovery_window.present? + opts[:recovery_window] = paranoia_recovery_window if opts[:recovery_window].blank? && paranoia_recovery_window.present? return unless opts[:recovery_window] (deleted_at - opts[:recovery_window]..deleted_at + opts[:recovery_window]) end @@ -256,11 +256,11 @@ def self.acts_as_paranoid(options={}) alias_method :destroy_without_paranoia, :destroy include Paranoia - class_attribute :paranoia_column, :paranoia_sentinel_value, :paranoia_default_recovery_window + class_attribute :paranoia_column, :paranoia_sentinel_value, :paranoia_recovery_window self.paranoia_column = (options[:column] || :deleted_at).to_s self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value } - self.paranoia_default_recovery_window = options.fetch(:default_recovery_window) { Paranoia.default_recovery_window } + self.paranoia_recovery_window = options.fetch(:recovery_window) { Paranoia.default_recovery_window } def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) @@ -303,8 +303,8 @@ def paranoia_sentinel_value self.class.paranoia_sentinel_value end - def paranoia_default_recovery_window - self.class.paranoia_default_recovery_window + def paranoia_recovery_window + self.class.paranoia_recovery_window end end end diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index d394530d..8c4c2d3f 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -228,14 +228,6 @@ def test_destroy_behavior_for_custom_column_models def test_default_sentinel_value assert_nil ParanoidModel.paranoia_sentinel_value - assert_equal 5, ParanoidModel.paranoia_sentinel_value = 5 - assert_nil ParanoidModel.paranoia_sentinel_value = nil - end - - def test_default_recovery_window - assert_nil ParanoidModel.default_recovery_window - assert_equal 120, ParanoidModel.default_recovery_window = 120 - assert_nil ParanoidModel.default_recovery_window = nil end def test_without_default_scope_option From acee95ba351da867f75f06af4555922a53bc52dc Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 10:05:56 -0700 Subject: [PATCH 06/12] Specs for paranoia_recovery_window --- 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 3ec5b92f..954a49ef 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -14,7 +14,7 @@ def self.default_sentinel_value end def self.default_recovery_window=(val) - @@default_recovery_window + @@default_recovery_window = val end def self.default_recovery_window diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 8c4c2d3f..16a1f7b2 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -230,6 +230,32 @@ def test_default_sentinel_value assert_nil ParanoidModel.paranoia_sentinel_value end + def test_default_sentinel_value_setter + ParanoidModel.paranoia_sentinel_value = 5 + assert_equal 5, ParanoidModel.paranoia_sentinel_value + ParanoidModel.paranoia_sentinel_value = nil + end + + def test_default_sentinel_value + assert_nil ParanoidModel.paranoia_sentinel_value + end + + def test_default_recovery_window_setter + ParanoidModel.paranoia_recovery_window = 5 + assert_equal 5, ParanoidModel.paranoia_recovery_window + ParanoidModel.paranoia_recovery_window = nil + end + + def test_default_recovery_window_value + assert_nil ParanoidModel.paranoia_recovery_window + end + + # def test_default_sentinel_value_setter + # ParanoidModel.paranoia_reovery = 5 + # assert_equal 5, ParanoidModel.paranoia_sentinel_value + # ParanoidModel.paranoia_sentinel_value = nil + # end + def test_without_default_scope_option model = WithoutDefaultScopeModel.create model.destroy From 64b47db4e7089677d9a59fc69e37f66d3f061693 Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 10:30:05 -0700 Subject: [PATCH 07/12] Add spec --- test/paranoia_test.rb | 54 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 16a1f7b2..e04df7b4 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -14,6 +14,7 @@ def setup! { 'parent_model_with_counter_cache_columns' => 'related_models_count INTEGER DEFAULT 0', 'parent_models' => 'deleted_at DATETIME', + 'parent_model_with_recovery_windows' => 'deleted_at DATETIME', 'paranoid_models' => 'parent_model_id INTEGER, 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)', @@ -250,12 +251,6 @@ def test_default_recovery_window_value assert_nil ParanoidModel.paranoia_recovery_window end - # def test_default_sentinel_value_setter - # ParanoidModel.paranoia_reovery = 5 - # assert_equal 5, ParanoidModel.paranoia_sentinel_value - # ParanoidModel.paranoia_sentinel_value = nil - # end - def test_without_default_scope_option model = WithoutDefaultScopeModel.create model.destroy @@ -639,6 +634,38 @@ def test_restore_with_associations_using_recovery_window assert_equal true, second_child.reload.deleted_at.nil? end + def test_restore_with_associations_using_default_recovery_window + parent = ParentModelWithRecoveryWindow.create + first_child = parent.very_related_models.create + second_child = parent.very_related_models.create + + parent.destroy + second_child.update(deleted_at: parent.deleted_at + 11.minutes) + + parent.restore!(:recursive => true) + assert_equal true, parent.deleted_at.nil? + assert_equal true, first_child.reload.deleted_at.nil? + assert_equal true, second_child.reload.deleted_at.nil? + + parent.destroy + second_child.update(deleted_at: parent.deleted_at + 11.minutes) + + parent.restore(:recursive => true) + assert_equal true, parent.deleted_at.nil? + assert_equal true, first_child.reload.deleted_at.nil? + assert_equal false, second_child.reload.deleted_at.nil? + + second_child.restore + parent.destroy + first_child.update(deleted_at: parent.deleted_at - 11.minutes) + second_child.update(deleted_at: parent.deleted_at - 9.minutes) + + ParentModel.restore(parent.id, :recursive => true) + assert_equal true, parent.reload.deleted_at.nil? + assert_equal false, first_child.reload.deleted_at.nil? + assert_equal true, second_child.reload.deleted_at.nil? + end + def test_restore_with_associations parent = ParentModel.create first_child = parent.very_related_models.create @@ -1076,7 +1103,6 @@ def get_featureful_model end # Helper classes - class ParanoidModel < ActiveRecord::Base belongs_to :parent_model acts_as_paranoid @@ -1146,6 +1172,20 @@ class ParentModel < ActiveRecord::Base has_one :polymorphic_model, as: :parent, dependent: :destroy end +class ParentModelWithRecoveryWindow < ActiveRecord::Base + acts_as_paranoid(recovery_window: 10.minutes) + + # parent_model_with_recovery_windows + + has_many :paranoid_models, foreign_key: 'parent_model_id' + has_many :related_models, foreign_key: 'parent_model_id' + has_many :very_related_models, class_name: 'RelatedModel', foreign_key: 'parent_model_id', dependent: :destroy + has_many :non_paranoid_models, foreign_key: 'parent_model_id', dependent: :destroy + has_one :non_paranoid_model, foreign_key: 'parent_model_id', dependent: :destroy + has_many :asplode_models, foreign_key: 'parent_model_id', dependent: :destroy + has_one :polymorphic_model, as: :parent, dependent: :destroy +end + class ParentModelWithCounterCacheColumn < ActiveRecord::Base has_many :related_models end From 14780a0881d212d7f6e12d5702f86cd7d9eb62a5 Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Wed, 23 Aug 2017 10:37:59 -0700 Subject: [PATCH 08/12] eh --- test/paranoia_test.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index e04df7b4..d8b93f49 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -1175,8 +1175,6 @@ class ParentModel < ActiveRecord::Base class ParentModelWithRecoveryWindow < ActiveRecord::Base acts_as_paranoid(recovery_window: 10.minutes) - # parent_model_with_recovery_windows - has_many :paranoid_models, foreign_key: 'parent_model_id' has_many :related_models, foreign_key: 'parent_model_id' has_many :very_related_models, class_name: 'RelatedModel', foreign_key: 'parent_model_id', dependent: :destroy From cf512ba6032f2b17d9ec1506c3ac084046ce033f Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Thu, 31 Aug 2017 15:04:48 -0700 Subject: [PATCH 09/12] Fix ParentModelWithRecoveryWindow spec --- test/paranoia_test.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index d8b93f49..60a008da 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -14,7 +14,6 @@ def setup! { 'parent_model_with_counter_cache_columns' => 'related_models_count INTEGER DEFAULT 0', 'parent_models' => 'deleted_at DATETIME', - 'parent_model_with_recovery_windows' => 'deleted_at DATETIME', 'paranoid_models' => 'parent_model_id INTEGER, 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)', @@ -642,7 +641,7 @@ def test_restore_with_associations_using_default_recovery_window parent.destroy second_child.update(deleted_at: parent.deleted_at + 11.minutes) - parent.restore!(:recursive => true) + parent.restore!(:recursive => true, recovery_window: 12.minutes) assert_equal true, parent.deleted_at.nil? assert_equal true, first_child.reload.deleted_at.nil? assert_equal true, second_child.reload.deleted_at.nil? @@ -660,7 +659,7 @@ def test_restore_with_associations_using_default_recovery_window first_child.update(deleted_at: parent.deleted_at - 11.minutes) second_child.update(deleted_at: parent.deleted_at - 9.minutes) - ParentModel.restore(parent.id, :recursive => true) + ParentModelWithRecoveryWindow.restore(parent.id, :recursive => true) assert_equal true, parent.reload.deleted_at.nil? assert_equal false, first_child.reload.deleted_at.nil? assert_equal true, second_child.reload.deleted_at.nil? @@ -1173,8 +1172,8 @@ class ParentModel < ActiveRecord::Base end class ParentModelWithRecoveryWindow < ActiveRecord::Base - acts_as_paranoid(recovery_window: 10.minutes) - + self.table_name = 'parent_models' + acts_as_paranoid recovery_window: 10.minutes has_many :paranoid_models, foreign_key: 'parent_model_id' has_many :related_models, foreign_key: 'parent_model_id' has_many :very_related_models, class_name: 'RelatedModel', foreign_key: 'parent_model_id', dependent: :destroy From de32e536c96d3ffdf132b0568e69f8c827a46773 Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Tue, 26 Sep 2017 14:15:54 -0700 Subject: [PATCH 10/12] Binding .pry --- lib/paranoia.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 954a49ef..7de9f8b4 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -261,7 +261,8 @@ def self.acts_as_paranoid(options={}) self.paranoia_column = (options[:column] || :deleted_at).to_s self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value } self.paranoia_recovery_window = options.fetch(:recovery_window) { Paranoia.default_recovery_window } - + binding.pry + def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) end From 388587b820b47f2901c6217d9bf32f7c1fb6ad37 Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Tue, 26 Sep 2017 14:26:51 -0700 Subject: [PATCH 11/12] Options any --- lib/paranoia.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 7de9f8b4..ad705b0d 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -261,7 +261,8 @@ def self.acts_as_paranoid(options={}) self.paranoia_column = (options[:column] || :deleted_at).to_s self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value } self.paranoia_recovery_window = options.fetch(:recovery_window) { Paranoia.default_recovery_window } - binding.pry + + binding.pry if options.any? def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) From 811218c45f3ceb38cc64908350500b735a7d4ceb Mon Sep 17 00:00:00 2001 From: Nathan Bertram Date: Tue, 26 Sep 2017 15:09:15 -0700 Subject: [PATCH 12/12] Remove pry debug code --- lib/paranoia.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index ad705b0d..954a49ef 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -262,8 +262,6 @@ def self.acts_as_paranoid(options={}) self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value } self.paranoia_recovery_window = options.fetch(:recovery_window) { Paranoia.default_recovery_window } - binding.pry if options.any? - def self.paranoia_scope where(paranoia_column => paranoia_sentinel_value) end