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

Added instance methods to determine the relationship between 2 nodes #314

Merged
merged 2 commits into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,22 +346,27 @@ When you include ```has_closure_tree``` in your model, you can provide a hash to

* ```tag.root``` returns the root for this node
* ```tag.root?``` returns true if this is a root node
* ```tag.root_of?(node)``` returns true if current node is root of another one
* ```tag.child?``` returns true if this is a child node. It has a parent.
* ```tag.leaf?``` returns true if this is a leaf node. It has no children.
* ```tag.leaves``` is scoped to all leaf nodes in self_and_descendants.
* ```tag.depth``` returns the depth, or "generation", for this node in the tree. A root node will have a value of 0.
* ```tag.parent``` returns the node's immediate parent. Root nodes will return nil.
* ```tag.parent_of?(node)``` returns true if current node is parent of another one
* ```tag.children``` is a ```has_many``` of immediate children (just those nodes whose parent is the current node).
* ```tag.child_ids``` is an array of the IDs of the children.
* ```tag.child_of?(node)``` returns true if current node is child of another one
* ```tag.ancestors``` is a ordered scope of [ parent, grandparent, great grandparent, … ]. Note that the size of this array will always equal ```tag.depth```.
* ```tag.ancestor_ids``` is an array of the IDs of the ancestors.
* ```tag.ancestor_of?(node)``` returns true if current node is ancestor of another one
* ```tag.self_and_ancestors``` returns a scope containing self, parent, grandparent, great grandparent, etc.
* ```tag.self_and_ancestors_ids``` returns IDs containing self, parent, grandparent, great grandparent, etc.
* ```tag.siblings``` returns a scope containing all nodes with the same parent as ```tag```, excluding self.
* ```tag.sibling_ids``` returns an array of the IDs of the siblings.
* ```tag.self_and_siblings``` returns a scope containing all nodes with the same parent as ```tag```, including self.
* ```tag.descendants``` returns a scope of all children, childrens' children, etc., excluding self ordered by depth.
* ```tag.descendant_ids``` returns an array of the IDs of the descendants.
* ```tag.descendant_of?(node)``` returns true if current node is descendant of another one
* ```tag.self_and_descendants``` returns a scope of self, all children, childrens' children, etc., ordered by depth.
* ```tag.self_and_descendant_ids``` returns IDs of self, all children, childrens' children, etc., ordered by depth.
* ```tag.hash_tree``` returns an [ordered, nested hash](#nested-hashes) that can be depth-limited.
Expand Down
25 changes: 25 additions & 0 deletions lib/closure_tree/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ def sibling_ids
_ct.ids_from(siblings)
end

# node's parent is this record
def parent_of?(node)
self == node.parent
end

# node's root is this record
def root_of?(node)
self == node.root
end

# node's ancestors include this record
def ancestor_of?(node)
node.ancestors.include? self
end

# node is record's ancestor
def descendant_of?(node)
self.ancestors.include? node
end

# node is record's parent
def child_of?(node)
self.parent == node
end

# Alias for appending to the children collection.
# You can also add directly to the children collection, if you'd prefer.
def add_child(child_node)
Expand Down
37 changes: 37 additions & 0 deletions spec/label_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,41 @@ def roots_name_and_order
expect(tree[@b]).to eq({})
end
end

context 'relationship between nodes' do
before do
create_label_tree
end

it "checks parent of node" do
expect(@a1.parent_of?(@b1)).to be_truthy
expect(@c2.parent_of?(@d2)).to be_truthy
expect(@c1.parent_of?(@b1)).to be_falsey
end

it "checks children of node" do
expect(@d1.child_of?(@c1)).to be_truthy
expect(@c2.child_of?(@b1)).to be_truthy
expect(@c3.child_of?(@b1)).to be_falsey
end

it "checks root of node" do
expect(@a1.root_of?(@d1)).to be_truthy
expect(@a1.root_of?(@c2)).to be_truthy
expect(@a2.root_of?(@c2)).to be_falsey
end

it "checks ancestor of node" do
expect(@a1.ancestor_of?(@d1)).to be_truthy
expect(@b1.ancestor_of?(@d1)).to be_truthy
expect(@b1.ancestor_of?(@c3)).to be_falsey
end

it "checks descendant of node" do
expect(@c1.descendant_of?(@a1)).to be_truthy
expect(@d2.descendant_of?(@a1)).to be_truthy
expect(@b1.descendant_of?(@a2)).to be_falsey
end
end

end