Module: Mongoid::Acts::NestedSet::Relations

Included in:
Document::InstanceMethods
Defined in:
lib/mongoid_nested_set/relations.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

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

#descendantsObject

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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

Returns:

  • (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

Returns:

  • (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

#leavesObject

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_siblingObject

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

#levelObject

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_siblingObject

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

#rootObject

Returns root



6
7
8
# File 'lib/mongoid_nested_set/relations.rb', line 6

def root
  self_and_ancestors.first
end

#self_and_ancestorsObject

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_descendantsObject

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_siblingsObject

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

#siblingsObject

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