Skip to content

Commit

Permalink
Gererations methods (#56)
Browse files Browse the repository at this point in the history
Adds methods to query the current node's level, nodes of the same generation and a hash of all generations grouped by level.
  • Loading branch information
Mark Brewster authored and felixbuenemann committed Oct 9, 2016
1 parent 6a5b14c commit f0048bc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ def self.roots
end
EOV

# Returns a hash of all nodes grouped by their level in the tree structure.
#
# Class.generations { 0=> [root1, root2], 1=> [root1child1, root1child2, root2child1, root2child2] }
def self.generations
all.group_by{ |node| node.level }
end

if configuration[:counter_cache]
after_update :update_parents_counter_cache

Expand Down Expand Up @@ -281,6 +288,27 @@ def self_and_siblings
parent ? parent.children : self.class.roots
end

# Returns all the nodes at the same level in the tree as the current node.
#
# root1child1.generation [root1child2, root2child1, root2child2]
def generation
self_and_generation - [self]
end

# Returns a reference to the current node and all the nodes at the same level as it in the tree.
#
# root1child1.generation [root1child1, root1child2, root2child1, root2child2]
def self_and_generation
self.class.select {|node| node.level == self.level }
end

# Returns the level (depth) of the current node
#
# root1child1.level 1
def level
self.ancestors.size
end

# Returns children (without subchildren) and current node itself.
#
# root.self_and_children # => [root, child1]
Expand Down
55 changes: 54 additions & 1 deletion test/acts_as_tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ def test_counter_cache_being_used
end
end


class TreeTestWithTouch < ActsAsTreeTestCase
def setup
setup_db
Expand Down Expand Up @@ -541,3 +540,57 @@ def test_nullify
assert_nil root4_child.reload.external_parent_id
end
end

class GenertaionMethods < ActsAsTreeTestCase
def setup
setup_db

@root1 = TreeMixin.create!
@root_child1 = TreeMixin.create! parent_id: @root1.id
@child1_child = TreeMixin.create! parent_id: @root_child1.id
@child1_child_child = TreeMixin.create! parent_id: @child1_child.id
@root_child2 = TreeMixin.create! parent_id: @root1.id
@root2 = TreeMixin.create!
@root2_child1 = TreeMixin.create! parent_id: @root2.id
@root2_child2 = TreeMixin.create! parent_id: @root2.id
@root2_child1_child = TreeMixin.create! parent_id: @root2_child1.id
@root3 = TreeMixin.create!
end

def test_generations
assert_equal(
{
0 => [@root1, @root2, @root3],
1 => [@root_child1, @root_child2, @root2_child1, @root2_child2],
2 => [@child1_child, @root2_child1_child],
3 => [@child1_child_child]
},
TreeMixin.generations
)
end

def test_generation
assert_equal [@root2, @root3], @root1.generation
assert_equal [@root_child2, @root2_child1, @root2_child2],
@root_child1.generation
assert_equal [@root2_child1_child], @child1_child.generation
assert_equal [], @child1_child_child.generation
end

def test_self_and_generation
assert_equal [@root1, @root2, @root3], @root1.self_and_generation
assert_equal [@root_child1, @root_child2, @root2_child1, @root2_child2],
@root_child1.self_and_generation
assert_equal [@child1_child, @root2_child1_child],
@child1_child.self_and_generation
assert_equal [@child1_child_child], @child1_child_child.self_and_generation
end

def test_level
assert_equal 0, @root1.level
assert_equal 1, @root_child1.level
assert_equal 2, @child1_child.level
assert_equal 3, @child1_child_child.level
end
end

0 comments on commit f0048bc

Please sign in to comment.