-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from ActsAsParanoid/issue-232-fix-association…
…-building Fix association building for belongs_to with :with_deleted option
- Loading branch information
Showing
4 changed files
with
172 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActsAsParanoid | ||
# Override for ActiveRecord::Reflection::AssociationReflection | ||
# | ||
# This makes automatic finding of inverse associations work where the | ||
# inverse is a belongs_to association with the :with_deleted option set. | ||
# | ||
# Specifying :with_deleted for the belongs_to association would stop the | ||
# inverse from being calculated because it sets scope where there was none, | ||
# and normally an association having a scope means ActiveRecord will not | ||
# automatically find the inverse association. | ||
# | ||
# This override adds an exception to that rule only for the case where the | ||
# scope was added just to support the :with_deleted option. | ||
module AssociationReflection | ||
if ActiveRecord::VERSION::MAJOR < 7 | ||
def can_find_inverse_of_automatically?(reflection) | ||
options = reflection.options | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters