diff --git a/README.md b/README.md index 7941a51..cb9a682 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,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 @@ -245,7 +261,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 bae0bb6..1bd5544 100644 --- a/lib/acts_as_paranoid.rb +++ b/lib/acts_as_paranoid.rb @@ -30,6 +30,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 diff --git a/test/test_core.rb b/test/test_core.rb index c041245..3be289c 100644 --- a/test/test_core.rb +++ b/test/test_core.rb @@ -1134,4 +1134,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 0fbf541..b332e04 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 + def timestamps(table) table.column :created_at, :timestamp, null: false table.column :updated_at, :timestamp, null: false