Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monkeypatch activerecord relations to work with rails >=5.2.0rc2 #435

Merged
merged 1 commit into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbonislawski

Don't you need to update env: RAILS='~> 5.2.0.beta2' line in allowed_failures section?

Copy link
Contributor Author

@bbonislawski bbonislawski Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch @eitoball . Fixed


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