@@ -1,865 +1,865 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | class QueryColumn |
|
19 | 19 | attr_accessor :name, :sortable, :groupable, :default_order |
|
20 | 20 | include Redmine::I18n |
|
21 | 21 | |
|
22 | 22 | def initialize(name, options={}) |
|
23 | 23 | self.name = name |
|
24 | 24 | self.sortable = options[:sortable] |
|
25 | 25 | self.groupable = options[:groupable] || false |
|
26 | 26 | if groupable == true |
|
27 | 27 | self.groupable = name.to_s |
|
28 | 28 | end |
|
29 | 29 | self.default_order = options[:default_order] |
|
30 | 30 | @caption_key = options[:caption] || "field_#{name}" |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | def caption |
|
34 | 34 | l(@caption_key) |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | # Returns true if the column is sortable, otherwise false |
|
38 | 38 | def sortable? |
|
39 | 39 | !@sortable.nil? |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def sortable |
|
43 | 43 | @sortable.is_a?(Proc) ? @sortable.call : @sortable |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def value(issue) |
|
47 | 47 | issue.send name |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def css_classes |
|
51 | 51 | name |
|
52 | 52 | end |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | class QueryCustomFieldColumn < QueryColumn |
|
56 | 56 | |
|
57 | 57 | def initialize(custom_field) |
|
58 | 58 | self.name = "cf_#{custom_field.id}".to_sym |
|
59 | 59 | self.sortable = custom_field.order_statement || false |
|
60 | 60 | if %w(list date bool int).include?(custom_field.field_format) && !custom_field.multiple? |
|
61 | 61 | self.groupable = custom_field.order_statement |
|
62 | 62 | end |
|
63 | 63 | self.groupable ||= false |
|
64 | 64 | @cf = custom_field |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def caption |
|
68 | 68 | @cf.name |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def custom_field |
|
72 | 72 | @cf |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | def value(issue) |
|
76 | 76 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} |
|
77 | 77 | cv.size > 1 ? cv : cv.first |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def css_classes |
|
81 | 81 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
82 | 82 | end |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | class Query < ActiveRecord::Base |
|
86 | 86 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | belongs_to :project |
|
90 | 90 | belongs_to :user |
|
91 | 91 | serialize :filters |
|
92 | 92 | serialize :column_names |
|
93 | 93 | serialize :sort_criteria, Array |
|
94 | 94 | |
|
95 | 95 | attr_protected :project_id, :user_id |
|
96 | 96 | |
|
97 | 97 | validates_presence_of :name |
|
98 | 98 | validates_length_of :name, :maximum => 255 |
|
99 | 99 | validate :validate_query_filters |
|
100 | 100 | |
|
101 | 101 | @@operators = { "=" => :label_equals, |
|
102 | 102 | "!" => :label_not_equals, |
|
103 | 103 | "o" => :label_open_issues, |
|
104 | 104 | "c" => :label_closed_issues, |
|
105 | 105 | "!*" => :label_none, |
|
106 | 106 | "*" => :label_all, |
|
107 | 107 | ">=" => :label_greater_or_equal, |
|
108 | 108 | "<=" => :label_less_or_equal, |
|
109 | 109 | "><" => :label_between, |
|
110 | 110 | "<t+" => :label_in_less_than, |
|
111 | 111 | ">t+" => :label_in_more_than, |
|
112 | 112 | "t+" => :label_in, |
|
113 | 113 | "t" => :label_today, |
|
114 | 114 | "w" => :label_this_week, |
|
115 | 115 | ">t-" => :label_less_than_ago, |
|
116 | 116 | "<t-" => :label_more_than_ago, |
|
117 | 117 | "t-" => :label_ago, |
|
118 | 118 | "~" => :label_contains, |
|
119 | 119 | "!~" => :label_not_contains } |
|
120 | 120 | |
|
121 | 121 | cattr_reader :operators |
|
122 | 122 | |
|
123 | 123 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
124 | 124 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
125 | 125 | :list_optional => [ "=", "!", "!*", "*" ], |
|
126 | 126 | :list_subprojects => [ "*", "!*", "=" ], |
|
127 | 127 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], |
|
128 | 128 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], |
|
129 | :string => [ "=", "~", "!", "!~" ], | |
|
130 | :text => [ "~", "!~" ], | |
|
129 | :string => [ "=", "~", "!", "!~", "!*", "*" ], | |
|
130 | :text => [ "~", "!~", "!*", "*" ], | |
|
131 | 131 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
132 | 132 | :float => [ "=", ">=", "<=", "><", "!*", "*" ] } |
|
133 | 133 | |
|
134 | 134 | cattr_reader :operators_by_filter_type |
|
135 | 135 | |
|
136 | 136 | @@available_columns = [ |
|
137 | 137 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
138 | 138 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
139 | 139 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
140 | 140 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
141 | 141 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
142 | 142 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
143 | 143 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
144 | 144 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
145 | 145 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
146 | 146 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
147 | 147 | QueryColumn.new(:fixed_version, :sortable => ["#{Version.table_name}.effective_date", "#{Version.table_name}.name"], :default_order => 'desc', :groupable => true), |
|
148 | 148 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
149 | 149 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
150 | 150 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
151 | 151 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
152 | 152 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
153 | 153 | ] |
|
154 | 154 | cattr_reader :available_columns |
|
155 | 155 | |
|
156 | 156 | named_scope :visible, lambda {|*args| |
|
157 | 157 | user = args.shift || User.current |
|
158 | 158 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
159 | 159 | user_id = user.logged? ? user.id : 0 |
|
160 | 160 | { |
|
161 | 161 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], |
|
162 | 162 | :include => :project |
|
163 | 163 | } |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | def initialize(attributes=nil, *args) |
|
167 | 167 | super attributes |
|
168 | 168 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
169 | 169 | @is_for_all = project.nil? |
|
170 | 170 | end |
|
171 | 171 | |
|
172 | 172 | def validate_query_filters |
|
173 | 173 | filters.each_key do |field| |
|
174 | 174 | if values_for(field) |
|
175 | 175 | case type_for(field) |
|
176 | 176 | when :integer |
|
177 | 177 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
178 | 178 | when :float |
|
179 | 179 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+(\.\d*)?$/) } |
|
180 | 180 | when :date, :date_past |
|
181 | 181 | case operator_for(field) |
|
182 | 182 | when "=", ">=", "<=", "><" |
|
183 | 183 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } |
|
184 | 184 | when ">t-", "<t-", "t-" |
|
185 | 185 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
186 | 186 | end |
|
187 | 187 | end |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | add_filter_error(field, :blank) unless |
|
191 | 191 | # filter requires one or more values |
|
192 | 192 | (values_for(field) and !values_for(field).first.blank?) or |
|
193 | 193 | # filter doesn't require any value |
|
194 | 194 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
195 | 195 | end if filters |
|
196 | 196 | end |
|
197 | 197 | |
|
198 | 198 | def add_filter_error(field, message) |
|
199 | 199 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') |
|
200 | 200 | errors.add(:base, m) |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | # Returns true if the query is visible to +user+ or the current user. |
|
204 | 204 | def visible?(user=User.current) |
|
205 | 205 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | def editable_by?(user) |
|
209 | 209 | return false unless user |
|
210 | 210 | # Admin can edit them all and regular users can edit their private queries |
|
211 | 211 | return true if user.admin? || (!is_public && self.user_id == user.id) |
|
212 | 212 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
213 | 213 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
214 | 214 | end |
|
215 | 215 | |
|
216 | 216 | def available_filters |
|
217 | 217 | return @available_filters if @available_filters |
|
218 | 218 | |
|
219 | 219 | trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
220 | 220 | |
|
221 | 221 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
222 | 222 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, |
|
223 | 223 | "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } }, |
|
224 | 224 | "subject" => { :type => :text, :order => 8 }, |
|
225 | 225 | "created_on" => { :type => :date_past, :order => 9 }, |
|
226 | 226 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
227 | 227 | "start_date" => { :type => :date, :order => 11 }, |
|
228 | 228 | "due_date" => { :type => :date, :order => 12 }, |
|
229 | 229 | "estimated_hours" => { :type => :float, :order => 13 }, |
|
230 | 230 | "done_ratio" => { :type => :integer, :order => 14 }} |
|
231 | 231 | |
|
232 | 232 | principals = [] |
|
233 | 233 | if project |
|
234 | 234 | principals += project.principals.sort |
|
235 | 235 | unless project.leaf? |
|
236 | 236 | subprojects = project.descendants.visible.all |
|
237 | 237 | if subprojects.any? |
|
238 | 238 | @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => subprojects.collect{|s| [s.name, s.id.to_s] } } |
|
239 | 239 | principals += Principal.member_of(subprojects) |
|
240 | 240 | end |
|
241 | 241 | end |
|
242 | 242 | else |
|
243 | 243 | all_projects = Project.visible.all |
|
244 | 244 | if all_projects.any? |
|
245 | 245 | # members of visible projects |
|
246 | 246 | principals += Principal.member_of(all_projects) |
|
247 | 247 | |
|
248 | 248 | # project filter |
|
249 | 249 | project_values = [] |
|
250 | 250 | if User.current.logged? && User.current.memberships.any? |
|
251 | 251 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
252 | 252 | end |
|
253 | 253 | Project.project_tree(all_projects) do |p, level| |
|
254 | 254 | prefix = (level > 0 ? ('--' * level + ' ') : '') |
|
255 | 255 | project_values << ["#{prefix}#{p.name}", p.id.to_s] |
|
256 | 256 | end |
|
257 | 257 | @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty? |
|
258 | 258 | end |
|
259 | 259 | end |
|
260 | 260 | principals.uniq! |
|
261 | 261 | principals.sort! |
|
262 | 262 | users = principals.select {|p| p.is_a?(User)} |
|
263 | 263 | |
|
264 | 264 | assigned_to_values = [] |
|
265 | 265 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
266 | 266 | assigned_to_values += (Setting.issue_group_assignment? ? principals : users).collect{|s| [s.name, s.id.to_s] } |
|
267 | 267 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => assigned_to_values } unless assigned_to_values.empty? |
|
268 | 268 | |
|
269 | 269 | author_values = [] |
|
270 | 270 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
271 | 271 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
272 | 272 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => author_values } unless author_values.empty? |
|
273 | 273 | |
|
274 | 274 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
275 | 275 | @available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty? |
|
276 | 276 | |
|
277 | 277 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
278 | 278 | @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty? |
|
279 | 279 | |
|
280 | 280 | if User.current.logged? |
|
281 | 281 | @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } |
|
282 | 282 | end |
|
283 | 283 | |
|
284 | 284 | if project |
|
285 | 285 | # project specific filters |
|
286 | 286 | categories = project.issue_categories.all |
|
287 | 287 | unless categories.empty? |
|
288 | 288 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => categories.collect{|s| [s.name, s.id.to_s] } } |
|
289 | 289 | end |
|
290 | 290 | versions = project.shared_versions.all |
|
291 | 291 | unless versions.empty? |
|
292 | 292 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
293 | 293 | end |
|
294 | 294 | add_custom_fields_filters(project.all_issue_custom_fields) |
|
295 | 295 | else |
|
296 | 296 | # global filters for cross project issue list |
|
297 | 297 | system_shared_versions = Version.visible.find_all_by_sharing('system') |
|
298 | 298 | unless system_shared_versions.empty? |
|
299 | 299 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
300 | 300 | end |
|
301 | 301 | add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true})) |
|
302 | 302 | end |
|
303 | 303 | @available_filters |
|
304 | 304 | end |
|
305 | 305 | |
|
306 | 306 | def add_filter(field, operator, values) |
|
307 | 307 | # values must be an array |
|
308 | 308 | return unless values.nil? || values.is_a?(Array) |
|
309 | 309 | # check if field is defined as an available filter |
|
310 | 310 | if available_filters.has_key? field |
|
311 | 311 | filter_options = available_filters[field] |
|
312 | 312 | # check if operator is allowed for that filter |
|
313 | 313 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
314 | 314 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
315 | 315 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
316 | 316 | #end |
|
317 | 317 | filters[field] = {:operator => operator, :values => (values || [''])} |
|
318 | 318 | end |
|
319 | 319 | end |
|
320 | 320 | |
|
321 | 321 | def add_short_filter(field, expression) |
|
322 | 322 | return unless expression && available_filters.has_key?(field) |
|
323 | 323 | field_type = available_filters[field][:type] |
|
324 | 324 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| |
|
325 | 325 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ |
|
326 | 326 | add_filter field, operator, $1.present? ? $1.split('|') : [''] |
|
327 | 327 | end || add_filter(field, '=', expression.split('|')) |
|
328 | 328 | end |
|
329 | 329 | |
|
330 | 330 | # Add multiple filters using +add_filter+ |
|
331 | 331 | def add_filters(fields, operators, values) |
|
332 | 332 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) |
|
333 | 333 | fields.each do |field| |
|
334 | 334 | add_filter(field, operators[field], values && values[field]) |
|
335 | 335 | end |
|
336 | 336 | end |
|
337 | 337 | end |
|
338 | 338 | |
|
339 | 339 | def has_filter?(field) |
|
340 | 340 | filters and filters[field] |
|
341 | 341 | end |
|
342 | 342 | |
|
343 | 343 | def type_for(field) |
|
344 | 344 | available_filters[field][:type] if available_filters.has_key?(field) |
|
345 | 345 | end |
|
346 | 346 | |
|
347 | 347 | def operator_for(field) |
|
348 | 348 | has_filter?(field) ? filters[field][:operator] : nil |
|
349 | 349 | end |
|
350 | 350 | |
|
351 | 351 | def values_for(field) |
|
352 | 352 | has_filter?(field) ? filters[field][:values] : nil |
|
353 | 353 | end |
|
354 | 354 | |
|
355 | 355 | def value_for(field, index=0) |
|
356 | 356 | (values_for(field) || [])[index] |
|
357 | 357 | end |
|
358 | 358 | |
|
359 | 359 | def label_for(field) |
|
360 | 360 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
361 | 361 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) |
|
362 | 362 | end |
|
363 | 363 | |
|
364 | 364 | def available_columns |
|
365 | 365 | return @available_columns if @available_columns |
|
366 | 366 | @available_columns = ::Query.available_columns.dup |
|
367 | 367 | @available_columns += (project ? |
|
368 | 368 | project.all_issue_custom_fields : |
|
369 | 369 | IssueCustomField.find(:all) |
|
370 | 370 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
371 | 371 | |
|
372 | 372 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
373 | 373 | index = nil |
|
374 | 374 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
375 | 375 | index = (index ? index + 1 : -1) |
|
376 | 376 | # insert the column after estimated_hours or at the end |
|
377 | 377 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
378 | 378 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", |
|
379 | 379 | :default_order => 'desc', |
|
380 | 380 | :caption => :label_spent_time |
|
381 | 381 | ) |
|
382 | 382 | end |
|
383 | 383 | @available_columns |
|
384 | 384 | end |
|
385 | 385 | |
|
386 | 386 | def self.available_columns=(v) |
|
387 | 387 | self.available_columns = (v) |
|
388 | 388 | end |
|
389 | 389 | |
|
390 | 390 | def self.add_available_column(column) |
|
391 | 391 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
392 | 392 | end |
|
393 | 393 | |
|
394 | 394 | # Returns an array of columns that can be used to group the results |
|
395 | 395 | def groupable_columns |
|
396 | 396 | available_columns.select {|c| c.groupable} |
|
397 | 397 | end |
|
398 | 398 | |
|
399 | 399 | # Returns a Hash of columns and the key for sorting |
|
400 | 400 | def sortable_columns |
|
401 | 401 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| |
|
402 | 402 | h[column.name.to_s] = column.sortable |
|
403 | 403 | h |
|
404 | 404 | }) |
|
405 | 405 | end |
|
406 | 406 | |
|
407 | 407 | def columns |
|
408 | 408 | # preserve the column_names order |
|
409 | 409 | (has_default_columns? ? default_columns_names : column_names).collect do |name| |
|
410 | 410 | available_columns.find { |col| col.name == name } |
|
411 | 411 | end.compact |
|
412 | 412 | end |
|
413 | 413 | |
|
414 | 414 | def default_columns_names |
|
415 | 415 | @default_columns_names ||= begin |
|
416 | 416 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
417 | 417 | |
|
418 | 418 | project.present? ? default_columns : [:project] | default_columns |
|
419 | 419 | end |
|
420 | 420 | end |
|
421 | 421 | |
|
422 | 422 | def column_names=(names) |
|
423 | 423 | if names |
|
424 | 424 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
425 | 425 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } |
|
426 | 426 | # Set column_names to nil if default columns |
|
427 | 427 | if names == default_columns_names |
|
428 | 428 | names = nil |
|
429 | 429 | end |
|
430 | 430 | end |
|
431 | 431 | write_attribute(:column_names, names) |
|
432 | 432 | end |
|
433 | 433 | |
|
434 | 434 | def has_column?(column) |
|
435 | 435 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) |
|
436 | 436 | end |
|
437 | 437 | |
|
438 | 438 | def has_default_columns? |
|
439 | 439 | column_names.nil? || column_names.empty? |
|
440 | 440 | end |
|
441 | 441 | |
|
442 | 442 | def sort_criteria=(arg) |
|
443 | 443 | c = [] |
|
444 | 444 | if arg.is_a?(Hash) |
|
445 | 445 | arg = arg.keys.sort.collect {|k| arg[k]} |
|
446 | 446 | end |
|
447 | 447 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} |
|
448 | 448 | write_attribute(:sort_criteria, c) |
|
449 | 449 | end |
|
450 | 450 | |
|
451 | 451 | def sort_criteria |
|
452 | 452 | read_attribute(:sort_criteria) || [] |
|
453 | 453 | end |
|
454 | 454 | |
|
455 | 455 | def sort_criteria_key(arg) |
|
456 | 456 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
457 | 457 | end |
|
458 | 458 | |
|
459 | 459 | def sort_criteria_order(arg) |
|
460 | 460 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
461 | 461 | end |
|
462 | 462 | |
|
463 | 463 | # Returns the SQL sort order that should be prepended for grouping |
|
464 | 464 | def group_by_sort_order |
|
465 | 465 | if grouped? && (column = group_by_column) |
|
466 | 466 | column.sortable.is_a?(Array) ? |
|
467 | 467 | column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') : |
|
468 | 468 | "#{column.sortable} #{column.default_order}" |
|
469 | 469 | end |
|
470 | 470 | end |
|
471 | 471 | |
|
472 | 472 | # Returns true if the query is a grouped query |
|
473 | 473 | def grouped? |
|
474 | 474 | !group_by_column.nil? |
|
475 | 475 | end |
|
476 | 476 | |
|
477 | 477 | def group_by_column |
|
478 | 478 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
479 | 479 | end |
|
480 | 480 | |
|
481 | 481 | def group_by_statement |
|
482 | 482 | group_by_column.try(:groupable) |
|
483 | 483 | end |
|
484 | 484 | |
|
485 | 485 | def project_statement |
|
486 | 486 | project_clauses = [] |
|
487 | 487 | if project && !project.descendants.active.empty? |
|
488 | 488 | ids = [project.id] |
|
489 | 489 | if has_filter?("subproject_id") |
|
490 | 490 | case operator_for("subproject_id") |
|
491 | 491 | when '=' |
|
492 | 492 | # include the selected subprojects |
|
493 | 493 | ids += values_for("subproject_id").each(&:to_i) |
|
494 | 494 | when '!*' |
|
495 | 495 | # main project only |
|
496 | 496 | else |
|
497 | 497 | # all subprojects |
|
498 | 498 | ids += project.descendants.collect(&:id) |
|
499 | 499 | end |
|
500 | 500 | elsif Setting.display_subprojects_issues? |
|
501 | 501 | ids += project.descendants.collect(&:id) |
|
502 | 502 | end |
|
503 | 503 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') |
|
504 | 504 | elsif project |
|
505 | 505 | project_clauses << "#{Project.table_name}.id = %d" % project.id |
|
506 | 506 | end |
|
507 | 507 | project_clauses.any? ? project_clauses.join(' AND ') : nil |
|
508 | 508 | end |
|
509 | 509 | |
|
510 | 510 | def statement |
|
511 | 511 | # filters clauses |
|
512 | 512 | filters_clauses = [] |
|
513 | 513 | filters.each_key do |field| |
|
514 | 514 | next if field == "subproject_id" |
|
515 | 515 | v = values_for(field).clone |
|
516 | 516 | next unless v and !v.empty? |
|
517 | 517 | operator = operator_for(field) |
|
518 | 518 | |
|
519 | 519 | # "me" value subsitution |
|
520 | 520 | if %w(assigned_to_id author_id watcher_id).include?(field) |
|
521 | 521 | if v.delete("me") |
|
522 | 522 | if User.current.logged? |
|
523 | 523 | v.push(User.current.id.to_s) |
|
524 | 524 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' |
|
525 | 525 | else |
|
526 | 526 | v.push("0") |
|
527 | 527 | end |
|
528 | 528 | end |
|
529 | 529 | end |
|
530 | 530 | |
|
531 | 531 | if field == 'project_id' |
|
532 | 532 | if v.delete('mine') |
|
533 | 533 | v += User.current.memberships.map(&:project_id).map(&:to_s) |
|
534 | 534 | end |
|
535 | 535 | end |
|
536 | 536 | |
|
537 | 537 | if field =~ /^cf_(\d+)$/ |
|
538 | 538 | # custom field |
|
539 | 539 | filters_clauses << sql_for_custom_field(field, operator, v, $1) |
|
540 | 540 | elsif respond_to?("sql_for_#{field}_field") |
|
541 | 541 | # specific statement |
|
542 | 542 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) |
|
543 | 543 | else |
|
544 | 544 | # regular field |
|
545 | 545 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' |
|
546 | 546 | end |
|
547 | 547 | end if filters and valid? |
|
548 | 548 | |
|
549 | 549 | filters_clauses << project_statement |
|
550 | 550 | filters_clauses.reject!(&:blank?) |
|
551 | 551 | |
|
552 | 552 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
553 | 553 | end |
|
554 | 554 | |
|
555 | 555 | # Returns the issue count |
|
556 | 556 | def issue_count |
|
557 | 557 | Issue.visible.count(:include => [:status, :project], :conditions => statement) |
|
558 | 558 | rescue ::ActiveRecord::StatementInvalid => e |
|
559 | 559 | raise StatementInvalid.new(e.message) |
|
560 | 560 | end |
|
561 | 561 | |
|
562 | 562 | # Returns the issue count by group or nil if query is not grouped |
|
563 | 563 | def issue_count_by_group |
|
564 | 564 | r = nil |
|
565 | 565 | if grouped? |
|
566 | 566 | begin |
|
567 | 567 | # Rails will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
568 | 568 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) |
|
569 | 569 | rescue ActiveRecord::RecordNotFound |
|
570 | 570 | r = {nil => issue_count} |
|
571 | 571 | end |
|
572 | 572 | c = group_by_column |
|
573 | 573 | if c.is_a?(QueryCustomFieldColumn) |
|
574 | 574 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
575 | 575 | end |
|
576 | 576 | end |
|
577 | 577 | r |
|
578 | 578 | rescue ::ActiveRecord::StatementInvalid => e |
|
579 | 579 | raise StatementInvalid.new(e.message) |
|
580 | 580 | end |
|
581 | 581 | |
|
582 | 582 | # Returns the issues |
|
583 | 583 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
584 | 584 | def issues(options={}) |
|
585 | 585 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
586 | 586 | order_option = nil if order_option.blank? |
|
587 | 587 | |
|
588 | 588 | joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil |
|
589 | 589 | |
|
590 | 590 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, |
|
591 | 591 | :conditions => statement, |
|
592 | 592 | :order => order_option, |
|
593 | 593 | :joins => joins, |
|
594 | 594 | :limit => options[:limit], |
|
595 | 595 | :offset => options[:offset] |
|
596 | 596 | |
|
597 | 597 | if has_column?(:spent_hours) |
|
598 | 598 | Issue.load_visible_spent_hours(issues) |
|
599 | 599 | end |
|
600 | 600 | issues |
|
601 | 601 | rescue ::ActiveRecord::StatementInvalid => e |
|
602 | 602 | raise StatementInvalid.new(e.message) |
|
603 | 603 | end |
|
604 | 604 | |
|
605 | 605 | # Returns the issues ids |
|
606 | 606 | def issue_ids(options={}) |
|
607 | 607 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
608 | 608 | order_option = nil if order_option.blank? |
|
609 | 609 | |
|
610 | 610 | joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil |
|
611 | 611 | |
|
612 | 612 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, |
|
613 | 613 | :conditions => statement, |
|
614 | 614 | :order => order_option, |
|
615 | 615 | :joins => joins, |
|
616 | 616 | :limit => options[:limit], |
|
617 | 617 | :offset => options[:offset]).find_ids |
|
618 | 618 | rescue ::ActiveRecord::StatementInvalid => e |
|
619 | 619 | raise StatementInvalid.new(e.message) |
|
620 | 620 | end |
|
621 | 621 | |
|
622 | 622 | # Returns the journals |
|
623 | 623 | # Valid options are :order, :offset, :limit |
|
624 | 624 | def journals(options={}) |
|
625 | 625 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], |
|
626 | 626 | :conditions => statement, |
|
627 | 627 | :order => options[:order], |
|
628 | 628 | :limit => options[:limit], |
|
629 | 629 | :offset => options[:offset] |
|
630 | 630 | rescue ::ActiveRecord::StatementInvalid => e |
|
631 | 631 | raise StatementInvalid.new(e.message) |
|
632 | 632 | end |
|
633 | 633 | |
|
634 | 634 | # Returns the versions |
|
635 | 635 | # Valid options are :conditions |
|
636 | 636 | def versions(options={}) |
|
637 | 637 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement |
|
638 | 638 | rescue ::ActiveRecord::StatementInvalid => e |
|
639 | 639 | raise StatementInvalid.new(e.message) |
|
640 | 640 | end |
|
641 | 641 | |
|
642 | 642 | def sql_for_watcher_id_field(field, operator, value) |
|
643 | 643 | db_table = Watcher.table_name |
|
644 | 644 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
645 | 645 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
646 | 646 | end |
|
647 | 647 | |
|
648 | 648 | def sql_for_member_of_group_field(field, operator, value) |
|
649 | 649 | if operator == '*' # Any group |
|
650 | 650 | groups = Group.all |
|
651 | 651 | operator = '=' # Override the operator since we want to find by assigned_to |
|
652 | 652 | elsif operator == "!*" |
|
653 | 653 | groups = Group.all |
|
654 | 654 | operator = '!' # Override the operator since we want to find by assigned_to |
|
655 | 655 | else |
|
656 | 656 | groups = Group.find_all_by_id(value) |
|
657 | 657 | end |
|
658 | 658 | groups ||= [] |
|
659 | 659 | |
|
660 | 660 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
661 | 661 | if group && group.user_ids.present? |
|
662 | 662 | user_ids << group.user_ids |
|
663 | 663 | end |
|
664 | 664 | user_ids.flatten.uniq.compact |
|
665 | 665 | }.sort.collect(&:to_s) |
|
666 | 666 | |
|
667 | 667 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
668 | 668 | end |
|
669 | 669 | |
|
670 | 670 | def sql_for_assigned_to_role_field(field, operator, value) |
|
671 | 671 | case operator |
|
672 | 672 | when "*", "!*" # Member / Not member |
|
673 | 673 | sw = operator == "!*" ? 'NOT' : '' |
|
674 | 674 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
675 | 675 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
676 | 676 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
677 | 677 | when "=", "!" |
|
678 | 678 | role_cond = value.any? ? |
|
679 | 679 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
680 | 680 | "1=0" |
|
681 | 681 | |
|
682 | 682 | sw = operator == "!" ? 'NOT' : '' |
|
683 | 683 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
684 | 684 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
685 | 685 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
686 | 686 | end |
|
687 | 687 | end |
|
688 | 688 | |
|
689 | 689 | private |
|
690 | 690 | |
|
691 | 691 | def sql_for_custom_field(field, operator, value, custom_field_id) |
|
692 | 692 | db_table = CustomValue.table_name |
|
693 | 693 | db_field = 'value' |
|
694 | 694 | filter = @available_filters[field] |
|
695 | 695 | if filter && filter[:format] == 'user' |
|
696 | 696 | if value.delete('me') |
|
697 | 697 | value.push User.current.id.to_s |
|
698 | 698 | end |
|
699 | 699 | end |
|
700 | 700 | not_in = nil |
|
701 | 701 | if operator == '!' |
|
702 | 702 | # Makes ! operator work for custom fields with multiple values |
|
703 | 703 | operator = '=' |
|
704 | 704 | not_in = 'NOT' |
|
705 | 705 | end |
|
706 | 706 | "#{Issue.table_name}.id #{not_in} IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + |
|
707 | 707 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' |
|
708 | 708 | end |
|
709 | 709 | |
|
710 | 710 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
711 | 711 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
712 | 712 | sql = '' |
|
713 | 713 | case operator |
|
714 | 714 | when "=" |
|
715 | 715 | if value.any? |
|
716 | 716 | case type_for(field) |
|
717 | 717 | when :date, :date_past |
|
718 | 718 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) |
|
719 | 719 | when :integer |
|
720 | 720 | if is_custom_filter |
|
721 | 721 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" |
|
722 | 722 | else |
|
723 | 723 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" |
|
724 | 724 | end |
|
725 | 725 | when :float |
|
726 | 726 | if is_custom_filter |
|
727 | 727 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" |
|
728 | 728 | else |
|
729 | 729 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" |
|
730 | 730 | end |
|
731 | 731 | else |
|
732 | 732 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
733 | 733 | end |
|
734 | 734 | else |
|
735 | 735 | # IN an empty set |
|
736 | 736 | sql = "1=0" |
|
737 | 737 | end |
|
738 | 738 | when "!" |
|
739 | 739 | if value.any? |
|
740 | 740 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
741 | 741 | else |
|
742 | 742 | # NOT IN an empty set |
|
743 | 743 | sql = "1=1" |
|
744 | 744 | end |
|
745 | 745 | when "!*" |
|
746 | 746 | sql = "#{db_table}.#{db_field} IS NULL" |
|
747 | 747 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
748 | 748 | when "*" |
|
749 | 749 | sql = "#{db_table}.#{db_field} IS NOT NULL" |
|
750 | 750 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
751 | 751 | when ">=" |
|
752 | 752 | if [:date, :date_past].include?(type_for(field)) |
|
753 | 753 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) |
|
754 | 754 | else |
|
755 | 755 | if is_custom_filter |
|
756 | 756 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" |
|
757 | 757 | else |
|
758 | 758 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" |
|
759 | 759 | end |
|
760 | 760 | end |
|
761 | 761 | when "<=" |
|
762 | 762 | if [:date, :date_past].include?(type_for(field)) |
|
763 | 763 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) |
|
764 | 764 | else |
|
765 | 765 | if is_custom_filter |
|
766 | 766 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" |
|
767 | 767 | else |
|
768 | 768 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" |
|
769 | 769 | end |
|
770 | 770 | end |
|
771 | 771 | when "><" |
|
772 | 772 | if [:date, :date_past].include?(type_for(field)) |
|
773 | 773 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) |
|
774 | 774 | else |
|
775 | 775 | if is_custom_filter |
|
776 | 776 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" |
|
777 | 777 | else |
|
778 | 778 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" |
|
779 | 779 | end |
|
780 | 780 | end |
|
781 | 781 | when "o" |
|
782 | 782 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" |
|
783 | 783 | when "c" |
|
784 | 784 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" |
|
785 | 785 | when ">t-" |
|
786 | 786 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) |
|
787 | 787 | when "<t-" |
|
788 | 788 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) |
|
789 | 789 | when "t-" |
|
790 | 790 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) |
|
791 | 791 | when ">t+" |
|
792 | 792 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) |
|
793 | 793 | when "<t+" |
|
794 | 794 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) |
|
795 | 795 | when "t+" |
|
796 | 796 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) |
|
797 | 797 | when "t" |
|
798 | 798 | sql = relative_date_clause(db_table, db_field, 0, 0) |
|
799 | 799 | when "w" |
|
800 | 800 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
801 | 801 | day_of_week = Date.today.cwday |
|
802 | 802 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
803 | 803 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) |
|
804 | 804 | when "~" |
|
805 | 805 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
806 | 806 | when "!~" |
|
807 | 807 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
808 | 808 | else |
|
809 | 809 | raise "Unknown query operator #{operator}" |
|
810 | 810 | end |
|
811 | 811 | |
|
812 | 812 | return sql |
|
813 | 813 | end |
|
814 | 814 | |
|
815 | 815 | def add_custom_fields_filters(custom_fields) |
|
816 | 816 | @available_filters ||= {} |
|
817 | 817 | |
|
818 | 818 | custom_fields.select(&:is_filter?).each do |field| |
|
819 | 819 | case field.field_format |
|
820 | 820 | when "text" |
|
821 | 821 | options = { :type => :text, :order => 20 } |
|
822 | 822 | when "list" |
|
823 | 823 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
824 | 824 | when "date" |
|
825 | 825 | options = { :type => :date, :order => 20 } |
|
826 | 826 | when "bool" |
|
827 | 827 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
828 | 828 | when "int" |
|
829 | 829 | options = { :type => :integer, :order => 20 } |
|
830 | 830 | when "float" |
|
831 | 831 | options = { :type => :float, :order => 20 } |
|
832 | 832 | when "user", "version" |
|
833 | 833 | next unless project |
|
834 | 834 | values = field.possible_values_options(project) |
|
835 | 835 | if User.current.logged? && field.field_format == 'user' |
|
836 | 836 | values.unshift ["<< #{l(:label_me)} >>", "me"] |
|
837 | 837 | end |
|
838 | 838 | options = { :type => :list_optional, :values => values, :order => 20} |
|
839 | 839 | else |
|
840 | 840 | options = { :type => :string, :order => 20 } |
|
841 | 841 | end |
|
842 | 842 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name, :format => field.field_format }) |
|
843 | 843 | end |
|
844 | 844 | end |
|
845 | 845 | |
|
846 | 846 | # Returns a SQL clause for a date or datetime field. |
|
847 | 847 | def date_clause(table, field, from, to) |
|
848 | 848 | s = [] |
|
849 | 849 | if from |
|
850 | 850 | from_yesterday = from - 1 |
|
851 | 851 | from_yesterday_utc = Time.gm(from_yesterday.year, from_yesterday.month, from_yesterday.day) |
|
852 | 852 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_utc.end_of_day)]) |
|
853 | 853 | end |
|
854 | 854 | if to |
|
855 | 855 | to_utc = Time.gm(to.year, to.month, to.day) |
|
856 | 856 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_utc.end_of_day)]) |
|
857 | 857 | end |
|
858 | 858 | s.join(' AND ') |
|
859 | 859 | end |
|
860 | 860 | |
|
861 | 861 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
862 | 862 | def relative_date_clause(table, field, days_from, days_to) |
|
863 | 863 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) |
|
864 | 864 | end |
|
865 | 865 | end |
@@ -1,145 +1,146 | |||
|
1 | 1 | --- |
|
2 | 2 | custom_fields_001: |
|
3 | 3 | name: Database |
|
4 | 4 | min_length: 0 |
|
5 | 5 | regexp: "" |
|
6 | 6 | is_for_all: true |
|
7 | 7 | is_filter: true |
|
8 | 8 | type: IssueCustomField |
|
9 | 9 | max_length: 0 |
|
10 | 10 | possible_values: |
|
11 | 11 | - MySQL |
|
12 | 12 | - PostgreSQL |
|
13 | 13 | - Oracle |
|
14 | 14 | id: 1 |
|
15 | 15 | is_required: false |
|
16 | 16 | field_format: list |
|
17 | 17 | default_value: "" |
|
18 | 18 | editable: true |
|
19 | 19 | custom_fields_002: |
|
20 | 20 | name: Searchable field |
|
21 | 21 | min_length: 1 |
|
22 | 22 | regexp: "" |
|
23 | 23 | is_for_all: true |
|
24 | is_filter: true | |
|
24 | 25 | type: IssueCustomField |
|
25 | 26 | max_length: 100 |
|
26 | 27 | possible_values: "" |
|
27 | 28 | id: 2 |
|
28 | 29 | is_required: false |
|
29 | 30 | field_format: string |
|
30 | 31 | searchable: true |
|
31 | 32 | default_value: "Default string" |
|
32 | 33 | editable: true |
|
33 | 34 | custom_fields_003: |
|
34 | 35 | name: Development status |
|
35 | 36 | min_length: 0 |
|
36 | 37 | regexp: "" |
|
37 | 38 | is_for_all: false |
|
38 | 39 | is_filter: true |
|
39 | 40 | type: ProjectCustomField |
|
40 | 41 | max_length: 0 |
|
41 | 42 | possible_values: |
|
42 | 43 | - Stable |
|
43 | 44 | - Beta |
|
44 | 45 | - Alpha |
|
45 | 46 | - Planning |
|
46 | 47 | id: 3 |
|
47 | 48 | is_required: false |
|
48 | 49 | field_format: list |
|
49 | 50 | default_value: "" |
|
50 | 51 | editable: true |
|
51 | 52 | custom_fields_004: |
|
52 | 53 | name: Phone number |
|
53 | 54 | min_length: 0 |
|
54 | 55 | regexp: "" |
|
55 | 56 | is_for_all: false |
|
56 | 57 | type: UserCustomField |
|
57 | 58 | max_length: 0 |
|
58 | 59 | possible_values: "" |
|
59 | 60 | id: 4 |
|
60 | 61 | is_required: false |
|
61 | 62 | field_format: string |
|
62 | 63 | default_value: "" |
|
63 | 64 | editable: true |
|
64 | 65 | custom_fields_005: |
|
65 | 66 | name: Money |
|
66 | 67 | min_length: 0 |
|
67 | 68 | regexp: "" |
|
68 | 69 | is_for_all: false |
|
69 | 70 | type: UserCustomField |
|
70 | 71 | max_length: 0 |
|
71 | 72 | possible_values: "" |
|
72 | 73 | id: 5 |
|
73 | 74 | is_required: false |
|
74 | 75 | field_format: float |
|
75 | 76 | default_value: "" |
|
76 | 77 | editable: true |
|
77 | 78 | custom_fields_006: |
|
78 | 79 | name: Float field |
|
79 | 80 | min_length: 0 |
|
80 | 81 | regexp: "" |
|
81 | 82 | is_for_all: true |
|
82 | 83 | type: IssueCustomField |
|
83 | 84 | max_length: 0 |
|
84 | 85 | possible_values: "" |
|
85 | 86 | id: 6 |
|
86 | 87 | is_required: false |
|
87 | 88 | field_format: float |
|
88 | 89 | default_value: "" |
|
89 | 90 | editable: true |
|
90 | 91 | custom_fields_007: |
|
91 | 92 | name: Billable |
|
92 | 93 | min_length: 0 |
|
93 | 94 | regexp: "" |
|
94 | 95 | is_for_all: false |
|
95 | 96 | is_filter: true |
|
96 | 97 | type: TimeEntryActivityCustomField |
|
97 | 98 | max_length: 0 |
|
98 | 99 | possible_values: "" |
|
99 | 100 | id: 7 |
|
100 | 101 | is_required: false |
|
101 | 102 | field_format: bool |
|
102 | 103 | default_value: "" |
|
103 | 104 | editable: true |
|
104 | 105 | custom_fields_008: |
|
105 | 106 | name: Custom date |
|
106 | 107 | min_length: 0 |
|
107 | 108 | regexp: "" |
|
108 | 109 | is_for_all: true |
|
109 | 110 | is_filter: false |
|
110 | 111 | type: IssueCustomField |
|
111 | 112 | max_length: 0 |
|
112 | 113 | possible_values: "" |
|
113 | 114 | id: 8 |
|
114 | 115 | is_required: false |
|
115 | 116 | field_format: date |
|
116 | 117 | default_value: "" |
|
117 | 118 | editable: true |
|
118 | 119 | custom_fields_009: |
|
119 | 120 | name: Project 1 cf |
|
120 | 121 | min_length: 0 |
|
121 | 122 | regexp: "" |
|
122 | 123 | is_for_all: false |
|
123 | 124 | is_filter: true |
|
124 | 125 | type: IssueCustomField |
|
125 | 126 | max_length: 0 |
|
126 | 127 | possible_values: "" |
|
127 | 128 | id: 9 |
|
128 | 129 | is_required: false |
|
129 | 130 | field_format: date |
|
130 | 131 | default_value: "" |
|
131 | 132 | editable: true |
|
132 | 133 | custom_fields_010: |
|
133 | 134 | name: Overtime |
|
134 | 135 | min_length: 0 |
|
135 | 136 | regexp: "" |
|
136 | 137 | is_for_all: false |
|
137 | 138 | is_filter: false |
|
138 | 139 | type: TimeEntryCustomField |
|
139 | 140 | max_length: 0 |
|
140 | 141 | possible_values: "" |
|
141 | 142 | id: 10 |
|
142 | 143 | is_required: false |
|
143 | 144 | field_format: bool |
|
144 | 145 | default_value: 0 |
|
145 | 146 | editable: true |
@@ -1,973 +1,991 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class QueryTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :projects, :enabled_modules, :users, :members, |
|
22 | 22 | :member_roles, :roles, :trackers, :issue_statuses, |
|
23 | 23 | :issue_categories, :enumerations, :issues, |
|
24 | 24 | :watchers, :custom_fields, :custom_values, :versions, |
|
25 | 25 | :queries, |
|
26 | 26 | :projects_trackers |
|
27 | 27 | |
|
28 | 28 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries |
|
29 | 29 | query = Query.new(:project => nil, :name => '_') |
|
30 | 30 | assert query.available_filters.has_key?('cf_1') |
|
31 | 31 | assert !query.available_filters.has_key?('cf_3') |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_system_shared_versions_should_be_available_in_global_queries |
|
35 | 35 | Version.find(2).update_attribute :sharing, 'system' |
|
36 | 36 | query = Query.new(:project => nil, :name => '_') |
|
37 | 37 | assert query.available_filters.has_key?('fixed_version_id') |
|
38 | 38 | assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'} |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def test_project_filter_in_global_queries |
|
42 | 42 | query = Query.new(:project => nil, :name => '_') |
|
43 | 43 | project_filter = query.available_filters["project_id"] |
|
44 | 44 | assert_not_nil project_filter |
|
45 | 45 | project_ids = project_filter[:values].map{|p| p[1]} |
|
46 | 46 | assert project_ids.include?("1") #public project |
|
47 | 47 | assert !project_ids.include?("2") #private project user cannot see |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def find_issues_with_query(query) |
|
51 | 51 | Issue.find :all, |
|
52 | 52 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
53 | 53 | :conditions => query.statement |
|
54 | 54 | end |
|
55 | 55 | |
|
56 | 56 | def assert_find_issues_with_query_is_successful(query) |
|
57 | 57 | assert_nothing_raised do |
|
58 | 58 | find_issues_with_query(query) |
|
59 | 59 | end |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def assert_query_statement_includes(query, condition) |
|
63 | 63 | assert query.statement.include?(condition), "Query statement condition not found in: #{query.statement}" |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def assert_query_result(expected, query) |
|
67 | 67 | assert_nothing_raised do |
|
68 | 68 | assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort |
|
69 | 69 | assert_equal expected.size, query.issue_count |
|
70 | 70 | end |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | def test_query_should_allow_shared_versions_for_a_project_query |
|
74 | 74 | subproject_version = Version.find(4) |
|
75 | 75 | query = Query.new(:project => Project.find(1), :name => '_') |
|
76 | 76 | query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s]) |
|
77 | 77 | |
|
78 | 78 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')") |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def test_query_with_multiple_custom_fields |
|
82 | 82 | query = Query.find(1) |
|
83 | 83 | assert query.valid? |
|
84 | 84 | assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") |
|
85 | 85 | issues = find_issues_with_query(query) |
|
86 | 86 | assert_equal 1, issues.length |
|
87 | 87 | assert_equal Issue.find(3), issues.first |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | def test_operator_none |
|
91 | 91 | query = Query.new(:project => Project.find(1), :name => '_') |
|
92 | 92 | query.add_filter('fixed_version_id', '!*', ['']) |
|
93 | 93 | query.add_filter('cf_1', '!*', ['']) |
|
94 | 94 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") |
|
95 | 95 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") |
|
96 | 96 | find_issues_with_query(query) |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def test_operator_none_for_integer |
|
100 | 100 | query = Query.new(:project => Project.find(1), :name => '_') |
|
101 | 101 | query.add_filter('estimated_hours', '!*', ['']) |
|
102 | 102 | issues = find_issues_with_query(query) |
|
103 | 103 | assert !issues.empty? |
|
104 | 104 | assert issues.all? {|i| !i.estimated_hours} |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def test_operator_none_for_date |
|
108 | 108 | query = Query.new(:project => Project.find(1), :name => '_') |
|
109 | 109 | query.add_filter('start_date', '!*', ['']) |
|
110 | 110 | issues = find_issues_with_query(query) |
|
111 | 111 | assert !issues.empty? |
|
112 | 112 | assert issues.all? {|i| i.start_date.nil?} |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | def test_operator_none_for_string_custom_field | |
|
116 | query = Query.new(:project => Project.find(1), :name => '_') | |
|
117 | query.add_filter('cf_2', '!*', ['']) | |
|
118 | assert query.has_filter?('cf_2') | |
|
119 | issues = find_issues_with_query(query) | |
|
120 | assert !issues.empty? | |
|
121 | assert issues.all? {|i| i.custom_field_value(2).blank?} | |
|
122 | end | |
|
123 | ||
|
115 | 124 | def test_operator_all |
|
116 | 125 | query = Query.new(:project => Project.find(1), :name => '_') |
|
117 | 126 | query.add_filter('fixed_version_id', '*', ['']) |
|
118 | 127 | query.add_filter('cf_1', '*', ['']) |
|
119 | 128 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") |
|
120 | 129 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") |
|
121 | 130 | find_issues_with_query(query) |
|
122 | 131 | end |
|
123 | 132 | |
|
124 | 133 | def test_operator_all_for_date |
|
125 | 134 | query = Query.new(:project => Project.find(1), :name => '_') |
|
126 | 135 | query.add_filter('start_date', '*', ['']) |
|
127 | 136 | issues = find_issues_with_query(query) |
|
128 | 137 | assert !issues.empty? |
|
129 | 138 | assert issues.all? {|i| i.start_date.present?} |
|
130 | 139 | end |
|
131 | 140 | |
|
141 | def test_operator_all_for_string_custom_field | |
|
142 | query = Query.new(:project => Project.find(1), :name => '_') | |
|
143 | query.add_filter('cf_2', '*', ['']) | |
|
144 | assert query.has_filter?('cf_2') | |
|
145 | issues = find_issues_with_query(query) | |
|
146 | assert !issues.empty? | |
|
147 | assert issues.all? {|i| i.custom_field_value(2).present?} | |
|
148 | end | |
|
149 | ||
|
132 | 150 | def test_numeric_filter_should_not_accept_non_numeric_values |
|
133 | 151 | query = Query.new(:name => '_') |
|
134 | 152 | query.add_filter('estimated_hours', '=', ['a']) |
|
135 | 153 | |
|
136 | 154 | assert query.has_filter?('estimated_hours') |
|
137 | 155 | assert !query.valid? |
|
138 | 156 | end |
|
139 | 157 | |
|
140 | 158 | def test_operator_is_on_float |
|
141 | 159 | Issue.update_all("estimated_hours = 171.2", "id=2") |
|
142 | 160 | |
|
143 | 161 | query = Query.new(:name => '_') |
|
144 | 162 | query.add_filter('estimated_hours', '=', ['171.20']) |
|
145 | 163 | issues = find_issues_with_query(query) |
|
146 | 164 | assert_equal 1, issues.size |
|
147 | 165 | assert_equal 2, issues.first.id |
|
148 | 166 | end |
|
149 | 167 | |
|
150 | 168 | def test_operator_is_on_integer_custom_field |
|
151 | 169 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true) |
|
152 | 170 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
153 | 171 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
154 | 172 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
155 | 173 | |
|
156 | 174 | query = Query.new(:name => '_') |
|
157 | 175 | query.add_filter("cf_#{f.id}", '=', ['12']) |
|
158 | 176 | issues = find_issues_with_query(query) |
|
159 | 177 | assert_equal 1, issues.size |
|
160 | 178 | assert_equal 2, issues.first.id |
|
161 | 179 | end |
|
162 | 180 | |
|
163 | 181 | def test_operator_is_on_float_custom_field |
|
164 | 182 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true) |
|
165 | 183 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3') |
|
166 | 184 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7') |
|
167 | 185 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
168 | 186 | |
|
169 | 187 | query = Query.new(:name => '_') |
|
170 | 188 | query.add_filter("cf_#{f.id}", '=', ['12.7']) |
|
171 | 189 | issues = find_issues_with_query(query) |
|
172 | 190 | assert_equal 1, issues.size |
|
173 | 191 | assert_equal 2, issues.first.id |
|
174 | 192 | end |
|
175 | 193 | |
|
176 | 194 | def test_operator_is_on_multi_list_custom_field |
|
177 | 195 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
178 | 196 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true) |
|
179 | 197 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
180 | 198 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
181 | 199 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
182 | 200 | |
|
183 | 201 | query = Query.new(:name => '_') |
|
184 | 202 | query.add_filter("cf_#{f.id}", '=', ['value1']) |
|
185 | 203 | issues = find_issues_with_query(query) |
|
186 | 204 | assert_equal [1, 3], issues.map(&:id).sort |
|
187 | 205 | |
|
188 | 206 | query = Query.new(:name => '_') |
|
189 | 207 | query.add_filter("cf_#{f.id}", '=', ['value2']) |
|
190 | 208 | issues = find_issues_with_query(query) |
|
191 | 209 | assert_equal [1], issues.map(&:id).sort |
|
192 | 210 | end |
|
193 | 211 | |
|
194 | 212 | def test_operator_is_not_on_multi_list_custom_field |
|
195 | 213 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
196 | 214 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true) |
|
197 | 215 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
198 | 216 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
199 | 217 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
200 | 218 | |
|
201 | 219 | query = Query.new(:name => '_') |
|
202 | 220 | query.add_filter("cf_#{f.id}", '!', ['value1']) |
|
203 | 221 | issues = find_issues_with_query(query) |
|
204 | 222 | assert !issues.map(&:id).include?(1) |
|
205 | 223 | assert !issues.map(&:id).include?(3) |
|
206 | 224 | |
|
207 | 225 | query = Query.new(:name => '_') |
|
208 | 226 | query.add_filter("cf_#{f.id}", '!', ['value2']) |
|
209 | 227 | issues = find_issues_with_query(query) |
|
210 | 228 | assert !issues.map(&:id).include?(1) |
|
211 | 229 | assert issues.map(&:id).include?(3) |
|
212 | 230 | end |
|
213 | 231 | |
|
214 | 232 | def test_operator_greater_than |
|
215 | 233 | query = Query.new(:project => Project.find(1), :name => '_') |
|
216 | 234 | query.add_filter('done_ratio', '>=', ['40']) |
|
217 | 235 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0") |
|
218 | 236 | find_issues_with_query(query) |
|
219 | 237 | end |
|
220 | 238 | |
|
221 | 239 | def test_operator_greater_than_a_float |
|
222 | 240 | query = Query.new(:project => Project.find(1), :name => '_') |
|
223 | 241 | query.add_filter('estimated_hours', '>=', ['40.5']) |
|
224 | 242 | assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5") |
|
225 | 243 | find_issues_with_query(query) |
|
226 | 244 | end |
|
227 | 245 | |
|
228 | 246 | def test_operator_greater_than_on_int_custom_field |
|
229 | 247 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
230 | 248 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
231 | 249 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
232 | 250 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
233 | 251 | |
|
234 | 252 | query = Query.new(:project => Project.find(1), :name => '_') |
|
235 | 253 | query.add_filter("cf_#{f.id}", '>=', ['8']) |
|
236 | 254 | issues = find_issues_with_query(query) |
|
237 | 255 | assert_equal 1, issues.size |
|
238 | 256 | assert_equal 2, issues.first.id |
|
239 | 257 | end |
|
240 | 258 | |
|
241 | 259 | def test_operator_lesser_than |
|
242 | 260 | query = Query.new(:project => Project.find(1), :name => '_') |
|
243 | 261 | query.add_filter('done_ratio', '<=', ['30']) |
|
244 | 262 | assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0") |
|
245 | 263 | find_issues_with_query(query) |
|
246 | 264 | end |
|
247 | 265 | |
|
248 | 266 | def test_operator_lesser_than_on_custom_field |
|
249 | 267 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
250 | 268 | query = Query.new(:project => Project.find(1), :name => '_') |
|
251 | 269 | query.add_filter("cf_#{f.id}", '<=', ['30']) |
|
252 | 270 | assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) <= 30.0") |
|
253 | 271 | find_issues_with_query(query) |
|
254 | 272 | end |
|
255 | 273 | |
|
256 | 274 | def test_operator_between |
|
257 | 275 | query = Query.new(:project => Project.find(1), :name => '_') |
|
258 | 276 | query.add_filter('done_ratio', '><', ['30', '40']) |
|
259 | 277 | assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement |
|
260 | 278 | find_issues_with_query(query) |
|
261 | 279 | end |
|
262 | 280 | |
|
263 | 281 | def test_operator_between_on_custom_field |
|
264 | 282 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
265 | 283 | query = Query.new(:project => Project.find(1), :name => '_') |
|
266 | 284 | query.add_filter("cf_#{f.id}", '><', ['30', '40']) |
|
267 | 285 | assert_include "CAST(custom_values.value AS decimal(60,3)) BETWEEN 30.0 AND 40.0", query.statement |
|
268 | 286 | find_issues_with_query(query) |
|
269 | 287 | end |
|
270 | 288 | |
|
271 | 289 | def test_date_filter_should_not_accept_non_date_values |
|
272 | 290 | query = Query.new(:name => '_') |
|
273 | 291 | query.add_filter('created_on', '=', ['a']) |
|
274 | 292 | |
|
275 | 293 | assert query.has_filter?('created_on') |
|
276 | 294 | assert !query.valid? |
|
277 | 295 | end |
|
278 | 296 | |
|
279 | 297 | def test_date_filter_should_not_accept_invalid_date_values |
|
280 | 298 | query = Query.new(:name => '_') |
|
281 | 299 | query.add_filter('created_on', '=', ['2011-01-34']) |
|
282 | 300 | |
|
283 | 301 | assert query.has_filter?('created_on') |
|
284 | 302 | assert !query.valid? |
|
285 | 303 | end |
|
286 | 304 | |
|
287 | 305 | def test_relative_date_filter_should_not_accept_non_integer_values |
|
288 | 306 | query = Query.new(:name => '_') |
|
289 | 307 | query.add_filter('created_on', '>t-', ['a']) |
|
290 | 308 | |
|
291 | 309 | assert query.has_filter?('created_on') |
|
292 | 310 | assert !query.valid? |
|
293 | 311 | end |
|
294 | 312 | |
|
295 | 313 | def test_operator_date_equals |
|
296 | 314 | query = Query.new(:name => '_') |
|
297 | 315 | query.add_filter('due_date', '=', ['2011-07-10']) |
|
298 | 316 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
299 | 317 | find_issues_with_query(query) |
|
300 | 318 | end |
|
301 | 319 | |
|
302 | 320 | def test_operator_date_lesser_than |
|
303 | 321 | query = Query.new(:name => '_') |
|
304 | 322 | query.add_filter('due_date', '<=', ['2011-07-10']) |
|
305 | 323 | assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
306 | 324 | find_issues_with_query(query) |
|
307 | 325 | end |
|
308 | 326 | |
|
309 | 327 | def test_operator_date_greater_than |
|
310 | 328 | query = Query.new(:name => '_') |
|
311 | 329 | query.add_filter('due_date', '>=', ['2011-07-10']) |
|
312 | 330 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement |
|
313 | 331 | find_issues_with_query(query) |
|
314 | 332 | end |
|
315 | 333 | |
|
316 | 334 | def test_operator_date_between |
|
317 | 335 | query = Query.new(:name => '_') |
|
318 | 336 | query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10']) |
|
319 | 337 | assert_match /issues\.due_date > '2011-06-22 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
320 | 338 | find_issues_with_query(query) |
|
321 | 339 | end |
|
322 | 340 | |
|
323 | 341 | def test_operator_in_more_than |
|
324 | 342 | Issue.find(7).update_attribute(:due_date, (Date.today + 15)) |
|
325 | 343 | query = Query.new(:project => Project.find(1), :name => '_') |
|
326 | 344 | query.add_filter('due_date', '>t+', ['15']) |
|
327 | 345 | issues = find_issues_with_query(query) |
|
328 | 346 | assert !issues.empty? |
|
329 | 347 | issues.each {|issue| assert(issue.due_date >= (Date.today + 15))} |
|
330 | 348 | end |
|
331 | 349 | |
|
332 | 350 | def test_operator_in_less_than |
|
333 | 351 | query = Query.new(:project => Project.find(1), :name => '_') |
|
334 | 352 | query.add_filter('due_date', '<t+', ['15']) |
|
335 | 353 | issues = find_issues_with_query(query) |
|
336 | 354 | assert !issues.empty? |
|
337 | 355 | issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} |
|
338 | 356 | end |
|
339 | 357 | |
|
340 | 358 | def test_operator_less_than_ago |
|
341 | 359 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
342 | 360 | query = Query.new(:project => Project.find(1), :name => '_') |
|
343 | 361 | query.add_filter('due_date', '>t-', ['3']) |
|
344 | 362 | issues = find_issues_with_query(query) |
|
345 | 363 | assert !issues.empty? |
|
346 | 364 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} |
|
347 | 365 | end |
|
348 | 366 | |
|
349 | 367 | def test_operator_more_than_ago |
|
350 | 368 | Issue.find(7).update_attribute(:due_date, (Date.today - 10)) |
|
351 | 369 | query = Query.new(:project => Project.find(1), :name => '_') |
|
352 | 370 | query.add_filter('due_date', '<t-', ['10']) |
|
353 | 371 | assert query.statement.include?("#{Issue.table_name}.due_date <=") |
|
354 | 372 | issues = find_issues_with_query(query) |
|
355 | 373 | assert !issues.empty? |
|
356 | 374 | issues.each {|issue| assert(issue.due_date <= (Date.today - 10))} |
|
357 | 375 | end |
|
358 | 376 | |
|
359 | 377 | def test_operator_in |
|
360 | 378 | Issue.find(7).update_attribute(:due_date, (Date.today + 2)) |
|
361 | 379 | query = Query.new(:project => Project.find(1), :name => '_') |
|
362 | 380 | query.add_filter('due_date', 't+', ['2']) |
|
363 | 381 | issues = find_issues_with_query(query) |
|
364 | 382 | assert !issues.empty? |
|
365 | 383 | issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)} |
|
366 | 384 | end |
|
367 | 385 | |
|
368 | 386 | def test_operator_ago |
|
369 | 387 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
370 | 388 | query = Query.new(:project => Project.find(1), :name => '_') |
|
371 | 389 | query.add_filter('due_date', 't-', ['3']) |
|
372 | 390 | issues = find_issues_with_query(query) |
|
373 | 391 | assert !issues.empty? |
|
374 | 392 | issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)} |
|
375 | 393 | end |
|
376 | 394 | |
|
377 | 395 | def test_operator_today |
|
378 | 396 | query = Query.new(:project => Project.find(1), :name => '_') |
|
379 | 397 | query.add_filter('due_date', 't', ['']) |
|
380 | 398 | issues = find_issues_with_query(query) |
|
381 | 399 | assert !issues.empty? |
|
382 | 400 | issues.each {|issue| assert_equal Date.today, issue.due_date} |
|
383 | 401 | end |
|
384 | 402 | |
|
385 | 403 | def test_operator_this_week_on_date |
|
386 | 404 | query = Query.new(:project => Project.find(1), :name => '_') |
|
387 | 405 | query.add_filter('due_date', 'w', ['']) |
|
388 | 406 | find_issues_with_query(query) |
|
389 | 407 | end |
|
390 | 408 | |
|
391 | 409 | def test_operator_this_week_on_datetime |
|
392 | 410 | query = Query.new(:project => Project.find(1), :name => '_') |
|
393 | 411 | query.add_filter('created_on', 'w', ['']) |
|
394 | 412 | find_issues_with_query(query) |
|
395 | 413 | end |
|
396 | 414 | |
|
397 | 415 | def test_operator_contains |
|
398 | 416 | query = Query.new(:project => Project.find(1), :name => '_') |
|
399 | 417 | query.add_filter('subject', '~', ['uNable']) |
|
400 | 418 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'") |
|
401 | 419 | result = find_issues_with_query(query) |
|
402 | 420 | assert result.empty? |
|
403 | 421 | result.each {|issue| assert issue.subject.downcase.include?('unable') } |
|
404 | 422 | end |
|
405 | 423 | |
|
406 | 424 | def test_range_for_this_week_with_week_starting_on_monday |
|
407 | 425 | I18n.locale = :fr |
|
408 | 426 | assert_equal '1', I18n.t(:general_first_day_of_week) |
|
409 | 427 | |
|
410 | 428 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
411 | 429 | |
|
412 | 430 | query = Query.new(:project => Project.find(1), :name => '_') |
|
413 | 431 | query.add_filter('due_date', 'w', ['']) |
|
414 | 432 | assert query.statement.match(/issues\.due_date > '2011-04-24 23:59:59(\.9+)?' AND issues\.due_date <= '2011-05-01 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
415 | 433 | I18n.locale = :en |
|
416 | 434 | end |
|
417 | 435 | |
|
418 | 436 | def test_range_for_this_week_with_week_starting_on_sunday |
|
419 | 437 | I18n.locale = :en |
|
420 | 438 | assert_equal '7', I18n.t(:general_first_day_of_week) |
|
421 | 439 | |
|
422 | 440 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
423 | 441 | |
|
424 | 442 | query = Query.new(:project => Project.find(1), :name => '_') |
|
425 | 443 | query.add_filter('due_date', 'w', ['']) |
|
426 | 444 | assert query.statement.match(/issues\.due_date > '2011-04-23 23:59:59(\.9+)?' AND issues\.due_date <= '2011-04-30 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
427 | 445 | end |
|
428 | 446 | |
|
429 | 447 | def test_operator_does_not_contains |
|
430 | 448 | query = Query.new(:project => Project.find(1), :name => '_') |
|
431 | 449 | query.add_filter('subject', '!~', ['uNable']) |
|
432 | 450 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'") |
|
433 | 451 | find_issues_with_query(query) |
|
434 | 452 | end |
|
435 | 453 | |
|
436 | 454 | def test_filter_assigned_to_me |
|
437 | 455 | user = User.find(2) |
|
438 | 456 | group = Group.find(10) |
|
439 | 457 | User.current = user |
|
440 | 458 | i1 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => user) |
|
441 | 459 | i2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => group) |
|
442 | 460 | i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11)) |
|
443 | 461 | group.users << user |
|
444 | 462 | |
|
445 | 463 | query = Query.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}}) |
|
446 | 464 | result = query.issues |
|
447 | 465 | assert_equal Issue.visible.all(:conditions => {:assigned_to_id => ([2] + user.reload.group_ids)}).sort_by(&:id), result.sort_by(&:id) |
|
448 | 466 | |
|
449 | 467 | assert result.include?(i1) |
|
450 | 468 | assert result.include?(i2) |
|
451 | 469 | assert !result.include?(i3) |
|
452 | 470 | end |
|
453 | 471 | |
|
454 | 472 | def test_user_custom_field_filtered_on_me |
|
455 | 473 | User.current = User.find(2) |
|
456 | 474 | cf = IssueCustomField.create!(:field_format => 'user', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1]) |
|
457 | 475 | issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'}, :subject => 'Test', :author_id => 1) |
|
458 | 476 | issue2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '3'}) |
|
459 | 477 | |
|
460 | 478 | query = Query.new(:name => '_', :project => Project.find(1)) |
|
461 | 479 | filter = query.available_filters["cf_#{cf.id}"] |
|
462 | 480 | assert_not_nil filter |
|
463 | 481 | assert_include 'me', filter[:values].map{|v| v[1]} |
|
464 | 482 | |
|
465 | 483 | query.filters = { "cf_#{cf.id}" => {:operator => '=', :values => ['me']}} |
|
466 | 484 | result = query.issues |
|
467 | 485 | assert_equal 1, result.size |
|
468 | 486 | assert_equal issue1, result.first |
|
469 | 487 | end |
|
470 | 488 | |
|
471 | 489 | def test_filter_my_projects |
|
472 | 490 | User.current = User.find(2) |
|
473 | 491 | query = Query.new(:name => '_') |
|
474 | 492 | filter = query.available_filters['project_id'] |
|
475 | 493 | assert_not_nil filter |
|
476 | 494 | assert_include 'mine', filter[:values].map{|v| v[1]} |
|
477 | 495 | |
|
478 | 496 | query.filters = { 'project_id' => {:operator => '=', :values => ['mine']}} |
|
479 | 497 | result = query.issues |
|
480 | 498 | assert_nil result.detect {|issue| !User.current.member_of?(issue.project)} |
|
481 | 499 | end |
|
482 | 500 | |
|
483 | 501 | def test_filter_watched_issues |
|
484 | 502 | User.current = User.find(1) |
|
485 | 503 | query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) |
|
486 | 504 | result = find_issues_with_query(query) |
|
487 | 505 | assert_not_nil result |
|
488 | 506 | assert !result.empty? |
|
489 | 507 | assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id) |
|
490 | 508 | User.current = nil |
|
491 | 509 | end |
|
492 | 510 | |
|
493 | 511 | def test_filter_unwatched_issues |
|
494 | 512 | User.current = User.find(1) |
|
495 | 513 | query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}}) |
|
496 | 514 | result = find_issues_with_query(query) |
|
497 | 515 | assert_not_nil result |
|
498 | 516 | assert !result.empty? |
|
499 | 517 | assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size) |
|
500 | 518 | User.current = nil |
|
501 | 519 | end |
|
502 | 520 | |
|
503 | 521 | def test_statement_should_be_nil_with_no_filters |
|
504 | 522 | q = Query.new(:name => '_') |
|
505 | 523 | q.filters = {} |
|
506 | 524 | |
|
507 | 525 | assert q.valid? |
|
508 | 526 | assert_nil q.statement |
|
509 | 527 | end |
|
510 | 528 | |
|
511 | 529 | def test_default_columns |
|
512 | 530 | q = Query.new |
|
513 | 531 | assert !q.columns.empty? |
|
514 | 532 | end |
|
515 | 533 | |
|
516 | 534 | def test_set_column_names |
|
517 | 535 | q = Query.new |
|
518 | 536 | q.column_names = ['tracker', :subject, '', 'unknonw_column'] |
|
519 | 537 | assert_equal [:tracker, :subject], q.columns.collect {|c| c.name} |
|
520 | 538 | c = q.columns.first |
|
521 | 539 | assert q.has_column?(c) |
|
522 | 540 | end |
|
523 | 541 | |
|
524 | 542 | def test_query_should_preload_spent_hours |
|
525 | 543 | q = Query.new(:name => '_', :column_names => [:subject, :spent_hours]) |
|
526 | 544 | assert q.has_column?(:spent_hours) |
|
527 | 545 | issues = q.issues |
|
528 | 546 | assert_not_nil issues.first.instance_variable_get("@spent_hours") |
|
529 | 547 | end |
|
530 | 548 | |
|
531 | 549 | def test_groupable_columns_should_include_custom_fields |
|
532 | 550 | q = Query.new |
|
533 | 551 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
534 | 552 | assert_not_nil column |
|
535 | 553 | assert_kind_of QueryCustomFieldColumn, column |
|
536 | 554 | end |
|
537 | 555 | |
|
538 | 556 | def test_groupable_columns_should_not_include_multi_custom_fields |
|
539 | 557 | field = CustomField.find(1) |
|
540 | 558 | field.update_attribute :multiple, true |
|
541 | 559 | |
|
542 | 560 | q = Query.new |
|
543 | 561 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
544 | 562 | assert_nil column |
|
545 | 563 | end |
|
546 | 564 | |
|
547 | 565 | def test_grouped_with_valid_column |
|
548 | 566 | q = Query.new(:group_by => 'status') |
|
549 | 567 | assert q.grouped? |
|
550 | 568 | assert_not_nil q.group_by_column |
|
551 | 569 | assert_equal :status, q.group_by_column.name |
|
552 | 570 | assert_not_nil q.group_by_statement |
|
553 | 571 | assert_equal 'status', q.group_by_statement |
|
554 | 572 | end |
|
555 | 573 | |
|
556 | 574 | def test_grouped_with_invalid_column |
|
557 | 575 | q = Query.new(:group_by => 'foo') |
|
558 | 576 | assert !q.grouped? |
|
559 | 577 | assert_nil q.group_by_column |
|
560 | 578 | assert_nil q.group_by_statement |
|
561 | 579 | end |
|
562 | 580 | |
|
563 | 581 | def test_sortable_columns_should_sort_assignees_according_to_user_format_setting |
|
564 | 582 | with_settings :user_format => 'lastname_coma_firstname' do |
|
565 | 583 | q = Query.new |
|
566 | 584 | assert q.sortable_columns.has_key?('assigned_to') |
|
567 | 585 | assert_equal %w(users.lastname users.firstname users.id), q.sortable_columns['assigned_to'] |
|
568 | 586 | end |
|
569 | 587 | end |
|
570 | 588 | |
|
571 | 589 | def test_sortable_columns_should_sort_authors_according_to_user_format_setting |
|
572 | 590 | with_settings :user_format => 'lastname_coma_firstname' do |
|
573 | 591 | q = Query.new |
|
574 | 592 | assert q.sortable_columns.has_key?('author') |
|
575 | 593 | assert_equal %w(authors.lastname authors.firstname authors.id), q.sortable_columns['author'] |
|
576 | 594 | end |
|
577 | 595 | end |
|
578 | 596 | |
|
579 | 597 | def test_sortable_columns_should_include_custom_field |
|
580 | 598 | q = Query.new |
|
581 | 599 | assert q.sortable_columns['cf_1'] |
|
582 | 600 | end |
|
583 | 601 | |
|
584 | 602 | def test_sortable_columns_should_not_include_multi_custom_field |
|
585 | 603 | field = CustomField.find(1) |
|
586 | 604 | field.update_attribute :multiple, true |
|
587 | 605 | |
|
588 | 606 | q = Query.new |
|
589 | 607 | assert !q.sortable_columns['cf_1'] |
|
590 | 608 | end |
|
591 | 609 | |
|
592 | 610 | def test_default_sort |
|
593 | 611 | q = Query.new |
|
594 | 612 | assert_equal [], q.sort_criteria |
|
595 | 613 | end |
|
596 | 614 | |
|
597 | 615 | def test_set_sort_criteria_with_hash |
|
598 | 616 | q = Query.new |
|
599 | 617 | q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']} |
|
600 | 618 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
601 | 619 | end |
|
602 | 620 | |
|
603 | 621 | def test_set_sort_criteria_with_array |
|
604 | 622 | q = Query.new |
|
605 | 623 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
606 | 624 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
607 | 625 | end |
|
608 | 626 | |
|
609 | 627 | def test_create_query_with_sort |
|
610 | 628 | q = Query.new(:name => 'Sorted') |
|
611 | 629 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
612 | 630 | assert q.save |
|
613 | 631 | q.reload |
|
614 | 632 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
615 | 633 | end |
|
616 | 634 | |
|
617 | 635 | def test_sort_by_string_custom_field_asc |
|
618 | 636 | q = Query.new |
|
619 | 637 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
620 | 638 | assert c |
|
621 | 639 | assert c.sortable |
|
622 | 640 | issues = Issue.find :all, |
|
623 | 641 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
624 | 642 | :conditions => q.statement, |
|
625 | 643 | :order => "#{c.sortable} ASC" |
|
626 | 644 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
627 | 645 | assert !values.empty? |
|
628 | 646 | assert_equal values.sort, values |
|
629 | 647 | end |
|
630 | 648 | |
|
631 | 649 | def test_sort_by_string_custom_field_desc |
|
632 | 650 | q = Query.new |
|
633 | 651 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
634 | 652 | assert c |
|
635 | 653 | assert c.sortable |
|
636 | 654 | issues = Issue.find :all, |
|
637 | 655 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
638 | 656 | :conditions => q.statement, |
|
639 | 657 | :order => "#{c.sortable} DESC" |
|
640 | 658 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
641 | 659 | assert !values.empty? |
|
642 | 660 | assert_equal values.sort.reverse, values |
|
643 | 661 | end |
|
644 | 662 | |
|
645 | 663 | def test_sort_by_float_custom_field_asc |
|
646 | 664 | q = Query.new |
|
647 | 665 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' } |
|
648 | 666 | assert c |
|
649 | 667 | assert c.sortable |
|
650 | 668 | issues = Issue.find :all, |
|
651 | 669 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
652 | 670 | :conditions => q.statement, |
|
653 | 671 | :order => "#{c.sortable} ASC" |
|
654 | 672 | values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact |
|
655 | 673 | assert !values.empty? |
|
656 | 674 | assert_equal values.sort, values |
|
657 | 675 | end |
|
658 | 676 | |
|
659 | 677 | def test_invalid_query_should_raise_query_statement_invalid_error |
|
660 | 678 | q = Query.new |
|
661 | 679 | assert_raise Query::StatementInvalid do |
|
662 | 680 | q.issues(:conditions => "foo = 1") |
|
663 | 681 | end |
|
664 | 682 | end |
|
665 | 683 | |
|
666 | 684 | def test_issue_count |
|
667 | 685 | q = Query.new(:name => '_') |
|
668 | 686 | issue_count = q.issue_count |
|
669 | 687 | assert_equal q.issues.size, issue_count |
|
670 | 688 | end |
|
671 | 689 | |
|
672 | 690 | def test_issue_count_with_archived_issues |
|
673 | 691 | p = Project.generate!( :status => Project::STATUS_ARCHIVED ) |
|
674 | 692 | i = Issue.generate!( :project => p, :tracker => p.trackers.first ) |
|
675 | 693 | assert !i.visible? |
|
676 | 694 | |
|
677 | 695 | test_issue_count |
|
678 | 696 | end |
|
679 | 697 | |
|
680 | 698 | def test_issue_count_by_association_group |
|
681 | 699 | q = Query.new(:name => '_', :group_by => 'assigned_to') |
|
682 | 700 | count_by_group = q.issue_count_by_group |
|
683 | 701 | assert_kind_of Hash, count_by_group |
|
684 | 702 | assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
685 | 703 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
686 | 704 | assert count_by_group.has_key?(User.find(3)) |
|
687 | 705 | end |
|
688 | 706 | |
|
689 | 707 | def test_issue_count_by_list_custom_field_group |
|
690 | 708 | q = Query.new(:name => '_', :group_by => 'cf_1') |
|
691 | 709 | count_by_group = q.issue_count_by_group |
|
692 | 710 | assert_kind_of Hash, count_by_group |
|
693 | 711 | assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
694 | 712 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
695 | 713 | assert count_by_group.has_key?('MySQL') |
|
696 | 714 | end |
|
697 | 715 | |
|
698 | 716 | def test_issue_count_by_date_custom_field_group |
|
699 | 717 | q = Query.new(:name => '_', :group_by => 'cf_8') |
|
700 | 718 | count_by_group = q.issue_count_by_group |
|
701 | 719 | assert_kind_of Hash, count_by_group |
|
702 | 720 | assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
703 | 721 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
704 | 722 | end |
|
705 | 723 | |
|
706 | 724 | def test_issue_ids |
|
707 | 725 | q = Query.new(:name => '_') |
|
708 | 726 | order = "issues.subject, issues.id" |
|
709 | 727 | issues = q.issues(:order => order) |
|
710 | 728 | assert_equal issues.map(&:id), q.issue_ids(:order => order) |
|
711 | 729 | end |
|
712 | 730 | |
|
713 | 731 | def test_label_for |
|
714 | 732 | q = Query.new |
|
715 | 733 | assert_equal 'Assignee', q.label_for('assigned_to_id') |
|
716 | 734 | end |
|
717 | 735 | |
|
718 | 736 | def test_editable_by |
|
719 | 737 | admin = User.find(1) |
|
720 | 738 | manager = User.find(2) |
|
721 | 739 | developer = User.find(3) |
|
722 | 740 | |
|
723 | 741 | # Public query on project 1 |
|
724 | 742 | q = Query.find(1) |
|
725 | 743 | assert q.editable_by?(admin) |
|
726 | 744 | assert q.editable_by?(manager) |
|
727 | 745 | assert !q.editable_by?(developer) |
|
728 | 746 | |
|
729 | 747 | # Private query on project 1 |
|
730 | 748 | q = Query.find(2) |
|
731 | 749 | assert q.editable_by?(admin) |
|
732 | 750 | assert !q.editable_by?(manager) |
|
733 | 751 | assert q.editable_by?(developer) |
|
734 | 752 | |
|
735 | 753 | # Private query for all projects |
|
736 | 754 | q = Query.find(3) |
|
737 | 755 | assert q.editable_by?(admin) |
|
738 | 756 | assert !q.editable_by?(manager) |
|
739 | 757 | assert q.editable_by?(developer) |
|
740 | 758 | |
|
741 | 759 | # Public query for all projects |
|
742 | 760 | q = Query.find(4) |
|
743 | 761 | assert q.editable_by?(admin) |
|
744 | 762 | assert !q.editable_by?(manager) |
|
745 | 763 | assert !q.editable_by?(developer) |
|
746 | 764 | end |
|
747 | 765 | |
|
748 | 766 | def test_visible_scope |
|
749 | 767 | query_ids = Query.visible(User.anonymous).map(&:id) |
|
750 | 768 | |
|
751 | 769 | assert query_ids.include?(1), 'public query on public project was not visible' |
|
752 | 770 | assert query_ids.include?(4), 'public query for all projects was not visible' |
|
753 | 771 | assert !query_ids.include?(2), 'private query on public project was visible' |
|
754 | 772 | assert !query_ids.include?(3), 'private query for all projects was visible' |
|
755 | 773 | assert !query_ids.include?(7), 'public query on private project was visible' |
|
756 | 774 | end |
|
757 | 775 | |
|
758 | 776 | context "#available_filters" do |
|
759 | 777 | setup do |
|
760 | 778 | @query = Query.new(:name => "_") |
|
761 | 779 | end |
|
762 | 780 | |
|
763 | 781 | should "include users of visible projects in cross-project view" do |
|
764 | 782 | users = @query.available_filters["assigned_to_id"] |
|
765 | 783 | assert_not_nil users |
|
766 | 784 | assert users[:values].map{|u|u[1]}.include?("3") |
|
767 | 785 | end |
|
768 | 786 | |
|
769 | 787 | should "include users of subprojects" do |
|
770 | 788 | user1 = User.generate_with_protected! |
|
771 | 789 | user2 = User.generate_with_protected! |
|
772 | 790 | project = Project.find(1) |
|
773 | 791 | Member.create!(:principal => user1, :project => project.children.visible.first, :role_ids => [1]) |
|
774 | 792 | @query.project = project |
|
775 | 793 | |
|
776 | 794 | users = @query.available_filters["assigned_to_id"] |
|
777 | 795 | assert_not_nil users |
|
778 | 796 | assert users[:values].map{|u|u[1]}.include?(user1.id.to_s) |
|
779 | 797 | assert !users[:values].map{|u|u[1]}.include?(user2.id.to_s) |
|
780 | 798 | end |
|
781 | 799 | |
|
782 | 800 | should "include visible projects in cross-project view" do |
|
783 | 801 | projects = @query.available_filters["project_id"] |
|
784 | 802 | assert_not_nil projects |
|
785 | 803 | assert projects[:values].map{|u|u[1]}.include?("1") |
|
786 | 804 | end |
|
787 | 805 | |
|
788 | 806 | context "'member_of_group' filter" do |
|
789 | 807 | should "be present" do |
|
790 | 808 | assert @query.available_filters.keys.include?("member_of_group") |
|
791 | 809 | end |
|
792 | 810 | |
|
793 | 811 | should "be an optional list" do |
|
794 | 812 | assert_equal :list_optional, @query.available_filters["member_of_group"][:type] |
|
795 | 813 | end |
|
796 | 814 | |
|
797 | 815 | should "have a list of the groups as values" do |
|
798 | 816 | Group.destroy_all # No fixtures |
|
799 | 817 | group1 = Group.generate!.reload |
|
800 | 818 | group2 = Group.generate!.reload |
|
801 | 819 | |
|
802 | 820 | expected_group_list = [ |
|
803 | 821 | [group1.name, group1.id.to_s], |
|
804 | 822 | [group2.name, group2.id.to_s] |
|
805 | 823 | ] |
|
806 | 824 | assert_equal expected_group_list.sort, @query.available_filters["member_of_group"][:values].sort |
|
807 | 825 | end |
|
808 | 826 | |
|
809 | 827 | end |
|
810 | 828 | |
|
811 | 829 | context "'assigned_to_role' filter" do |
|
812 | 830 | should "be present" do |
|
813 | 831 | assert @query.available_filters.keys.include?("assigned_to_role") |
|
814 | 832 | end |
|
815 | 833 | |
|
816 | 834 | should "be an optional list" do |
|
817 | 835 | assert_equal :list_optional, @query.available_filters["assigned_to_role"][:type] |
|
818 | 836 | end |
|
819 | 837 | |
|
820 | 838 | should "have a list of the Roles as values" do |
|
821 | 839 | assert @query.available_filters["assigned_to_role"][:values].include?(['Manager','1']) |
|
822 | 840 | assert @query.available_filters["assigned_to_role"][:values].include?(['Developer','2']) |
|
823 | 841 | assert @query.available_filters["assigned_to_role"][:values].include?(['Reporter','3']) |
|
824 | 842 | end |
|
825 | 843 | |
|
826 | 844 | should "not include the built in Roles as values" do |
|
827 | 845 | assert ! @query.available_filters["assigned_to_role"][:values].include?(['Non member','4']) |
|
828 | 846 | assert ! @query.available_filters["assigned_to_role"][:values].include?(['Anonymous','5']) |
|
829 | 847 | end |
|
830 | 848 | |
|
831 | 849 | end |
|
832 | 850 | |
|
833 | 851 | end |
|
834 | 852 | |
|
835 | 853 | context "#statement" do |
|
836 | 854 | context "with 'member_of_group' filter" do |
|
837 | 855 | setup do |
|
838 | 856 | Group.destroy_all # No fixtures |
|
839 | 857 | @user_in_group = User.generate! |
|
840 | 858 | @second_user_in_group = User.generate! |
|
841 | 859 | @user_in_group2 = User.generate! |
|
842 | 860 | @user_not_in_group = User.generate! |
|
843 | 861 | |
|
844 | 862 | @group = Group.generate!.reload |
|
845 | 863 | @group.users << @user_in_group |
|
846 | 864 | @group.users << @second_user_in_group |
|
847 | 865 | |
|
848 | 866 | @group2 = Group.generate!.reload |
|
849 | 867 | @group2.users << @user_in_group2 |
|
850 | 868 | |
|
851 | 869 | end |
|
852 | 870 | |
|
853 | 871 | should "search assigned to for users in the group" do |
|
854 | 872 | @query = Query.new(:name => '_') |
|
855 | 873 | @query.add_filter('member_of_group', '=', [@group.id.to_s]) |
|
856 | 874 | |
|
857 | 875 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}')" |
|
858 | 876 | assert_find_issues_with_query_is_successful @query |
|
859 | 877 | end |
|
860 | 878 | |
|
861 | 879 | should "search not assigned to any group member (none)" do |
|
862 | 880 | @query = Query.new(:name => '_') |
|
863 | 881 | @query.add_filter('member_of_group', '!*', ['']) |
|
864 | 882 | |
|
865 | 883 | # Users not in a group |
|
866 | 884 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}')" |
|
867 | 885 | assert_find_issues_with_query_is_successful @query |
|
868 | 886 | end |
|
869 | 887 | |
|
870 | 888 | should "search assigned to any group member (all)" do |
|
871 | 889 | @query = Query.new(:name => '_') |
|
872 | 890 | @query.add_filter('member_of_group', '*', ['']) |
|
873 | 891 | |
|
874 | 892 | # Only users in a group |
|
875 | 893 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}')" |
|
876 | 894 | assert_find_issues_with_query_is_successful @query |
|
877 | 895 | end |
|
878 | 896 | |
|
879 | 897 | should "return an empty set with = empty group" do |
|
880 | 898 | @empty_group = Group.generate! |
|
881 | 899 | @query = Query.new(:name => '_') |
|
882 | 900 | @query.add_filter('member_of_group', '=', [@empty_group.id.to_s]) |
|
883 | 901 | |
|
884 | 902 | assert_equal [], find_issues_with_query(@query) |
|
885 | 903 | end |
|
886 | 904 | |
|
887 | 905 | should "return issues with ! empty group" do |
|
888 | 906 | @empty_group = Group.generate! |
|
889 | 907 | @query = Query.new(:name => '_') |
|
890 | 908 | @query.add_filter('member_of_group', '!', [@empty_group.id.to_s]) |
|
891 | 909 | |
|
892 | 910 | assert_find_issues_with_query_is_successful @query |
|
893 | 911 | end |
|
894 | 912 | end |
|
895 | 913 | |
|
896 | 914 | context "with 'assigned_to_role' filter" do |
|
897 | 915 | setup do |
|
898 | 916 | @manager_role = Role.find_by_name('Manager') |
|
899 | 917 | @developer_role = Role.find_by_name('Developer') |
|
900 | 918 | |
|
901 | 919 | @project = Project.generate! |
|
902 | 920 | @manager = User.generate! |
|
903 | 921 | @developer = User.generate! |
|
904 | 922 | @boss = User.generate! |
|
905 | 923 | @guest = User.generate! |
|
906 | 924 | User.add_to_project(@manager, @project, @manager_role) |
|
907 | 925 | User.add_to_project(@developer, @project, @developer_role) |
|
908 | 926 | User.add_to_project(@boss, @project, [@manager_role, @developer_role]) |
|
909 | 927 | |
|
910 | 928 | @issue1 = Issue.generate_for_project!(@project, :assigned_to_id => @manager.id) |
|
911 | 929 | @issue2 = Issue.generate_for_project!(@project, :assigned_to_id => @developer.id) |
|
912 | 930 | @issue3 = Issue.generate_for_project!(@project, :assigned_to_id => @boss.id) |
|
913 | 931 | @issue4 = Issue.generate_for_project!(@project, :assigned_to_id => @guest.id) |
|
914 | 932 | @issue5 = Issue.generate_for_project!(@project) |
|
915 | 933 | end |
|
916 | 934 | |
|
917 | 935 | should "search assigned to for users with the Role" do |
|
918 | 936 | @query = Query.new(:name => '_', :project => @project) |
|
919 | 937 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
920 | 938 | |
|
921 | 939 | assert_query_result [@issue1, @issue3], @query |
|
922 | 940 | end |
|
923 | 941 | |
|
924 | 942 | should "search assigned to for users with the Role on the issue project" do |
|
925 | 943 | other_project = Project.generate! |
|
926 | 944 | User.add_to_project(@developer, other_project, @manager_role) |
|
927 | 945 | |
|
928 | 946 | @query = Query.new(:name => '_', :project => @project) |
|
929 | 947 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
930 | 948 | |
|
931 | 949 | assert_query_result [@issue1, @issue3], @query |
|
932 | 950 | end |
|
933 | 951 | |
|
934 | 952 | should "return an empty set with empty role" do |
|
935 | 953 | @empty_role = Role.generate! |
|
936 | 954 | @query = Query.new(:name => '_', :project => @project) |
|
937 | 955 | @query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s]) |
|
938 | 956 | |
|
939 | 957 | assert_query_result [], @query |
|
940 | 958 | end |
|
941 | 959 | |
|
942 | 960 | should "search assigned to for users without the Role" do |
|
943 | 961 | @query = Query.new(:name => '_', :project => @project) |
|
944 | 962 | @query.add_filter('assigned_to_role', '!', [@manager_role.id.to_s]) |
|
945 | 963 | |
|
946 | 964 | assert_query_result [@issue2, @issue4, @issue5], @query |
|
947 | 965 | end |
|
948 | 966 | |
|
949 | 967 | should "search assigned to for users not assigned to any Role (none)" do |
|
950 | 968 | @query = Query.new(:name => '_', :project => @project) |
|
951 | 969 | @query.add_filter('assigned_to_role', '!*', ['']) |
|
952 | 970 | |
|
953 | 971 | assert_query_result [@issue4, @issue5], @query |
|
954 | 972 | end |
|
955 | 973 | |
|
956 | 974 | should "search assigned to for users assigned to any Role (all)" do |
|
957 | 975 | @query = Query.new(:name => '_', :project => @project) |
|
958 | 976 | @query.add_filter('assigned_to_role', '*', ['']) |
|
959 | 977 | |
|
960 | 978 | assert_query_result [@issue1, @issue2, @issue3], @query |
|
961 | 979 | end |
|
962 | 980 | |
|
963 | 981 | should "return issues with ! empty role" do |
|
964 | 982 | @empty_role = Role.generate! |
|
965 | 983 | @query = Query.new(:name => '_', :project => @project) |
|
966 | 984 | @query.add_filter('assigned_to_role', '!', [@empty_role.id.to_s]) |
|
967 | 985 | |
|
968 | 986 | assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query |
|
969 | 987 | end |
|
970 | 988 | end |
|
971 | 989 | end |
|
972 | 990 | |
|
973 | 991 | end |
General Comments 0
You need to be logged in to leave comments.
Login now