Skip to content

Commit

Permalink
Provide alternative override for ActiveRecord below version 7
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Apr 18, 2022
1 parent 9d6575f commit 7c26072
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/acts_as_paranoid/association_reflection.rb
Expand Up @@ -3,13 +3,28 @@
module ActsAsParanoid
# Override for ActiveRecord::Reflection::AssociationReflection
module AssociationReflection
def scope_allows_automatic_inverse_of?(reflection, inverse_reflection)
if reflection.scope
if ActiveRecord::VERSION::MAJOR < 7
def can_find_inverse_of_automatically?(reflection)
options = reflection.options
return true if options[:with_deleted] && !options.fetch(:original_scope)

if reflection.macro == :belongs_to && options[:with_deleted]
return false if options[:inverse_of] == false
return false if options[:foreign_key]

!options.fetch(:original_scope)
else
super
end
end
else
def scope_allows_automatic_inverse_of?(reflection, inverse_reflection)
if reflection.scope
options = reflection.options
return true if options[:with_deleted] && !options.fetch(:original_scope)
end

super
super
end
end
end
end
58 changes: 58 additions & 0 deletions test/test_associations.rb
Expand Up @@ -140,13 +140,25 @@ class DoubleHasOneNotParanoid < HasOneNotParanoid
class ParanoidParent < ActiveRecord::Base
acts_as_paranoid
has_many :paranoid_children
has_many :paranoid_no_inverse_children
has_many :paranoid_foreign_key_children
end

class ParanoidChild < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_parent, with_deleted: true
end

class ParanoidNoInverseChild < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_parent, with_deleted: true, inverse_of: false
end

class ParanoidForeignKeyChild < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_parent, with_deleted: true, foreign_key: :paranoid_parent_id
end

# rubocop:disable Metrics/AbcSize
def setup
ActiveRecord::Schema.define(version: 1) do # rubocop:disable Metrics/BlockLength
Expand Down Expand Up @@ -274,6 +286,20 @@ def setup

timestamps t
end

create_table :paranoid_no_inverse_children do |t|
t.datetime :deleted_at
t.integer :paranoid_parent_id

timestamps t
end

create_table :paranoid_foreign_key_children do |t|
t.datetime :deleted_at
t.integer :paranoid_parent_id

timestamps t
end
end
end
# rubocop:enable Metrics/AbcSize
Expand Down Expand Up @@ -392,12 +418,44 @@ def test_belongs_to_with_deleted
end

def test_building_belongs_to_associations
paranoid_product = ParanoidProduct.new
paranoid_destroy_company =
ParanoidDestroyCompany.new(paranoid_products: [paranoid_product])

assert_equal paranoid_destroy_company,
paranoid_destroy_company.paranoid_products.first.paranoid_destroy_company
end

def test_building_belongs_to_associations_with_deleted
paranoid_child = ParanoidChild.new
paranoid_parent = ParanoidParent.new(paranoid_children: [paranoid_child])

assert_equal paranoid_parent, paranoid_parent.paranoid_children.first.paranoid_parent
end

def test_building_polymorphic_belongs_to_associations_with_deleted
paranoid_belongs_to = ParanoidBelongsToPolymorphic.new
paranoid_has_many =
ParanoidHasManyAsParent.new(paranoid_belongs_to_polymorphics: [paranoid_belongs_to])

assert_equal paranoid_has_many,
paranoid_has_many.paranoid_belongs_to_polymorphics.first.parent
end

def test_building_belongs_to_associations_with_deleted_with_inverse_of_false
paranoid_child = ParanoidNoInverseChild.new
paranoid_parent = ParanoidParent.new(paranoid_no_inverse_children: [paranoid_child])

assert_nil paranoid_parent.paranoid_no_inverse_children.first.paranoid_parent
end

def test_building_belongs_to_associations_with_deleted_with_foreign_key
paranoid_child = ParanoidForeignKeyChild.new
paranoid_parent = ParanoidParent.new(paranoid_foreign_key_children: [paranoid_child])

assert_nil paranoid_parent.paranoid_foreign_key_children.first.paranoid_parent
end

def test_belongs_to_with_deleted_as_inverse_of_has_many
has_many_reflection = ParanoidParent.reflect_on_association :paranoid_children
belongs_to_reflection = ParanoidChild.reflect_on_association :paranoid_parent
Expand Down

0 comments on commit 7c26072

Please sign in to comment.