Skip to content

Commit

Permalink
Monkeypatch activerecord relations to work with rails >=5.2.0rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
bbonislawski committed Mar 23, 2018
1 parent a328640 commit 65649aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ env:
- RAILS='~> 4.2.0'
- RAILS='~> 5.0.0'
- RAILS='~> 5.1.0'
- RAILS='~> 5.2.0.beta2'
- RAILS='~> 5.2.0.rc2'

matrix:
allow_failures:
Expand All @@ -24,5 +24,5 @@ matrix:
rvm: jruby-9.1.6.0
- env: RAILS='~> 5.1.0'
rvm: jruby-9.1.6.0
- env: RAILS='~> 5.2.0.beta2'
- env: RAILS='~> 5.2.0.rc2'
rvm: jruby-9.1.6.0
1 change: 1 addition & 0 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_record' unless defined? ActiveRecord
require 'paranoia/active_record_patches'

module Paranoia
@@default_sentinel_value = nil
Expand Down
41 changes: 41 additions & 0 deletions lib/paranoia/active_record_patches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module HandleParanoiaDestroyedInBelongsToAssociation
def handle_dependency
return unless load_target

case options[:dependent]
when :destroy
target.destroy
if defined? target.deleted?
raise ActiveRecord::Rollback unless target.deleted?
else
raise ActiveRecord::Rollback unless target.destroyed?
end
else
target.send(options[:dependent])
end
end
end

module HandleParanoiaDestroyedInHasOneAssociation
def delete(method = options[:dependent])
if load_target
case method
when :delete
target.delete
when :destroy
target.destroyed_by_association = reflection
target.destroy
if defined? target.deleted?
throw(:abort) unless target.deleted?
else
throw(:abort) unless target.destroyed?
end
when :nullify
target.update_columns(reflection.foreign_key => nil) if target.persisted?
end
end
end
end

ActiveRecord::Associations::BelongsToAssociation.prepend HandleParanoiaDestroyedInBelongsToAssociation
ActiveRecord::Associations::HasOneAssociation.prepend HandleParanoiaDestroyedInHasOneAssociation

0 comments on commit 65649aa

Please sign in to comment.