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