Module: Redmine::NestedSet::Traversing
- Defined in:
- lib/redmine/nested_set/traversing.rb
Class Method Summary collapse
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.
Class Method Details
.included(base) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/redmine/nested_set/traversing.rb', line 23 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
#ancestors ⇒ Object
Returns the ancestors
70 71 72 73 74 75 76 |
# File 'lib/redmine/nested_set/traversing.rb', line 70 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
36 37 38 |
# File 'lib/redmine/nested_set/traversing.rb', line 36 def child? !root? end |
#children ⇒ Object
Returns the children
51 52 53 54 55 56 57 |
# File 'lib/redmine/nested_set/traversing.rb', line 51 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
94 95 96 97 98 99 100 |
# File 'lib/redmine/nested_set/traversing.rb', line 94 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
118 119 120 121 122 123 |
# File 'lib/redmine/nested_set/traversing.rb', line 118 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
84 85 86 |
# File 'lib/redmine/nested_set/traversing.rb', line 84 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
108 109 110 |
# File 'lib/redmine/nested_set/traversing.rb', line 108 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
89 90 91 |
# File 'lib/redmine/nested_set/traversing.rb', line 89 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
113 114 115 |
# File 'lib/redmine/nested_set/traversing.rb', line 113 def is_or_is_descendant_of?(other) other == self || is_descendant_of?(other) end |
#leaf? ⇒ Boolean
Returns true if the element has no children
41 42 43 |
# File 'lib/redmine/nested_set/traversing.rb', line 41 def leaf? new_record? || (rgt - lft == 1) end |
#leaves ⇒ Object
Returns the descendants that have no children
60 61 62 |
# File 'lib/redmine/nested_set/traversing.rb', line 60 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)
46 47 48 |
# File 'lib/redmine/nested_set/traversing.rb', line 46 def root self_and_ancestors.first end |
#root? ⇒ Boolean
Returns true if the element has no parent
31 32 33 |
# File 'lib/redmine/nested_set/traversing.rb', line 31 def root? parent_id.nil? end |
#self_and_ancestors ⇒ Object
Returns the element and its ancestors
79 80 81 |
# File 'lib/redmine/nested_set/traversing.rb', line 79 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
103 104 105 |
# File 'lib/redmine/nested_set/traversing.rb', line 103 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
65 66 67 |
# File 'lib/redmine/nested_set/traversing.rb', line 65 def siblings nested_set_scope.where(:parent_id => parent_id).where("#{self.class.table_name}.id <> ?", id) end |