forked from JackDanger/permanent_records
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restrict to Rails < 7, minimize active record patch
... by delegating to super so we don't have to copy more of active record than necessary.
- Loading branch information
Showing
3 changed files
with
10 additions
and
15 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 |
---|---|---|
@@ -1,40 +1,34 @@ | ||
# rubocop:disable Metrics/AbcSize | ||
# Support destroy for rails belongs_to assocations. | ||
module HandlePermanentRecordsDestroyedInBelongsToAssociation | ||
def handle_dependency | ||
return unless load_target | ||
|
||
# only patch :destroy case and delegate to super otherwise | ||
case options[:dependent] | ||
when :destroy | ||
target.destroy | ||
raise ActiveRecord::Rollback if target.respond_to?(:deleted?) && !target.deleted? | ||
else | ||
target.send(options[:dependent]) | ||
super | ||
end | ||
end | ||
end | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
# rubocop:disable Metrics/CyclomaticComplexity | ||
# Support destroy for rails 5.2. has_on associations. | ||
# Support destroy for rails 5.2. has_one associations. | ||
module HandlePermanentRecordsDestroyedInHasOneAssociation | ||
def delete(method = options[:dependent]) | ||
return unless load_target | ||
|
||
# only patch :destroy case and delegate to super otherwise | ||
case method | ||
when :delete | ||
target.delete | ||
when :destroy | ||
target.destroyed_by_association = reflection | ||
target.destroy | ||
throw(:abort) if target.respond_to?(:deleted?) && !target.deleted? | ||
when :nullify | ||
target.update_columns(reflection.foreign_key => nil) if target.persisted? | ||
else | ||
super(method) | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
# rubocop:enable Metrics/MethodLength | ||
# rubocop:enable Metrics/AbcSize | ||
ActiveRecord::Associations::BelongsToAssociation.prepend(HandlePermanentRecordsDestroyedInBelongsToAssociation) | ||
ActiveRecord::Associations::HasOneAssociation.prepend(HandlePermanentRecordsDestroyedInHasOneAssociation) |
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