Module: ActsAsRecursiveTree::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/acts_as_recursive_tree/model.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#ancestors(&block) ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#descendants(&block) ⇒ Object
Returns list of descendants, starting from current node, not including current node.
-
#leaf? ⇒ Boolean
Returns true if node has no children, false otherwise.
-
#leaves ⇒ Object
Returns all Leaves.
-
#root ⇒ Object
Returns the root node of the tree.
-
#root? ⇒ Boolean
Returns true if node has no parent, false otherwise.
-
#self_and_ancestors(&block) ⇒ Object
Returns ancestors and current node itself.
-
#self_and_children ⇒ Object
Returns children (without subchildren) and current node itself.
-
#self_and_descendants(&block) ⇒ Object
Returns list of descendants, starting from current node, including current node.
-
#siblings ⇒ Object
Returns all siblings of the current node.
Instance Method Details
#ancestors(&block) ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
10 11 12 |
# File 'lib/acts_as_recursive_tree/model.rb', line 10 def ancestors(&block) base_class.ancestors_of(self, &block) end |
#descendants(&block) ⇒ Object
Returns list of descendants, starting from current node, not including current node.
root.descendants # => [child1, child2, subchild1, subchild2, subchild3, subchild4]
27 28 29 |
# File 'lib/acts_as_recursive_tree/model.rb', line 27 def descendants(&block) base_class.descendants_of(self, &block) end |
#leaf? ⇒ Boolean
Returns true if node has no children, false otherwise
subchild1.leaf? # => true child1.leaf? # => false
89 90 91 |
# File 'lib/acts_as_recursive_tree/model.rb', line 89 def leaf? !children.any? end |
#leaves ⇒ Object
Returns all Leaves
72 73 74 |
# File 'lib/acts_as_recursive_tree/model.rb', line 72 def leaves base_class.leaves_of(self) end |
#root ⇒ Object
Returns the root node of the tree.
42 43 44 |
# File 'lib/acts_as_recursive_tree/model.rb', line 42 def root self_and_ancestors.where(self._recursive_tree_config.parent_key => nil).first end |
#root? ⇒ Boolean
Returns true if node has no parent, false otherwise
subchild1.root? # => false root.root? # => true
81 82 83 |
# File 'lib/acts_as_recursive_tree/model.rb', line 81 def root? self.attributes[self._recursive_tree_config.parent_key.to_s].blank? end |
#self_and_ancestors(&block) ⇒ Object
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
18 19 20 |
# File 'lib/acts_as_recursive_tree/model.rb', line 18 def self_and_ancestors(&block) base_class.self_and_ancestors_of(self, &block) end |
#self_and_children ⇒ Object
Returns children (without subchildren) and current node itself.
root.self_and_children # => [root, child1]
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/acts_as_recursive_tree/model.rb', line 58 def self_and_children table = self.class.arel_table id = self.attributes[self._recursive_tree_config.primary_key.to_s] base_class.where( table[self._recursive_tree_config.primary_key].eq(id).or( table[self._recursive_tree_config.parent_key].eq(id) ) ) end |
#self_and_descendants(&block) ⇒ Object
Returns list of descendants, starting from current node, including current node.
root.self_and_descendants # => [root, child1, child2, subchild1, subchild2, subchild3, subchild4]
36 37 38 |
# File 'lib/acts_as_recursive_tree/model.rb', line 36 def self_and_descendants(&block) base_class.self_and_descendants_of(self, &block) end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
50 51 52 |
# File 'lib/acts_as_recursive_tree/model.rb', line 50 def siblings self_and_siblings.where.not(id: self.id) end |