Skip to content

Commit d366296

Browse files
committed
update readme and remove unused method
1 parent 6066514 commit d366296

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ When you include ```has_closure_tree``` in your model, you can provide a hash to
319319
* ```nil``` does nothing with descendant nodes
320320
* ```:name_column``` used by #```find_or_create_by_path```, #```find_by_path```, and ```ancestry_path``` instance methods. This is primarily useful if the model only has one required field (like a "tag").
321321
* ```:order``` used to set up [deterministic ordering](#deterministic-ordering)
322+
* ```:scope``` restricts root nodes and sibling ordering to specific columns. Can be a single symbol or an array of symbols. Example: ```scope: :user_id``` or ```scope: [:user_id, :group_id]```. This ensures that root nodes and siblings are scoped correctly when reordering. See [Ordering Roots](#ordering-roots) for more details.
322323
* ```:touch``` delegates to the `belongs_to` annotation for the parent, so `touch`ing cascades to all children (the performance of this for deep trees isn't currently optimal).
323324

324325
## Accessing Data
@@ -491,9 +492,30 @@ table. So for instance if you have 5 nodes with no parent, they will be ordered
491492
If your model represents many separate trees and you have a lot of records, this can cause performance
492493
problems, and doesn't really make much sense.
493494
494-
You can disable this default behavior by passing `dont_order_roots: true` as an option to your delcaration:
495+
You can scope root nodes and sibling ordering by passing the `scope` option:
495496
497+
```ruby
498+
class Block < ApplicationRecord
499+
has_closure_tree order: 'sort_order', numeric_order: true, scope: :user_id
500+
end
496501
```
502+
503+
This ensures that:
504+
* Root nodes are scoped by the specified columns. You can filter roots like: ```Block.roots.where(user_id: 123)```
505+
* Sibling reordering only affects nodes with the same scope values
506+
* Children reordering respects the parent's scope values
507+
508+
You can also scope by multiple columns:
509+
510+
```ruby
511+
class Block < ApplicationRecord
512+
has_closure_tree order: 'sort_order', numeric_order: true, scope: [:user_id, :group_id]
513+
end
514+
```
515+
516+
Alternatively, you can disable root ordering entirely by passing `dont_order_roots: true`:
517+
518+
```ruby
497519
has_closure_tree order: 'sort_order', numeric_order: true, dont_order_roots: true
498520
```
499521

lib/closure_tree/finders.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def without_instance(instance)
7272
end
7373

7474
def roots
75-
base_scope = where(_ct.parent_column_name => nil)
76-
scoped_roots = _ct.apply_root_scope(base_scope)
77-
_ct.scope_with_order(scoped_roots)
75+
_ct.scope_with_order(where(_ct.parent_column_name => nil))
7876
end
7977

8078
# Returns an arbitrary node that has no parents.

lib/closure_tree/support.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ def scope_columns
209209
end
210210
end
211211

212-
def apply_root_scope(scope)
213-
scope
214-
end
215-
216212
def scope_values_from_instance(instance)
217213
return {} unless options[:scope] && instance
218214

0 commit comments

Comments
 (0)