-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
Keep current scope when calling roots #437
Conversation
@p8 thanks for this; I take it that being marked as draft means it's not ready? |
@parndt Yes, I'm not sure this is the correct implementation. I'll create an issue in Rails to make sure. |
@parndt Please merge it。 |
f735acd
to
2389a9e
Compare
I've created an issue in Rails: rails/rails#41066 |
1bd0137
to
ecce33c
Compare
6c5040c Fixed the Rails 6.1 deprecation warnings for inherited scoping by making scopes explicit using `Model.unscoped`, or `Model.default_scoped`. However the `default_scoped` method added to `Model.nested_set_scope` broke calling `roots` on `Relatable` when the nested set is scoped: bob = User.create!(name: 'Bob') bobs_root = bob.categories.create(name: 'Root Category') alice = User.create!(name: 'Alice') # Alice has no categories so root should return nil # Instead it returned bobs_root assert_equal(nil, alice.categories.root) Instead of using two separate classmethods `nested_set_scope_without_default_scope` and `nested_set_scope` that apply the scope, we move the `default_scoped` and `unscoped` calls to `self.class.base_class`. This fixes the `roots` scope and removes the deprecation warnings. Also see the Rails discussion: rails/rails#41066 (comment) Fixes: collectiveidea#436
@parndt I've made some changes as suggested by the @kamipo, the author of the deprecation warning: |
@@ -96,7 +96,7 @@ def case_condition_for_parent | |||
|
|||
def lock_nodes_between!(left_bound, right_bound) | |||
# select the rows in the model between a and d, and apply a lock | |||
instance_base_class.nested_set_scope. | |||
instance_base_class.default_scoped.nested_set_scope. |
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.
Substituting default_scoped
with unscoped
makes the specs pass as well.
Looking at the previous implementation it seems unscoped could be used, but the scope was probably inherited, hence the warnings...
6c5040c#diff-b1bc4bf228f372cd9fa501fa4d3ade576476fba30f75fbc2ec6de15fb9004444L99
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 is a tricky one. My typical case where things can get tricky is a default scope that filters out records like
default_scope -> { where(deleted: false) }
In this case it might seem safer to use unscoped
and make sure you are locking all records
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.
@marcrohloff Good point! I think you are correct. I've created a PR to change this:
#450
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 that PR still needs some work...
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 all your effort here @p8. I think it looks good, and we've followed the advice from the Rails team, and all the tests pass.
Thanks @parndt ! |
@marcrohloff as the author of the original change, I thought you probably wanted to know about these changes. |
@parndt Has the problem been solved? |
@parndt thanks for reply, I am asking about this work.
right now. |
@clwy-cn what is your Rails version? |
|
can you find out for me whether it works on Ruby 2.7.2? |
6c5040c Fixed the Rails 6.1 deprecation warnings for inherited scoping by
making scopes explicit using
Model.unscoped
, orModel.default_scoped
.However the
default_scoped
method added toModel.nested_set_scope
broke calling
roots
onRelatable
when the nested set is scoped:Instead of using two separate class methods,
nested_set_scope_without_default_scope
andnested_set_scope
, thatapply the scope, we move the
default_scoped
andunscoped
calls toself.class.base_class
. This fixes theroots
scope and removes thedeprecation warnings.
Also see the Rails discussion:
rails/rails#41066 (comment)
Fixes: #436