Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default configuration #236

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/acts_as_paranoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is kind of important, that the class definition (rather this method call) is done after setting up the config, while I didn't want to change the behaviour of all other paranoiacs used in the tests. This is why I create this class inside of the test.

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
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading