forked from JackDanger/permanent_records
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rails 5.2 Fixing has_one callbacks (#3)
* table name update * Update to include special callbacks for Rails 5.2 * fixed some mis-committed code * simplifying the AR 5.2 code
- Loading branch information
1 parent
8b50ec5
commit 129b4c4
Showing
2 changed files
with
38 additions
and
0 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,33 @@ | ||
module HandlePermanentRecordsDestroyedInBelongsToAssociation | ||
def handle_dependency | ||
return unless load_target | ||
|
||
case options[:dependent] | ||
when :destroy | ||
target.destroy | ||
raise ActiveRecord::Rollback if target.respond_to?(:deleted?) && !target.deleted? | ||
else | ||
target.send(options[:dependent]) | ||
end | ||
end | ||
end | ||
|
||
module HandlePermanentRecordsDestroyedInHasOneAssociation | ||
def delete(method = options[:dependent]) | ||
if load_target | ||
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? | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Associations::BelongsToAssociation.prepend HandlePermanentRecordsDestroyedInBelongsToAssociation | ||
ActiveRecord::Associations::HasOneAssociation.prepend HandlePermanentRecordsDestroyedInHasOneAssociation |