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

Warn if acts_as_paranoid is called more than once on the same model #492

Merged
merged 1 commit into from
Feb 2, 2022
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# paranoia Changelog

## 2.5.2

* [#492](https://github.com/rubysherpas/paranoia/pull/492) Warn if acts_as_paranoid is called more than once on the same model

[Ignatius Reza](https://github.com/ignatiusreza)

## 2.5.1

* [#481](https://github.com/rubysherpas/paranoia/pull/481) Replaces hard coded `deleted_at` with `paranoia_column`.
Expand Down
6 changes: 6 additions & 0 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ def restore_associated_records(recovery_window_range = nil)
ActiveSupport.on_load(:active_record) do
class ActiveRecord::Base
def self.acts_as_paranoid(options={})
if included_modules.include?(Paranoia)
puts "[WARN] #{self.name} is calling acts_as_paranoid more than once!"

return
end

define_model_callbacks :restore, :real_destroy

alias_method :really_destroyed?, :destroyed?
Expand Down
16 changes: 16 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ def test_paranoid_model_class_is_paranoid
assert_equal true, ParanoidModel.paranoid?
end

def test_doubly_paranoid_model_class_is_warned
assert_output(/DoublyParanoidModel is calling acts_as_paranoid more than once!/) do
DoublyParanoidModel.acts_as_paranoid
end

refute_equal(
DoublyParanoidModel.instance_method(:destroy).source_location,
DoublyParanoidModel.instance_method(:destroy_without_paranoia).source_location
)
end

def test_plain_models_are_not_paranoid
assert_equal false, PlainModel.new.paranoid?
end
Expand Down Expand Up @@ -1099,6 +1110,11 @@ class ParanoidModel < ActiveRecord::Base
acts_as_paranoid
end

class DoublyParanoidModel < ActiveRecord::Base
self.table_name = 'plain_models'
acts_as_paranoid
end

class ParanoidWithUnparanoids < ActiveRecord::Base
self.table_name = 'plain_models'
has_many :unparanoid_unique_models
Expand Down