@@ -1,124 +1,124 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | module Redmine |
|
19 | 19 | module NestedSet |
|
20 | 20 | module Traversing |
|
21 | 21 | def self.included(base) |
|
22 | 22 | base.class_eval do |
|
23 | 23 | scope :roots, lambda {where :parent_id => nil} |
|
24 | 24 | scope :leaves, lambda {where "#{table_name}.rgt - #{table_name}.lft = ?", 1} |
|
25 | 25 | end |
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 28 | # Returns true if the element has no parent |
|
29 | 29 | def root? |
|
30 | 30 | parent_id.nil? |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | # Returns true if the element has a parent |
|
34 | 34 | def child? |
|
35 | 35 | !root? |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | # Returns true if the element has no children |
|
39 | 39 | def leaf? |
|
40 | 40 | new_record? || (rgt - lft == 1) |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | # Returns the root element (ancestor with no parent) |
|
44 | 44 | def root |
|
45 | 45 | self_and_ancestors.first |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | # Returns the children |
|
49 | 49 | def children |
|
50 | 50 | if id.nil? |
|
51 | 51 | nested_set_scope.none |
|
52 | 52 | else |
|
53 | 53 | self.class.order(:lft).where(:parent_id => id) |
|
54 | 54 | end |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | # Returns the descendants that have no children |
|
58 | 58 | def leaves |
|
59 | 59 | descendants.where("#{self.class.table_name}.rgt - #{self.class.table_name}.lft = ?", 1) |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | # Returns the siblings |
|
63 | 63 | def siblings |
|
64 | nested_set_scope.where(:parent_id => parent_id).where("id <> ?", id) | |
|
64 | nested_set_scope.where(:parent_id => parent_id).where("#{self.class.table_name}.id <> ?", id) | |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | # Returns the ancestors |
|
68 | 68 | def ancestors |
|
69 | 69 | if root? |
|
70 | 70 | nested_set_scope.none |
|
71 | 71 | else |
|
72 | 72 | nested_set_scope.where("#{self.class.table_name}.lft < ? AND #{self.class.table_name}.rgt > ?", lft, rgt) |
|
73 | 73 | end |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | # Returns the element and its ancestors |
|
77 | 77 | def self_and_ancestors |
|
78 | 78 | nested_set_scope.where("#{self.class.table_name}.lft <= ? AND #{self.class.table_name}.rgt >= ?", lft, rgt) |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | # Returns true if the element is an ancestor of other |
|
82 | 82 | def is_ancestor_of?(other) |
|
83 | 83 | same_nested_set_scope?(other) && other.lft > lft && other.rgt < rgt |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | # Returns true if the element equals other or is an ancestor of other |
|
87 | 87 | def is_or_is_ancestor_of?(other) |
|
88 | 88 | other == self || is_ancestor_of?(other) |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | # Returns the descendants |
|
92 | 92 | def descendants |
|
93 | 93 | if leaf? |
|
94 | 94 | nested_set_scope.none |
|
95 | 95 | else |
|
96 | 96 | nested_set_scope.where("#{self.class.table_name}.lft > ? AND #{self.class.table_name}.rgt < ?", lft, rgt) |
|
97 | 97 | end |
|
98 | 98 | end |
|
99 | 99 | |
|
100 | 100 | # Returns the element and its descendants |
|
101 | 101 | def self_and_descendants |
|
102 | 102 | nested_set_scope.where("#{self.class.table_name}.lft >= ? AND #{self.class.table_name}.rgt <= ?", lft, rgt) |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | # Returns true if the element is a descendant of other |
|
106 | 106 | def is_descendant_of?(other) |
|
107 | 107 | same_nested_set_scope?(other) && other.lft < lft && other.rgt > rgt |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | # Returns true if the element equals other or is a descendant of other |
|
111 | 111 | def is_or_is_descendant_of?(other) |
|
112 | 112 | other == self || is_descendant_of?(other) |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | # Returns the ancestors, the element and its descendants |
|
116 | 116 | def hierarchy |
|
117 | 117 | nested_set_scope.where( |
|
118 | 118 | "#{self.class.table_name}.lft >= :lft AND #{self.class.table_name}.rgt <= :rgt" + |
|
119 | 119 | " OR #{self.class.table_name}.lft < :lft AND #{self.class.table_name}.rgt > :rgt", |
|
120 | 120 | {:lft => lft, :rgt => rgt}) |
|
121 | 121 | end |
|
122 | 122 | end |
|
123 | 123 | end |
|
124 | 124 | end |
General Comments 0
You need to be logged in to leave comments.
Login now