collectiveidea / awesome_nested_set

An awesome replacement for acts_as_nested_set and better_nested_set.
MIT License
2.39k stars 492 forks source link

Keep current scope when calling roots #437

Closed p8 closed 3 years ago

p8 commented 3 years ago

6c5040c4c116cbff2bea69724972cff500f8334f 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 class methods, 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: https://github.com/rails/rails/issues/41066#issuecomment-757374841

Fixes: #436

parndt commented 3 years ago

@p8 thanks for this; I take it that being marked as draft means it's not ready?

p8 commented 3 years ago

@parndt Yes, I'm not sure this is the correct implementation. I'll create an issue in Rails to make sure.

clwy-cn commented 3 years ago

@parndt Please merge it。

parndt commented 3 years ago

@parndt Please merge it。

@clwy-cn no, thanks... I'm waiting for @p8 to confirm, as it's their PR.

p8 commented 3 years ago

I've created an issue in Rails: https://github.com/rails/rails/issues/41066

p8 commented 3 years ago

@parndt I've made some changes as suggested by the @kamipo, the author of the deprecation warning: https://github.com/rails/rails/issues/41066#issuecomment-757374841

p8 commented 3 years ago

Thanks @parndt !

p8 commented 3 years ago

@marcrohloff as the author of the original change, I thought you probably wanted to know about these changes.

clwy-ito commented 3 years ago

@parndt Has the problem been solved? Please push to rubygems.org

image

parndt commented 3 years ago

@clwy-cn to clarify, are you asking about the Rails 4.0 regression mentioned in #443, or are you asking about this work on the master branch being pushed as 4.0.0 ?

clwy-ito commented 3 years ago

@parndt thanks for reply, I am asking about this work. This is because the version 3.31 , scoped is not working properly,I am using this gem by

gem 'awesome_nested_set', git: 'https://github.com/collectiveidea/awesome_nested_set'

right now.

parndt commented 3 years ago

@clwy-cn what is your Rails version?

clwy-cn commented 3 years ago

@parndt

parndt commented 3 years ago

ruby 3.0.0

can you find out for me whether it works on Ruby 2.7.2?