Skip to content

Commit

Permalink
Warn if acts_as_paranoid is called more than once on the same model
Browse files Browse the repository at this point in the history
which will breaks the method aliasing of `#destroy` and makes them lost
access to the original one defined by ActiveRecord
  • Loading branch information
ignatiusreza committed Jan 19, 2022
1 parent b6b5167 commit 1bee49e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
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

0 comments on commit 1bee49e

Please sign in to comment.