Skip to content

Commit c1beee5

Browse files
committed
Add an instance method to check the relationship between 2 nodes
1 parent 5d44d22 commit c1beee5

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ When you include ```has_closure_tree``` in your model, you can provide a hash to
369369
* ```tag.descendant_of?(node)``` returns true if current node is descendant of another one
370370
* ```tag.self_and_descendants``` returns a scope of self, all children, childrens' children, etc., ordered by depth.
371371
* ```tag.self_and_descendant_ids``` returns IDs of self, all children, childrens' children, etc., ordered by depth.
372+
* ```tag.family_of?``` returns true if current node and another one have a same root.
372373
* ```tag.hash_tree``` returns an [ordered, nested hash](#nested-hashes) that can be depth-limited.
373374
* ```tag.find_by_path(path)``` returns the node whose name path *from ```tag```* is ```path```. See (#find_or_create_by_path).
374375
* ```tag.find_or_create_by_path(path)``` returns the node whose name path *from ```tag```* is ```path```, and will create the node if it doesn't exist already.See (#find_or_create_by_path).

lib/closure_tree/model.rb

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ def child_of?(node)
159159
self.parent == node
160160
end
161161

162+
# node and record have a same root
163+
def family_of?(node)
164+
self.root == node.root
165+
end
166+
162167
# Alias for appending to the children collection.
163168
# You can also add directly to the children collection, if you'd prefer.
164169
def add_child(child_node)

spec/label_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,14 @@ def roots_name_and_order
633633
expect(@d2.descendant_of?(@a1)).to be_truthy
634634
expect(@b1.descendant_of?(@a2)).to be_falsey
635635
end
636+
637+
it "checks descendant of node" do
638+
expect(@b1.family_of?(@b1)).to be_truthy
639+
expect(@a1.family_of?(@c1)).to be_truthy
640+
expect(@d3.family_of?(@a2)).to be_truthy
641+
expect(@c1.family_of?(@d2)).to be_truthy
642+
expect(@c3.family_of?(@a1)).to be_falsey
643+
end
636644
end
637645

638646
end

0 commit comments

Comments
 (0)