Skip to content

Commit

Permalink
add in_subtree_of? helper function, for symmetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Roguelazer committed Jun 12, 2024
1 parent 498638e commit b594e99
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ The yellow nodes are those returned by the method.
| includes self |self..indirects |root..self |
|`sibling_ids` |`subtree_ids` |`path_ids` |
|`has_siblings?` | | |
|`sibling_of?(node)` | | |
|`sibling_of?(node)` |`in_subtree_of?` | |

When using `STI` all classes are returned from the scopes unless you specify otherwise using `where(:type => "ChildClass")`.

Expand Down
4 changes: 4 additions & 0 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ def subtree_ids depth_options = {}
subtree(depth_options).pluck(self.class.primary_key)
end

def in_subtree_of?(node)
id == node.id || descendant_of?(node)
end

# Callback disabling

def without_ancestry_callbacks
Expand Down
4 changes: 4 additions & 0 deletions test/concerns/tree_predicate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def test_tree_predicates
# Descendants assertions
assert children.map { |n| !root.descendant_of?(n) }.all?
assert children.map { |n| n.descendant_of?(root) }.all?
# Subtrees assertions
assert children.map { |n| n.in_subtree_of?(root) }.all?
assert children.map { |n| !root.in_subtree_of?(n) }.all?
assert root.in_subtree_of?(root)
end
end
end
Expand Down

0 comments on commit b594e99

Please sign in to comment.