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

Permit acts_as_paranoid to be given on abstract AR classes #306

Open
wants to merge 1 commit into
base: rails4
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
9 changes: 9 additions & 0 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

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

What are you asserting to not happen here?

end

private
def get_featureful_model
FeaturefulModel.new(:name => "not empty")
Expand Down Expand Up @@ -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