module Mongoid::Acts::NestedSet::Relations
Public Instance Methods
ancestors()
click to toggle source
Returns an array of all parents
# File lib/mongoid_nested_set/relations.rb, line 21 def ancestors without_self self_and_ancestors end
descendants()
click to toggle source
Returns a set of all of its children and nested children
# File lib/mongoid_nested_set/relations.rb, line 61 def descendants without_self self_and_descendants end
is_ancestor_of?(other)
click to toggle source
# 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
Also aliased as: ancestor_of?
is_descendant_of?(other)
click to toggle source
# 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
Also aliased as: descendant_of?
is_or_is_ancestor_of?(other)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
Returns a set of all of its nested children which do not have children
# 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()
click to toggle source
Find the first sibling to the left
# 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()
click to toggle source
Returns the level of this object in the tree root level is 0
# File lib/mongoid_nested_set/relations.rb, line 46 def level parent_id.nil? ? 0 : ancestors.count end
right_sibling()
click to toggle source
Find the first sibling to the right
# 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()
click to toggle source
Returns root
# File lib/mongoid_nested_set/relations.rb, line 6 def root self_and_ancestors.first end
self_and_ancestors()
click to toggle source
Returns the array of all parents and self
# 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()
click to toggle source
Returns a set of itself and all of its nested children
# 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()
click to toggle source
Returns the array of all children of the parent, including self
# File lib/mongoid_nested_set/relations.rb, line 27 def self_and_siblings nested_set_scope.where(parent_field_name => parent_id) end
siblings()
click to toggle source
Returns the array of all children of the parent, except self
# File lib/mongoid_nested_set/relations.rb, line 33 def siblings without_self self_and_siblings end