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 ⇒ Object
Returns list of ancestors, starting from parent until root.
-
#descendants ⇒ 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.
-
#preload_tree(includes: nil) ⇒ Object
Fetches all descendants of this node and assigns the parent/children associations.
-
#root ⇒ Object
Returns the root node of the tree.
-
#root? ⇒ Boolean
Returns true if node has no parent, false otherwise.
-
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
-
#self_and_children ⇒ Object
Returns children (without subchildren) and current node itself.
-
#self_and_descendants ⇒ 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 ⇒ Object
Returns list of ancestors, starting from parent until root.
subchild1.ancestors # => [child1, root]
12 13 14 |
# File 'lib/acts_as_recursive_tree/model.rb', line 12 def ancestors(&) base_class.ancestors_of(self, &) end |
#descendants ⇒ Object
Returns list of descendants, starting from current node, not including current node.
root.descendants # => [child1, child2, subchild1, subchild2, subchild3, subchild4]
29 30 31 |
# File 'lib/acts_as_recursive_tree/model.rb', line 29 def descendants(&) base_class.descendants_of(self, &) end |
#leaf? ⇒ Boolean
Returns true if node has no children, false otherwise
subchild1.leaf? # => true child1.leaf? # => false
90 91 92 |
# File 'lib/acts_as_recursive_tree/model.rb', line 90 def leaf? children.none? end |
#leaves ⇒ Object
Returns all Leaves
74 75 76 |
# File 'lib/acts_as_recursive_tree/model.rb', line 74 def leaves base_class.leaves_of(self) end |
#preload_tree(includes: nil) ⇒ Object
Fetches all descendants of this node and assigns the parent/children associations
99 100 101 102 |
# File 'lib/acts_as_recursive_tree/model.rb', line 99 def preload_tree(includes: nil) ActsAsRecursiveTree::Preloaders::Descendants.new(self, includes:).preload! true end |
#root ⇒ Object
Returns the root node of the tree.
44 45 46 |
# File 'lib/acts_as_recursive_tree/model.rb', line 44 def root self_and_ancestors.where(_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
82 83 84 |
# File 'lib/acts_as_recursive_tree/model.rb', line 82 def root? attributes[_recursive_tree_config.parent_key.to_s].blank? end |
#self_and_ancestors ⇒ Object
Returns ancestors and current node itself.
subchild1.self_and_ancestors # => [subchild1, child1, root]
20 21 22 |
# File 'lib/acts_as_recursive_tree/model.rb', line 20 def self_and_ancestors(&) base_class.self_and_ancestors_of(self, &) end |
#self_and_children ⇒ Object
Returns children (without subchildren) and current node itself.
root.self_and_children # => [root, child1]
60 61 62 63 64 65 66 67 68 69 |
# 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 |
#self_and_descendants ⇒ Object
Returns list of descendants, starting from current node, including current node.
root.self_and_descendants # => [root, child1, child2, subchild1, subchild2, subchild3, subchild4]
38 39 40 |
# File 'lib/acts_as_recursive_tree/model.rb', line 38 def self_and_descendants(&) base_class.self_and_descendants_of(self, &) end |
#siblings ⇒ Object
Returns all siblings of the current node.
subchild1.siblings # => [subchild2]
52 53 54 |
# File 'lib/acts_as_recursive_tree/model.rb', line 52 def siblings self_and_siblings.where.not(id:) end |