From 0e54d08892487a671597555baa282ed80e5369a4 Mon Sep 17 00:00:00 2001 From: Jay Schneider Date: Fri, 21 May 2021 14:28:25 +0200 Subject: [PATCH 1/2] feat: add configuration option to set default options for acts_as_paranoid globally --- README.md | 18 +++++++++++++++++- lib/acts_as_paranoid.rb | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 65ebb8f2..d2371d10 100755 --- a/README.md +++ b/README.md @@ -80,6 +80,22 @@ option which is `true` by default. When set to `false`, entities that have have `true` will be considered deleted. When `true` everything that has a not-null value will be considered deleted. +### Configure Default Options + +If you want to use a configuration option globally in your application +as a default, you can set up the following in your application.rb or in +an initializer: +```ruby + # config/initializers/acts_as_paranoid.rb: + Rails.application.config.acts_as_paranoid_default_options = { + column: 'deleted', + recover_dependent_associations: false, + double_tap_destroys_fully: false, + } +``` +This overrides the defaults set by the gem but in turn can still be overriden +when defining a class as `acts_as_paranoid`. + ### Filtering If a record is deleted by ActsAsParanoid, it won't be retrieved when accessing @@ -246,7 +262,7 @@ p1.recover #=> fails validation! ### Status -A paranoid object could be deleted or destroyed fully. +A paranoid object could be deleted or destroyed fully. You can check if the object is deleted with the `deleted?` helper diff --git a/lib/acts_as_paranoid.rb b/lib/acts_as_paranoid.rb index b7d300e6..99673487 100644 --- a/lib/acts_as_paranoid.rb +++ b/lib/acts_as_paranoid.rb @@ -29,6 +29,11 @@ def acts_as_paranoid(options = {}) dependent_recovery_window: 2.minutes, double_tap_destroys_fully: true } + if defined?(Rails) + paranoid_configuration.merge!( + Hash(::Rails.try(:application).try(:config).try(:acts_as_paranoid_default_options)) + ) + end if options[:column_type] == "string" paranoid_configuration.merge!(deleted_value: "deleted") end From 3351826f7bf671fc3b6bacd44b0572fbe61cf73a Mon Sep 17 00:00:00 2001 From: Jay Schneider Date: Fri, 21 May 2021 14:31:33 +0200 Subject: [PATCH 2/2] test: global configuration for default options --- test/test_core.rb | 20 ++++++++++++++++++++ test/test_helper.rb | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/test/test_core.rb b/test/test_core.rb index d1648304..b5973bda 100644 --- a/test/test_core.rb +++ b/test/test_core.rb @@ -733,4 +733,24 @@ def test_deleted_inside_time_window assert_equal 0, ParanoidTime.deleted_inside_time_window(3.minutes.from_now, 1.minute).count end + + def test_configure_default_configuration + Rails.application.config.acts_as_paranoid_default_options = { + column: "test_column_name", + double_tap_destroys_fully: false, + } + + temporary_paranoiac = Class.new(ActiveRecord::Base) do + acts_as_paranoid + end + + expected_default_options = { + column_type: "time", + column: "test_column_name", + dependent_recovery_window: 2.minutes, + double_tap_destroys_fully: false, + recover_dependent_associations: true, + } + assert_equal expected_default_options, temporary_paranoiac.paranoid_configuration + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 026cb81a..ca48a1ea 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -37,6 +37,14 @@ file_path = File.join(log_dir, "test.log") ActiveRecord::Base.logger = Logger.new(file_path) +unless defined?(Rails) # simulate Rails config behaviour during tests + Rails = Struct.new(:application).new( + Struct.new(:config).new( # application attribute + OpenStruct.new # config attribute + ) + ) +end + # rubocop:disable Metrics/AbcSize def setup_db ActiveRecord::Schema.define(version: 1) do # rubocop:disable Metrics/BlockLength