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

Root ordering #442

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AlexeyMatskevich
Copy link

@AlexeyMatskevich AlexeyMatskevich commented Oct 28, 2024

Within the current implementation with root ordering there is a problem described in the readme

With numeric ordering, root nodes are, by default, assigned order values globally across the whole database table.

Also, changing the order of the root nodes is quite problematic since they are ordered globally as mentioned earlier.
Typical situation - I want to get all root blocks of a user and change their order:

block = Block.find(id)
old_order_block = block.siblings.where(user_id: user.id).find_by!(sort_order: sort_order)
old_order_block.prepend_sibling(block)

But in this case, the sort_order for all root nodes of one user will be a sparse sequence of 1, 10, 48 ....

I was thinking that in the case where parent_id = nil, you could rely on associations to restrict sort_order to root nodes within the association scope.

My option to implement this as simply as possible is to add an order_belong_to option, through which the foreign key field for the corresponding association is provided.

class Block < ApplicationRecord
  acts_as_tree numeric_order: true, order_belong_to: :user_id
end

class User < ApplicationRecord
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant