Module: Mongoid::Acts::NestedSet::Relations
- Included in:
- Document::InstanceMethods
- Defined in:
- lib/mongoid_nested_set/relations.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns an array of all parents.
-
#descendants ⇒ Object
Returns a set of all of its children and nested children.
- #is_ancestor_of?(other) ⇒ Boolean (also: #ancestor_of?)
- #is_descendant_of?(other) ⇒ Boolean (also: #descendant_of?)
- #is_or_is_ancestor_of?(other) ⇒ Boolean
- #is_or_is_descendant_of?(other) ⇒ Boolean
-
#leaves ⇒ Object
Returns a set of all of its nested children which do not have children.
-
#left_sibling ⇒ Object
Find the first sibling to the left.
-
#level ⇒ Object
Returns the level of this object in the tree root level is 0.
-
#right_sibling ⇒ Object
Find the first sibling to the right.
-
#root ⇒ Object
Returns root.
-
#self_and_ancestors ⇒ Object
Returns the array of all parents and self.
-
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children.
-
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, including self.
-
#siblings ⇒ Object
Returns the array of all children of the parent, except self.
Instance Method Details
#ancestors ⇒ Object
Returns an array of all parents
21 22 23 |
# File 'lib/mongoid_nested_set/relations.rb', line 21 def ancestors without_self self_and_ancestors end |
#descendants ⇒ Object
Returns a set of all of its children and nested children
61 62 63 |
# File 'lib/mongoid_nested_set/relations.rb', line 61 def descendants without_self self_and_descendants end |
#is_ancestor_of?(other) ⇒ Boolean Also known as: ancestor_of?
77 78 79 |
# File 'lib/mongoid_nested_set/relations.rb', line 77 def is_ancestor_of?(other) self.left < other.left && other.left < self.right && same_scope?(other) end |
#is_descendant_of?(other) ⇒ Boolean Also known as: descendant_of?
66 67 68 |
# File 'lib/mongoid_nested_set/relations.rb', line 66 def is_descendant_of?(other) other.left < self.left && self.left < other.right && same_scope?(other) end |
#is_or_is_ancestor_of?(other) ⇒ Boolean
83 84 85 |
# File 'lib/mongoid_nested_set/relations.rb', line 83 def is_or_is_ancestor_of?(other) self.left <= other.left && other.left < self.right && same_scope?(other) end |
#is_or_is_descendant_of?(other) ⇒ Boolean
72 73 74 |
# File 'lib/mongoid_nested_set/relations.rb', line 72 def is_or_is_descendant_of?(other) other.left <= self.left && self.left < other.right && same_scope?(other) end |
#leaves ⇒ Object
Returns a set of all of its nested children which do not have children
39 40 41 |
# File 'lib/mongoid_nested_set/relations.rb', line 39 def leaves descendants.where("this.#{right_field_name} - this.#{left_field_name} == 1") end |
#left_sibling ⇒ Object
Find the first sibling to the left
89 90 91 |
# File 'lib/mongoid_nested_set/relations.rb', line 89 def left_sibling siblings.where(left_field_name => {"$lt" => left}).remove_order_by.desc(left_field_name).first end |
#level ⇒ Object
Returns the level of this object in the tree root level is 0
46 47 48 |
# File 'lib/mongoid_nested_set/relations.rb', line 46 def level parent_id.nil? ? 0 : ancestors.count end |
#right_sibling ⇒ Object
Find the first sibling to the right
95 96 97 |
# File 'lib/mongoid_nested_set/relations.rb', line 95 def right_sibling siblings.where(left_field_name => {"$gt" => left}).asc(left_field_name).first end |
#root ⇒ Object
Returns root
6 7 8 |
# File 'lib/mongoid_nested_set/relations.rb', line 6 def root self_and_ancestors.first end |
#self_and_ancestors ⇒ Object
Returns the array of all parents and self
12 13 14 15 16 17 |
# File 'lib/mongoid_nested_set/relations.rb', line 12 def self_and_ancestors nested_set_scope.where( left_field_name => {"$lte" => left}, right_field_name => {"$gte" => right} ) end |
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children
52 53 54 55 56 57 |
# File 'lib/mongoid_nested_set/relations.rb', line 52 def self_and_descendants nested_set_scope.where( left_field_name => {"$gte" => left}, right_field_name => {"$lte" => right} ) end |
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, including self
27 28 29 |
# File 'lib/mongoid_nested_set/relations.rb', line 27 def self_and_siblings nested_set_scope.where(parent_field_name => parent_id) end |
#siblings ⇒ Object
Returns the array of all children of the parent, except self
33 34 35 |
# File 'lib/mongoid_nested_set/relations.rb', line 33 def siblings without_self self_and_siblings end |