Skip to content

Commit

Permalink
Rails 5.2 Fixing has_one callbacks (#3)
Browse files Browse the repository at this point in the history
* 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
NikoRoberts authored Oct 10, 2019
1 parent 8b50ec5 commit 129b4c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/permanent_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,9 @@ def self.dependent_permanent_reflections(klass)

ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.send :include, PermanentRecords::ActiveRecord

if [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] == [5, 2] ||
ActiveRecord::VERSION::MAJOR > 5
require 'permanent_records/active_record_5_2'
end
end
33 changes: 33 additions & 0 deletions lib/permanent_records/active_record_5_2.rb
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

0 comments on commit 129b4c4

Please sign in to comment.