module ActsAsRecursiveTree::Model
Public Instance Methods
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
# File lib/acts_as_recursive_tree/model.rb, line 12 def ancestors(&block) base_class.ancestors_of(self, &block) end
Returns list of descendants, starting from current node, not including current node.
root.descendants # => [child1, child2, subchild1, subchild2, subchild3, subchild4]
# File lib/acts_as_recursive_tree/model.rb, line 29 def descendants(&block) base_class.descendants_of(self, &block) end
Returns true if node has no children, false otherwise
subchild1.leaf? # => true child1.leaf? # => false
# File lib/acts_as_recursive_tree/model.rb, line 90 def leaf? children.none? end
Returns all Leaves
# File lib/acts_as_recursive_tree/model.rb, line 74 def leaves base_class.leaves_of(self) end
Returns the root node of the tree.
# File lib/acts_as_recursive_tree/model.rb, line 44 def root self_and_ancestors.where(_recursive_tree_config.parent_key => nil).first end
Returns true if node has no parent, false otherwise
subchild1.root? # => false root.root? # => true
# File lib/acts_as_recursive_tree/model.rb, line 82 def root? attributes[_recursive_tree_config.parent_key.to_s].blank? end
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
# File lib/acts_as_recursive_tree/model.rb, line 20 def self_and_ancestors(&block) base_class.self_and_ancestors_of(self, &block) end
Returns children (without subchildren) and current node itself.
root.self_and_children # => [root, child1]
# File lib/acts_as_recursive_tree/model.rb, line 60 def self_and_children table = self.class.arel_table id = attributes[_recursive_tree_config.primary_key.to_s] base_class.where( table[_recursive_tree_config.primary_key].eq(id).or( table[_recursive_tree_config.parent_key].eq(id) ) ) end
Returns list of descendants, starting from current node, including current node.
root.self_and_descendants # => [root, child1, child2, subchild1, subchild2, subchild3, subchild4]
# File lib/acts_as_recursive_tree/model.rb, line 38 def self_and_descendants(&block) base_class.self_and_descendants_of(self, &block) end
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
# File lib/acts_as_recursive_tree/model.rb, line 52 def siblings self_and_siblings.where.not(id: id) end
Private Instance Methods
# File lib/acts_as_recursive_tree/model.rb, line 94 def base_class self.class.base_class end