From 0813616c9338e2b792c4a94b5d82aa2280c492e0 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 7 Feb 2016 20:48:29 -0500 Subject: [PATCH] Permit acts_as_paranoid to be given on abstract AR classes Works around https://github.com/rails/rails/issues/10658 and https://github.com/rails/rails/issues/23413. --- lib/paranoia.rb | 9 +++++++++ test/paranoia_test.rb | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index a6bc20d0..94ad97f3 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -221,7 +221,16 @@ def self.paranoia_scope end unless options[:without_default_scope] + # this default_scope declaration sets paranoia_sentinel_value + # in constructors of objects using Paranoia default_scope { paranoia_scope } + # this class method makes it possible to use acts_as_paranoid on + # abstract ActiveRecord classes, working around + # https://github.com/rails/rails/issues/10658 and + # https://github.com/rails/rails/issues/23413 + def self.default_scope + paranoia_scope + end end before_restore { diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index a49a4d4e..21a5a554 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -41,6 +41,7 @@ def setup! 'unparanoid_unique_models' => 'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER', 'active_column_models' => 'deleted_at DATETIME, active BOOLEAN', 'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN', + 'paranoid_children' => 'deleted_at DATETIME', 'without_default_scope_models' => 'deleted_at DATETIME' }.each do |table_name, columns_as_sql_string| ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})" @@ -936,6 +937,14 @@ def test_callbacks_for_counter_cache_column_update_on_really_destroy! end end + def test_abstract_intermediary + ParanoidChild.create! + # triggers https://github.com/rails/rails/issues/10658 / + # https://github.com/rails/rails/issues/23413 + # if default_scope is specified as a closure instead of a class method + child = ParanoidChild.first + end + private def get_featureful_model FeaturefulModel.new(:name => "not empty") @@ -1208,3 +1217,11 @@ class ParanoidBelongsTo < ActiveRecord::Base belongs_to :paranoid_has_one end end + +class AbstractIntermediary < ActiveRecord::Base + self.abstract_class = true + acts_as_paranoid +end + +class ParanoidChild < AbstractIntermediary +end