-
Notifications
You must be signed in to change notification settings - Fork 191
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
Add Rails 6 Support #124
Add Rails 6 Support #124
Conversation
This reverts commit bd13b83.
Create a V6 class, in case there are differences that need to be coded later.
the association_scope method was made private in Rails public API.
…oject from Github
Hi @danielricecodes ! We have spent some time investigating the last failing specs on your branch. @jbryant92 was able to fix We think we understand the reason why the last two are failing. The reason is, named scopes are no longer leaking to class level in rails 6 (See associated Rails PR: rails/rails#32380) In our case, this line
is the culprit.(https://github.com/ActsAsParanoid/acts_as_paranoid/blob/master/lib/acts_as_paranoid/associations.rb#L34)
Few ideas that don't seem quite correct.
Since it is possible to use We also tried to prepend I hope this can help. If you have any ideas or comments, let us know ! |
Here are the changes I made to get it going with Rails 6.0.0, also the small fix for the spec: railsagency#1 |
Let the rails-6-support branch run with 6.0.0
@jbryant92 @RomainAlexandre thank you so much for helping with the build! |
lib/acts_as_paranoid/validations.rb
Outdated
end | ||
|
||
module ClassMethods | ||
def validates_uniqueness_of_without_deleted(*attr_names) | ||
validates_with UniquenessWithoutDeletedValidator[ActiveRecord::VERSION::MAJOR], _merge_attributes(attr_names) | ||
if ActiveRecord::VERSION::MAJOR == '6' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like unique validations tests should fail here since the code for AR v6 is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like ActiveRecord::VERSION::MAJOR
is an Int
so the condition is returning false and then running the old code and passing the tests 🎉
What was the reason this condition was added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. This can be reverted. I had it in here before when I was trying to figure out why I kept getting "6" is not a valid validator, must be "V5" or "V4". I understand the code a lot better now than I did when I made that commit :)
Almost there but not quite:
|
…d to avoid using the default scope
Morning @danielricecodes Let me know what you think ! |
Using unscoped when retrieving the belongs_to association with deleted
Crushing it @danielricecodes ! 🙌 |
@RomainAlexandre @jbryant92 thanks for your help. With a passing build I'd say its safe to merge! |
Do you know who we should ping to get this PR reviewed and merged ? |
Yes! I'll take a look soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, @danielricecodes and everyone who helped. I have just a few nits and something I need to check.
.travis.yml
Outdated
- bundler | ||
- directories: | ||
- vendor/bundle | ||
language: ruby |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This key is duplicated.
.gitignore
Outdated
@@ -4,3 +4,5 @@ pkg | |||
Gemfile.lock | |||
gemfiles/*.lock | |||
.idea/ | |||
|
|||
\.ruby-version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the \
can go?
@@ -38,8 +37,11 @@ def validate_each(record, attribute, value) | |||
protected | |||
|
|||
def build_relation(klass, attribute, value) | |||
return super(klass, klass.arel_table, attribute, value) if ActiveRecord::VERSION::MINOR == 0 | |||
super | |||
if ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::MAJOR == 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks a bit odd because you would think this class would only be used with Rails 5 (except it isn't because it's subclassed to V6
). I'm not sure how to resolve this without duplicating all of V5
(except build_relation
) to V6
, so 🤷♂️.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I wasn't sure what the best way to code this would be since most options seemed to involve just copying and pasting, so I opted for the fewest lines of code: simply making V6 an empty subclass of V5.
@@ -31,7 +31,7 @@ def #{target}_with_unscoped(*args) | |||
association = association(:#{target}) | |||
return nil if association.options[:polymorphic] && association.klass.nil? | |||
return #{target}_without_unscoped(*args) unless association.klass.paranoid? | |||
association.klass.with_deleted.scoping { #{target}_without_unscoped(*args) } | |||
association.klass.with_deleted.scoping { association.klass.unscoped { #{target}_without_unscoped(*args) } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to check if this doesn't have any unwanted side effects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to explain here why I made that change, if it helps railsagency#2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I created some explicit tests for this and it looks like everything is fine 👍. I'll merge this in and then create another PR to add those tests.
@@ -228,7 +228,7 @@ def test_double_belongs_to_with_deleted | |||
end | |||
|
|||
def test_mass_assignment_of_paranoid_column_enabled | |||
if ActiveRecord::VERSION::MAJOR > 4 && ActiveRecord::VERSION::MINOR > 1 | |||
if Gem.loaded_specs['activerecord'].version >= Gem::Version.new('5.2.0') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mvz - there are several places in the code that check the current ActiveRecord version. Since Rails 6 is out now, it might be worthwhile to release a new major version of this gem where old versions of Rails/Ruby are no longer supported. A new gem version that supports only Rails 5.2+ w/ Ruby 2.5+ only would be nice clean slate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's definitely a good idea!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind working on that after we get this PR merged.
Thanks @mvz I’ll work on a Rails 5.2+ only gem and offer a PR for that as well. |
This pull requests offers Support for Rails 6.0, which was released on August 15th, 2019.