Module: Redmine::NestedSet::Traversing
- Defined in:
- lib/redmine/nested_set/traversing.rb
Overview
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns the ancestors.
-
#child? ⇒ Boolean
Returns true if the element has a parent.
-
#children ⇒ Object
Returns the children.
-
#descendants ⇒ Object
Returns the descendants.
-
#hierarchy ⇒ Object
Returns the ancestors, the element and its descendants.
-
#is_ancestor_of?(other) ⇒ Boolean
Returns true if the element is an ancestor of other.
-
#is_descendant_of?(other) ⇒ Boolean
Returns true if the element is a descendant of other.
-
#is_or_is_ancestor_of?(other) ⇒ Boolean
Returns true if the element equals other or is an ancestor of other.
-
#is_or_is_descendant_of?(other) ⇒ Boolean
Returns true if the element equals other or is a descendant of other.
-
#leaf? ⇒ Boolean
Returns true if the element has no children.
-
#leaves ⇒ Object
Returns the descendants that have no children.
-
#root ⇒ Object
Returns the root element (ancestor with no parent).
-
#root? ⇒ Boolean
Returns true if the element has no parent.
-
#self_and_ancestors ⇒ Object
Returns the element and its ancestors.
-
#self_and_descendants ⇒ Object
Returns the element and its descendants.
-
#siblings ⇒ Object
Returns the siblings.
Instance Method Details
#ancestors ⇒ Object
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
34 35 36 |
# File 'lib/redmine/nested_set/traversing.rb', line 34 def child? !root? end |
#children ⇒ Object
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 |
#descendants ⇒ Object
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 |
#hierarchy ⇒ Object
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
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
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
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
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
39 40 41 |
# File 'lib/redmine/nested_set/traversing.rb', line 39 def leaf? new_record? || (rgt - lft == 1) end |
#leaves ⇒ Object
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 |
#root ⇒ Object
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
29 30 31 |
# File 'lib/redmine/nested_set/traversing.rb', line 29 def root? parent_id.nil? end |
#self_and_ancestors ⇒ Object
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_descendants ⇒ Object
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 |
#siblings ⇒ Object
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 |