Add scope option for root nodes and sibling ordering #466
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds support for scoping root nodes and sibling ordering, allowing users to maintain separate ordering sequences for different scope groups (e.g., per user, per tenant, etc.). This addresses the issue where root nodes are ordered globally across the entire table, which can cause performance problems and sparse ordering sequences when dealing with many separate trees.
Motivation
As described in PR #442 by @AlexeyMatskevich, when using numeric ordering with root nodes, the current implementation assigns order values globally across the whole database table. This means:
This PR implements a solution similar to how
awesome_nested_sethandles scoping, allowing root nodes and siblings to be scoped by one or more columns.Changes
New Option:
scopeThe
scopeoption accepts:scope: :user_idscope: [:user_id, :group_id]Behavior
When
scopeis specified:Root Nodes: Root queries can be filtered by scope values:
Sibling Queries: Siblings are automatically scoped - only nodes with matching scope values are considered siblings:
Reordering: When reordering siblings or children, only nodes with matching scope values are reordered:
Children Reordering: Children reordering respects the parent's scope values, ensuring that children with different scope values maintain separate ordering sequences.
Implementation Details
scopeis a Symbol or Array of Symbolsself_and_siblingsto apply scope conditionsreorder_with_parent_idin all adapters (MySQL, PostgreSQL, Generic) to accept and apply scope conditions_ct_reorder_siblings,_ct_reorder_children) to pass scope values from instancesExample Usage
Credits
This PR is based on the original work by @AlexeyMatskevich in PR #442, which introduced the
order_belong_tooption. This implementation uses thescopeoption name (as suggested by @seuros in the PR discussion) to align with the syntax used byacts_as_listandawesome_nested_set.Backward Compatibility
This change is fully backward compatible. Models without the
scopeoption continue to work exactly as before. The scope option is purely additive and does not affect existing functionality.