Module: Redmine::NestedSet::Traversing

Defined in:
lib/redmine/nested_set/traversing.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
25
26
# File 'lib/redmine/nested_set/traversing.rb', line 21

def self.included(base)
  base.class_eval do
    scope :roots, lambda {where :parent_id => nil}
    scope :leaves, lambda {where "#{table_name}.rgt - #{table_name}.lft = ?", 1}
  end
end

Instance Method Details

#ancestorsObject

Returns the ancestors



68
69
70
71
72
73
74
# File 'lib/redmine/nested_set/traversing.rb', line 68

def ancestors
  if root?
    nested_set_scope.none
  else
    nested_set_scope.where("#{self.class.table_name}.lft < ? AND #{self.class.table_name}.rgt > ?", lft, rgt)
  end
end

#child?Boolean

Returns true if the element has a parent

Returns:

  • (Boolean)


34
35
36
# File 'lib/redmine/nested_set/traversing.rb', line 34

def child?
  !root?
end

#childrenObject

Returns the children



49
50
51
52
53
54
55
# File 'lib/redmine/nested_set/traversing.rb', line 49

def children
  if id.nil?
    nested_set_scope.none
  else
    self.class.order(:lft).where(:parent_id => id)
  end
end

#descendantsObject

Returns the descendants



92
93
94
95
96
97
98
# File 'lib/redmine/nested_set/traversing.rb', line 92

def descendants
  if leaf?
    nested_set_scope.none
  else
    nested_set_scope.where("#{self.class.table_name}.lft > ? AND #{self.class.table_name}.rgt < ?", lft, rgt)
  end
end

#hierarchyObject

Returns the ancestors, the element and its descendants



116
117
118
119
120
121
# File 'lib/redmine/nested_set/traversing.rb', line 116

def hierarchy
  nested_set_scope.where(
    "#{self.class.table_name}.lft >= :lft AND #{self.class.table_name}.rgt <= :rgt" +
    " OR #{self.class.table_name}.lft < :lft AND #{self.class.table_name}.rgt > :rgt",
    {:lft => lft, :rgt => rgt})
end

#is_ancestor_of?(other) ⇒ Boolean

Returns true if the element is an ancestor of other

Returns:

  • (Boolean)


82
83
84
# File 'lib/redmine/nested_set/traversing.rb', line 82

def is_ancestor_of?(other)
  same_nested_set_scope?(other) && other.lft > lft && other.rgt < rgt
end

#is_descendant_of?(other) ⇒ Boolean

Returns true if the element is a descendant of other

Returns:

  • (Boolean)


106
107
108
# File 'lib/redmine/nested_set/traversing.rb', line 106

def is_descendant_of?(other)
  same_nested_set_scope?(other) && other.lft < lft && other.rgt > rgt
end

#is_or_is_ancestor_of?(other) ⇒ Boolean

Returns true if the element equals other or is an ancestor of other

Returns:

  • (Boolean)


87
88
89
# File 'lib/redmine/nested_set/traversing.rb', line 87

def is_or_is_ancestor_of?(other)
  other == self || is_ancestor_of?(other)
end

#is_or_is_descendant_of?(other) ⇒ Boolean

Returns true if the element equals other or is a descendant of other

Returns:

  • (Boolean)


111
112
113
# File 'lib/redmine/nested_set/traversing.rb', line 111

def is_or_is_descendant_of?(other)
  other == self || is_descendant_of?(other)
end

#leaf?Boolean

Returns true if the element has no children

Returns:

  • (Boolean)


39
40
41
# File 'lib/redmine/nested_set/traversing.rb', line 39

def leaf?
  new_record? || (rgt - lft == 1)
end

#leavesObject

Returns the descendants that have no children



58
59
60
# File 'lib/redmine/nested_set/traversing.rb', line 58

def leaves
  descendants.where("#{self.class.table_name}.rgt - #{self.class.table_name}.lft = ?", 1)
end

#rootObject

Returns the root element (ancestor with no parent)



44
45
46
# File 'lib/redmine/nested_set/traversing.rb', line 44

def root
  self_and_ancestors.first
end

#root?Boolean

Returns true if the element has no parent

Returns:

  • (Boolean)


29
30
31
# File 'lib/redmine/nested_set/traversing.rb', line 29

def root?
  parent_id.nil?
end

#self_and_ancestorsObject

Returns the element and its ancestors



77
78
79
# File 'lib/redmine/nested_set/traversing.rb', line 77

def self_and_ancestors
  nested_set_scope.where("#{self.class.table_name}.lft <= ? AND #{self.class.table_name}.rgt >= ?", lft, rgt)
end

#self_and_descendantsObject

Returns the element and its descendants



101
102
103
# File 'lib/redmine/nested_set/traversing.rb', line 101

def self_and_descendants
  nested_set_scope.where("#{self.class.table_name}.lft >= ? AND #{self.class.table_name}.rgt <= ?", lft, rgt)
end

#siblingsObject

Returns the siblings



63
64
65
# File 'lib/redmine/nested_set/traversing.rb', line 63

def siblings
  nested_set_scope.where(:parent_id => parent_id).where("#{self.class.table_name}.id <> ?", id)
end