diff --git a/CHANGELOG.md b/CHANGELOG.md index 2775702b..8c4e69b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 4c0b6f36..453a4e69 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -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? diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 3e9beeb6..8d8b16f7 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -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 @@ -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