##// END OF EJS Templates
Refactor: makes issue id a regular QueryColumn....
Jean-Philippe Lang -
r11217:9b1ebd6808d2
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,393 +1,393
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module IssuesHelper
21 21 include ApplicationHelper
22 22
23 23 def issue_list(issues, &block)
24 24 ancestors = []
25 25 issues.each do |issue|
26 26 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
27 27 ancestors.pop
28 28 end
29 29 yield issue, ancestors.size
30 30 ancestors << issue unless issue.leaf?
31 31 end
32 32 end
33 33
34 34 # Renders a HTML/CSS tooltip
35 35 #
36 36 # To use, a trigger div is needed. This is a div with the class of "tooltip"
37 37 # that contains this method wrapped in a span with the class of "tip"
38 38 #
39 39 # <div class="tooltip"><%= link_to_issue(issue) %>
40 40 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
41 41 # </div>
42 42 #
43 43 def render_issue_tooltip(issue)
44 44 @cached_label_status ||= l(:field_status)
45 45 @cached_label_start_date ||= l(:field_start_date)
46 46 @cached_label_due_date ||= l(:field_due_date)
47 47 @cached_label_assigned_to ||= l(:field_assigned_to)
48 48 @cached_label_priority ||= l(:field_priority)
49 49 @cached_label_project ||= l(:field_project)
50 50
51 51 link_to_issue(issue) + "<br /><br />".html_safe +
52 52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
53 53 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
54 54 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
55 55 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
56 56 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
57 57 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
58 58 end
59 59
60 60 def issue_heading(issue)
61 61 h("#{issue.tracker} ##{issue.id}")
62 62 end
63 63
64 64 def render_issue_subject_with_tree(issue)
65 65 s = ''
66 66 ancestors = issue.root? ? [] : issue.ancestors.visible.all
67 67 ancestors.each do |ancestor|
68 68 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
69 69 end
70 70 s << '<div>'
71 71 subject = h(issue.subject)
72 72 if issue.is_private?
73 73 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
74 74 end
75 75 s << content_tag('h3', subject)
76 76 s << '</div>' * (ancestors.size + 1)
77 77 s.html_safe
78 78 end
79 79
80 80 def render_descendants_tree(issue)
81 81 s = '<form><table class="list issues">'
82 82 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
83 83 css = "issue issue-#{child.id} hascontextmenu"
84 84 css << " idnt idnt-#{level}" if level > 0
85 85 s << content_tag('tr',
86 86 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
87 87 content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
88 88 content_tag('td', h(child.status)) +
89 89 content_tag('td', link_to_user(child.assigned_to)) +
90 90 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
91 91 :class => css)
92 92 end
93 93 s << '</table></form>'
94 94 s.html_safe
95 95 end
96 96
97 97 # Returns a link for adding a new subtask to the given issue
98 98 def link_to_new_subtask(issue)
99 99 attrs = {
100 100 :tracker_id => issue.tracker,
101 101 :parent_issue_id => issue
102 102 }
103 103 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
104 104 end
105 105
106 106 class IssueFieldsRows
107 107 include ActionView::Helpers::TagHelper
108 108
109 109 def initialize
110 110 @left = []
111 111 @right = []
112 112 end
113 113
114 114 def left(*args)
115 115 args.any? ? @left << cells(*args) : @left
116 116 end
117 117
118 118 def right(*args)
119 119 args.any? ? @right << cells(*args) : @right
120 120 end
121 121
122 122 def size
123 123 @left.size > @right.size ? @left.size : @right.size
124 124 end
125 125
126 126 def to_html
127 127 html = ''.html_safe
128 128 blank = content_tag('th', '') + content_tag('td', '')
129 129 size.times do |i|
130 130 left = @left[i] || blank
131 131 right = @right[i] || blank
132 132 html << content_tag('tr', left + right)
133 133 end
134 134 html
135 135 end
136 136
137 137 def cells(label, text, options={})
138 138 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
139 139 end
140 140 end
141 141
142 142 def issue_fields_rows
143 143 r = IssueFieldsRows.new
144 144 yield r
145 145 r.to_html
146 146 end
147 147
148 148 def render_custom_fields_rows(issue)
149 149 return if issue.custom_field_values.empty?
150 150 ordered_values = []
151 151 half = (issue.custom_field_values.size / 2.0).ceil
152 152 half.times do |i|
153 153 ordered_values << issue.custom_field_values[i]
154 154 ordered_values << issue.custom_field_values[i + half]
155 155 end
156 156 s = "<tr>\n"
157 157 n = 0
158 158 ordered_values.compact.each do |value|
159 159 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
160 160 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
161 161 n += 1
162 162 end
163 163 s << "</tr>\n"
164 164 s.html_safe
165 165 end
166 166
167 167 def issues_destroy_confirmation_message(issues)
168 168 issues = [issues] unless issues.is_a?(Array)
169 169 message = l(:text_issues_destroy_confirmation)
170 170 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
171 171 if descendant_count > 0
172 172 issues.each do |issue|
173 173 next if issue.root?
174 174 issues.each do |other_issue|
175 175 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
176 176 end
177 177 end
178 178 if descendant_count > 0
179 179 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
180 180 end
181 181 end
182 182 message
183 183 end
184 184
185 185 def sidebar_queries
186 186 unless @sidebar_queries
187 187 @sidebar_queries = IssueQuery.visible.all(
188 188 :order => "#{Query.table_name}.name ASC",
189 189 # Project specific queries and global queries
190 190 :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
191 191 )
192 192 end
193 193 @sidebar_queries
194 194 end
195 195
196 196 def query_links(title, queries)
197 197 # links to #index on issues/show
198 198 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
199 199
200 200 content_tag('h3', h(title)) +
201 201 queries.collect {|query|
202 202 css = 'query'
203 203 css << ' selected' if query == @query
204 204 link_to(h(query.name), url_params.merge(:query_id => query), :class => css)
205 205 }.join('<br />').html_safe
206 206 end
207 207
208 208 def render_sidebar_queries
209 209 out = ''.html_safe
210 210 queries = sidebar_queries.select {|q| !q.is_public?}
211 211 out << query_links(l(:label_my_queries), queries) if queries.any?
212 212 queries = sidebar_queries.select {|q| q.is_public?}
213 213 out << query_links(l(:label_query_plural), queries) if queries.any?
214 214 out
215 215 end
216 216
217 217 # Returns the textual representation of a journal details
218 218 # as an array of strings
219 219 def details_to_strings(details, no_html=false, options={})
220 220 options[:only_path] = (options[:only_path] == false ? false : true)
221 221 strings = []
222 222 values_by_field = {}
223 223 details.each do |detail|
224 224 if detail.property == 'cf'
225 225 field_id = detail.prop_key
226 226 field = CustomField.find_by_id(field_id)
227 227 if field && field.multiple?
228 228 values_by_field[field_id] ||= {:added => [], :deleted => []}
229 229 if detail.old_value
230 230 values_by_field[field_id][:deleted] << detail.old_value
231 231 end
232 232 if detail.value
233 233 values_by_field[field_id][:added] << detail.value
234 234 end
235 235 next
236 236 end
237 237 end
238 238 strings << show_detail(detail, no_html, options)
239 239 end
240 240 values_by_field.each do |field_id, changes|
241 241 detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
242 242 if changes[:added].any?
243 243 detail.value = changes[:added]
244 244 strings << show_detail(detail, no_html, options)
245 245 elsif changes[:deleted].any?
246 246 detail.old_value = changes[:deleted]
247 247 strings << show_detail(detail, no_html, options)
248 248 end
249 249 end
250 250 strings
251 251 end
252 252
253 253 # Returns the textual representation of a single journal detail
254 254 def show_detail(detail, no_html=false, options={})
255 255 multiple = false
256 256 case detail.property
257 257 when 'attr'
258 258 field = detail.prop_key.to_s.gsub(/\_id$/, "")
259 259 label = l(("field_" + field).to_sym)
260 260 case detail.prop_key
261 261 when 'due_date', 'start_date'
262 262 value = format_date(detail.value.to_date) if detail.value
263 263 old_value = format_date(detail.old_value.to_date) if detail.old_value
264 264
265 265 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
266 266 'priority_id', 'category_id', 'fixed_version_id'
267 267 value = find_name_by_reflection(field, detail.value)
268 268 old_value = find_name_by_reflection(field, detail.old_value)
269 269
270 270 when 'estimated_hours'
271 271 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
272 272 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
273 273
274 274 when 'parent_id'
275 275 label = l(:field_parent_issue)
276 276 value = "##{detail.value}" unless detail.value.blank?
277 277 old_value = "##{detail.old_value}" unless detail.old_value.blank?
278 278
279 279 when 'is_private'
280 280 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
281 281 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
282 282 end
283 283 when 'cf'
284 284 custom_field = CustomField.find_by_id(detail.prop_key)
285 285 if custom_field
286 286 multiple = custom_field.multiple?
287 287 label = custom_field.name
288 288 value = format_value(detail.value, custom_field.field_format) if detail.value
289 289 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
290 290 end
291 291 when 'attachment'
292 292 label = l(:label_attachment)
293 293 end
294 294 call_hook(:helper_issues_show_detail_after_setting,
295 295 {:detail => detail, :label => label, :value => value, :old_value => old_value })
296 296
297 297 label ||= detail.prop_key
298 298 value ||= detail.value
299 299 old_value ||= detail.old_value
300 300
301 301 unless no_html
302 302 label = content_tag('strong', label)
303 303 old_value = content_tag("i", h(old_value)) if detail.old_value
304 304 old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank?
305 305 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
306 306 # Link to the attachment if it has not been removed
307 307 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
308 308 if options[:only_path] != false && atta.is_text?
309 309 value += link_to(
310 310 image_tag('magnifier.png'),
311 311 :controller => 'attachments', :action => 'show',
312 312 :id => atta, :filename => atta.filename
313 313 )
314 314 end
315 315 else
316 316 value = content_tag("i", h(value)) if value
317 317 end
318 318 end
319 319
320 320 if detail.property == 'attr' && detail.prop_key == 'description'
321 321 s = l(:text_journal_changed_no_detail, :label => label)
322 322 unless no_html
323 323 diff_link = link_to 'diff',
324 324 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
325 325 :detail_id => detail.id, :only_path => options[:only_path]},
326 326 :title => l(:label_view_diff)
327 327 s << " (#{ diff_link })"
328 328 end
329 329 s.html_safe
330 330 elsif detail.value.present?
331 331 case detail.property
332 332 when 'attr', 'cf'
333 333 if detail.old_value.present?
334 334 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
335 335 elsif multiple
336 336 l(:text_journal_added, :label => label, :value => value).html_safe
337 337 else
338 338 l(:text_journal_set_to, :label => label, :value => value).html_safe
339 339 end
340 340 when 'attachment'
341 341 l(:text_journal_added, :label => label, :value => value).html_safe
342 342 end
343 343 else
344 344 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
345 345 end
346 346 end
347 347
348 348 # Find the name of an associated record stored in the field attribute
349 349 def find_name_by_reflection(field, id)
350 350 unless id.present?
351 351 return nil
352 352 end
353 353 association = Issue.reflect_on_association(field.to_sym)
354 354 if association
355 355 record = association.class_name.constantize.find_by_id(id)
356 356 return record.name if record
357 357 end
358 358 end
359 359
360 360 # Renders issue children recursively
361 361 def render_api_issue_children(issue, api)
362 362 return if issue.leaf?
363 363 api.array :children do
364 364 issue.children.each do |child|
365 365 api.issue(:id => child.id) do
366 366 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
367 367 api.subject child.subject
368 368 render_api_issue_children(child, api)
369 369 end
370 370 end
371 371 end
372 372 end
373 373
374 374 def issues_to_csv(issues, project, query, options={})
375 375 encoding = l(:general_csv_encoding)
376 376 columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
377 377 if options[:description]
378 378 if description = query.available_columns.detect {|q| q.name == :description}
379 379 columns << description
380 380 end
381 381 end
382 382
383 383 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
384 384 # csv header fields
385 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
385 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
386 386 # csv lines
387 387 issues.each do |issue|
388 csv << [ issue.id.to_s ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, issue), encoding) }
388 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, issue), encoding) }
389 389 end
390 390 end
391 391 export
392 392 end
393 393 end
@@ -1,164 +1,166
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module QueriesHelper
21 21 def filters_options_for_select(query)
22 22 options_for_select(filters_options(query))
23 23 end
24 24
25 25 def filters_options(query)
26 26 options = [[]]
27 27 options += query.available_filters.map do |field, field_options|
28 28 [field_options[:name], field]
29 29 end
30 30 end
31 31
32 32 def available_block_columns_tags(query)
33 33 tags = ''.html_safe
34 34 query.available_block_columns.each do |column|
35 35 tags << content_tag('label', check_box_tag('c[]', column.name.to_s, query.has_column?(column)) + " #{column.caption}", :class => 'inline')
36 36 end
37 37 tags
38 38 end
39 39
40 40 def column_header(column)
41 41 column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption,
42 42 :default_order => column.default_order) :
43 43 content_tag('th', h(column.caption))
44 44 end
45 45
46 46 def column_content(column, issue)
47 47 value = column.value(issue)
48 48 if value.is_a?(Array)
49 49 value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe
50 50 else
51 51 column_value(column, issue, value)
52 52 end
53 53 end
54 54
55 55 def column_value(column, issue, value)
56 56 case value.class.name
57 57 when 'String'
58 58 if column.name == :subject
59 59 link_to(h(value), :controller => 'issues', :action => 'show', :id => issue)
60 60 elsif column.name == :description
61 61 issue.description? ? content_tag('div', textilizable(issue, :description), :class => "wiki") : ''
62 62 else
63 63 h(value)
64 64 end
65 65 when 'Time'
66 66 format_time(value)
67 67 when 'Date'
68 68 format_date(value)
69 69 when 'Fixnum'
70 if column.name == :done_ratio
70 if column.name == :id
71 link_to value, issue_path(issue)
72 elsif column.name == :done_ratio
71 73 progress_bar(value, :width => '80px')
72 74 else
73 75 value.to_s
74 76 end
75 77 when 'Float'
76 78 sprintf "%.2f", value
77 79 when 'User'
78 80 link_to_user value
79 81 when 'Project'
80 82 link_to_project value
81 83 when 'Version'
82 84 link_to(h(value), :controller => 'versions', :action => 'show', :id => value)
83 85 when 'TrueClass'
84 86 l(:general_text_Yes)
85 87 when 'FalseClass'
86 88 l(:general_text_No)
87 89 when 'Issue'
88 90 value.visible? ? link_to_issue(value) : "##{value.id}"
89 91 when 'IssueRelation'
90 92 other = value.other_issue(issue)
91 93 content_tag('span',
92 94 (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe,
93 95 :class => value.css_classes_for(issue))
94 96 else
95 97 h(value)
96 98 end
97 99 end
98 100
99 101 def csv_content(column, issue)
100 102 value = column.value(issue)
101 103 if value.is_a?(Array)
102 104 value.collect {|v| csv_value(column, issue, v)}.compact.join(', ')
103 105 else
104 106 csv_value(column, issue, value)
105 107 end
106 108 end
107 109
108 110 def csv_value(column, issue, value)
109 111 case value.class.name
110 112 when 'Time'
111 113 format_time(value)
112 114 when 'Date'
113 115 format_date(value)
114 116 when 'Float'
115 117 sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator))
116 118 when 'IssueRelation'
117 119 other = value.other_issue(issue)
118 120 l(value.label_for(issue)) + " ##{other.id}"
119 121 else
120 122 value.to_s
121 123 end
122 124 end
123 125
124 126 # Retrieve query from session or build a new query
125 127 def retrieve_query
126 128 if !params[:query_id].blank?
127 129 cond = "project_id IS NULL"
128 130 cond << " OR project_id = #{@project.id}" if @project
129 131 @query = IssueQuery.find(params[:query_id], :conditions => cond)
130 132 raise ::Unauthorized unless @query.visible?
131 133 @query.project = @project
132 134 session[:query] = {:id => @query.id, :project_id => @query.project_id}
133 135 sort_clear
134 136 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
135 137 # Give it a name, required to be valid
136 138 @query = IssueQuery.new(:name => "_")
137 139 @query.project = @project
138 140 @query.build_from_params(params)
139 141 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
140 142 else
141 143 # retrieve from session
142 144 @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id]
143 145 @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
144 146 @query.project = @project
145 147 end
146 148 end
147 149
148 150 def retrieve_query_from_session
149 151 if session[:query]
150 152 if session[:query][:id]
151 153 @query = IssueQuery.find_by_id(session[:query][:id])
152 154 return unless @query
153 155 else
154 156 @query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
155 157 end
156 158 if session[:query].has_key?(:project_id)
157 159 @query.project_id = session[:query][:project_id]
158 160 else
159 161 @query.project = @project
160 162 end
161 163 @query
162 164 end
163 165 end
164 166 end
@@ -1,408 +1,405
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 IssueQuery < Query
19 19
20 20 self.queried_class = Issue
21 21
22 22 self.available_columns = [
23 QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true),
23 24 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
24 25 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
25 26 QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
26 27 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
27 28 QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
28 29 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
29 30 QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
30 31 QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
31 32 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
32 33 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
33 34 QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
34 35 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
35 36 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
36 37 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
37 38 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
38 39 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
39 40 QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'),
40 41 QueryColumn.new(:relations, :caption => :label_related_issues),
41 42 QueryColumn.new(:description, :inline => false)
42 43 ]
43 44
44 45 scope :visible, lambda {|*args|
45 46 user = args.shift || User.current
46 47 base = Project.allowed_to_condition(user, :view_issues, *args)
47 48 user_id = user.logged? ? user.id : 0
48 49
49 50 includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id)
50 51 }
51 52
52 53 def initialize(attributes=nil, *args)
53 54 super attributes
54 55 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
55 56 end
56 57
57 58 # Returns true if the query is visible to +user+ or the current user.
58 59 def visible?(user=User.current)
59 60 (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id)
60 61 end
61 62
62 63 def initialize_available_filters
63 64 principals = []
64 65 subprojects = []
65 66 versions = []
66 67 categories = []
67 68 issue_custom_fields = []
68 69
69 70 if project
70 71 principals += project.principals.sort
71 72 unless project.leaf?
72 73 subprojects = project.descendants.visible.all
73 74 principals += Principal.member_of(subprojects)
74 75 end
75 76 versions = project.shared_versions.all
76 77 categories = project.issue_categories.all
77 78 issue_custom_fields = project.all_issue_custom_fields
78 79 else
79 80 if all_projects.any?
80 81 principals += Principal.member_of(all_projects)
81 82 end
82 83 versions = Version.visible.find_all_by_sharing('system')
83 84 issue_custom_fields = IssueCustomField.where(:is_filter => true, :is_for_all => true).all
84 85 end
85 86 principals.uniq!
86 87 principals.sort!
87 88 users = principals.select {|p| p.is_a?(User)}
88 89
89 90
90 91 add_available_filter "status_id",
91 92 :type => :list_status, :values => IssueStatus.sorted.all.collect{|s| [s.name, s.id.to_s] }
92 93
93 94 if project.nil?
94 95 project_values = []
95 96 if User.current.logged? && User.current.memberships.any?
96 97 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
97 98 end
98 99 project_values += all_projects_values
99 100 add_available_filter("project_id",
100 101 :type => :list, :values => project_values
101 102 ) unless project_values.empty?
102 103 end
103 104
104 105 add_available_filter "tracker_id",
105 106 :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] }
106 107 add_available_filter "priority_id",
107 108 :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
108 109
109 110 author_values = []
110 111 author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
111 112 author_values += users.collect{|s| [s.name, s.id.to_s] }
112 113 add_available_filter("author_id",
113 114 :type => :list, :values => author_values
114 115 ) unless author_values.empty?
115 116
116 117 assigned_to_values = []
117 118 assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
118 119 assigned_to_values += (Setting.issue_group_assignment? ?
119 120 principals : users).collect{|s| [s.name, s.id.to_s] }
120 121 add_available_filter("assigned_to_id",
121 122 :type => :list_optional, :values => assigned_to_values
122 123 ) unless assigned_to_values.empty?
123 124
124 125 group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
125 126 add_available_filter("member_of_group",
126 127 :type => :list_optional, :values => group_values
127 128 ) unless group_values.empty?
128 129
129 130 role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
130 131 add_available_filter("assigned_to_role",
131 132 :type => :list_optional, :values => role_values
132 133 ) unless role_values.empty?
133 134
134 135 if versions.any?
135 136 add_available_filter "fixed_version_id",
136 137 :type => :list_optional,
137 138 :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] }
138 139 end
139 140
140 141 if categories.any?
141 142 add_available_filter "category_id",
142 143 :type => :list_optional,
143 144 :values => categories.collect{|s| [s.name, s.id.to_s] }
144 145 end
145 146
146 147 add_available_filter "subject", :type => :text
147 148 add_available_filter "created_on", :type => :date_past
148 149 add_available_filter "updated_on", :type => :date_past
149 150 add_available_filter "closed_on", :type => :date_past
150 151 add_available_filter "start_date", :type => :date
151 152 add_available_filter "due_date", :type => :date
152 153 add_available_filter "estimated_hours", :type => :float
153 154 add_available_filter "done_ratio", :type => :integer
154 155
155 156 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
156 157 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
157 158 add_available_filter "is_private",
158 159 :type => :list,
159 160 :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
160 161 end
161 162
162 163 if User.current.logged?
163 164 add_available_filter "watcher_id",
164 165 :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]]
165 166 end
166 167
167 168 if subprojects.any?
168 169 add_available_filter "subproject_id",
169 170 :type => :list_subprojects,
170 171 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
171 172 end
172 173
173 174 add_custom_fields_filters(issue_custom_fields)
174 175
175 176 add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
176 177
177 178 IssueRelation::TYPES.each do |relation_type, options|
178 179 add_available_filter relation_type, :type => :relation, :label => options[:name]
179 180 end
180 181
181 182 Tracker.disabled_core_fields(trackers).each {|field|
182 183 delete_available_filter field
183 184 }
184 185 end
185 186
186 187 def available_columns
187 188 return @available_columns if @available_columns
188 189 @available_columns = self.class.available_columns.dup
189 190 @available_columns += (project ?
190 191 project.all_issue_custom_fields :
191 192 IssueCustomField.all
192 193 ).collect {|cf| QueryCustomFieldColumn.new(cf) }
193 194
194 195 if User.current.allowed_to?(:view_time_entries, project, :global => true)
195 196 index = nil
196 197 @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
197 198 index = (index ? index + 1 : -1)
198 199 # insert the column after estimated_hours or at the end
199 200 @available_columns.insert index, QueryColumn.new(:spent_hours,
200 201 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)",
201 202 :default_order => 'desc',
202 203 :caption => :label_spent_time
203 204 )
204 205 end
205 206
206 207 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
207 208 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
208 209 @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
209 210 end
210 211
211 212 disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
212 213 @available_columns.reject! {|column|
213 214 disabled_fields.include?(column.name.to_s)
214 215 }
215 216
216 217 @available_columns
217 218 end
218 219
219 def sortable_columns
220 {'id' => "#{Issue.table_name}.id"}.merge(super)
221 end
222
223 220 def default_columns_names
224 221 @default_columns_names ||= begin
225 222 default_columns = Setting.issue_list_default_columns.map(&:to_sym)
226 223
227 224 project.present? ? default_columns : [:project] | default_columns
228 225 end
229 226 end
230 227
231 228 # Returns the issue count
232 229 def issue_count
233 230 Issue.visible.count(:include => [:status, :project], :conditions => statement)
234 231 rescue ::ActiveRecord::StatementInvalid => e
235 232 raise StatementInvalid.new(e.message)
236 233 end
237 234
238 235 # Returns the issue count by group or nil if query is not grouped
239 236 def issue_count_by_group
240 237 r = nil
241 238 if grouped?
242 239 begin
243 240 # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value
244 241 r = Issue.visible.count(:joins => joins_for_order_statement(group_by_statement), :group => group_by_statement, :include => [:status, :project], :conditions => statement)
245 242 rescue ActiveRecord::RecordNotFound
246 243 r = {nil => issue_count}
247 244 end
248 245 c = group_by_column
249 246 if c.is_a?(QueryCustomFieldColumn)
250 247 r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
251 248 end
252 249 end
253 250 r
254 251 rescue ::ActiveRecord::StatementInvalid => e
255 252 raise StatementInvalid.new(e.message)
256 253 end
257 254
258 255 # Returns the issues
259 256 # Valid options are :order, :offset, :limit, :include, :conditions
260 257 def issues(options={})
261 258 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
262 259
263 260 issues = Issue.visible.where(options[:conditions]).all(
264 261 :include => ([:status, :project] + (options[:include] || [])).uniq,
265 262 :conditions => statement,
266 263 :order => order_option,
267 264 :joins => joins_for_order_statement(order_option.join(',')),
268 265 :limit => options[:limit],
269 266 :offset => options[:offset]
270 267 )
271 268
272 269 if has_column?(:spent_hours)
273 270 Issue.load_visible_spent_hours(issues)
274 271 end
275 272 if has_column?(:relations)
276 273 Issue.load_visible_relations(issues)
277 274 end
278 275 issues
279 276 rescue ::ActiveRecord::StatementInvalid => e
280 277 raise StatementInvalid.new(e.message)
281 278 end
282 279
283 280 # Returns the issues ids
284 281 def issue_ids(options={})
285 282 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
286 283
287 284 Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq,
288 285 :conditions => statement,
289 286 :order => order_option,
290 287 :joins => joins_for_order_statement(order_option.join(',')),
291 288 :limit => options[:limit],
292 289 :offset => options[:offset]).find_ids
293 290 rescue ::ActiveRecord::StatementInvalid => e
294 291 raise StatementInvalid.new(e.message)
295 292 end
296 293
297 294 # Returns the journals
298 295 # Valid options are :order, :offset, :limit
299 296 def journals(options={})
300 297 Journal.visible.all(
301 298 :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}],
302 299 :conditions => statement,
303 300 :order => options[:order],
304 301 :limit => options[:limit],
305 302 :offset => options[:offset]
306 303 )
307 304 rescue ::ActiveRecord::StatementInvalid => e
308 305 raise StatementInvalid.new(e.message)
309 306 end
310 307
311 308 # Returns the versions
312 309 # Valid options are :conditions
313 310 def versions(options={})
314 311 Version.visible.where(options[:conditions]).all(
315 312 :include => :project,
316 313 :conditions => project_statement
317 314 )
318 315 rescue ::ActiveRecord::StatementInvalid => e
319 316 raise StatementInvalid.new(e.message)
320 317 end
321 318
322 319 def sql_for_watcher_id_field(field, operator, value)
323 320 db_table = Watcher.table_name
324 321 "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
325 322 sql_for_field(field, '=', value, db_table, 'user_id') + ')'
326 323 end
327 324
328 325 def sql_for_member_of_group_field(field, operator, value)
329 326 if operator == '*' # Any group
330 327 groups = Group.all
331 328 operator = '=' # Override the operator since we want to find by assigned_to
332 329 elsif operator == "!*"
333 330 groups = Group.all
334 331 operator = '!' # Override the operator since we want to find by assigned_to
335 332 else
336 333 groups = Group.find_all_by_id(value)
337 334 end
338 335 groups ||= []
339 336
340 337 members_of_groups = groups.inject([]) {|user_ids, group|
341 338 user_ids + group.user_ids + [group.id]
342 339 }.uniq.compact.sort.collect(&:to_s)
343 340
344 341 '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
345 342 end
346 343
347 344 def sql_for_assigned_to_role_field(field, operator, value)
348 345 case operator
349 346 when "*", "!*" # Member / Not member
350 347 sw = operator == "!*" ? 'NOT' : ''
351 348 nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
352 349 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
353 350 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
354 351 when "=", "!"
355 352 role_cond = value.any? ?
356 353 "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" :
357 354 "1=0"
358 355
359 356 sw = operator == "!" ? 'NOT' : ''
360 357 nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
361 358 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
362 359 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
363 360 end
364 361 end
365 362
366 363 def sql_for_is_private_field(field, operator, value)
367 364 op = (operator == "=" ? 'IN' : 'NOT IN')
368 365 va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',')
369 366
370 367 "#{Issue.table_name}.is_private #{op} (#{va})"
371 368 end
372 369
373 370 def sql_for_relations(field, operator, value, options={})
374 371 relation_options = IssueRelation::TYPES[field]
375 372 return relation_options unless relation_options
376 373
377 374 relation_type = field
378 375 join_column, target_join_column = "issue_from_id", "issue_to_id"
379 376 if relation_options[:reverse] || options[:reverse]
380 377 relation_type = relation_options[:reverse] || relation_type
381 378 join_column, target_join_column = target_join_column, join_column
382 379 end
383 380
384 381 sql = case operator
385 382 when "*", "!*"
386 383 op = (operator == "*" ? 'IN' : 'NOT IN')
387 384 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')"
388 385 when "=", "!"
389 386 op = (operator == "=" ? 'IN' : 'NOT IN')
390 387 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})"
391 388 when "=p", "=!p", "!p"
392 389 op = (operator == "!p" ? 'NOT IN' : 'IN')
393 390 comp = (operator == "=!p" ? '<>' : '=')
394 391 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})"
395 392 end
396 393
397 394 if relation_options[:sym] == field && !options[:reverse]
398 395 sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)]
399 396 sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ")
400 397 else
401 398 sql
402 399 end
403 400 end
404 401
405 402 IssueRelation::TYPES.keys.each do |relation_type|
406 403 alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations
407 404 end
408 405 end
@@ -1,822 +1,828
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 @inline = options.key?(:inline) ? options[:inline] : true
31 @caption_key = options[:caption] || "field_#{name}"
31 @caption_key = options[:caption] || "field_#{name}".to_sym
32 @frozen = options[:frozen]
32 33 end
33 34
34 35 def caption
35 l(@caption_key)
36 @caption_key.is_a?(Symbol) ? l(@caption_key) : @caption_key
36 37 end
37 38
38 39 # Returns true if the column is sortable, otherwise false
39 40 def sortable?
40 41 !@sortable.nil?
41 42 end
42 43
43 44 def sortable
44 45 @sortable.is_a?(Proc) ? @sortable.call : @sortable
45 46 end
46 47
47 48 def inline?
48 49 @inline
49 50 end
50 51
52 def frozen?
53 @frozen
54 end
55
51 56 def value(object)
52 57 object.send name
53 58 end
54 59
55 60 def css_classes
56 61 name
57 62 end
58 63 end
59 64
60 65 class QueryCustomFieldColumn < QueryColumn
61 66
62 67 def initialize(custom_field)
63 68 self.name = "cf_#{custom_field.id}".to_sym
64 69 self.sortable = custom_field.order_statement || false
65 70 self.groupable = custom_field.group_statement || false
66 71 @inline = true
67 72 @cf = custom_field
68 73 end
69 74
70 75 def caption
71 76 @cf.name
72 77 end
73 78
74 79 def custom_field
75 80 @cf
76 81 end
77 82
78 83 def value(object)
79 84 cv = object.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)}
80 85 cv.size > 1 ? cv.sort {|a,b| a.to_s <=> b.to_s} : cv.first
81 86 end
82 87
83 88 def css_classes
84 89 @css_classes ||= "#{name} #{@cf.field_format}"
85 90 end
86 91 end
87 92
88 93 class QueryAssociationCustomFieldColumn < QueryCustomFieldColumn
89 94
90 95 def initialize(association, custom_field)
91 96 super(custom_field)
92 97 self.name = "#{association}.cf_#{custom_field.id}".to_sym
93 98 # TODO: support sorting/grouping by association custom field
94 99 self.sortable = false
95 100 self.groupable = false
96 101 @association = association
97 102 end
98 103
99 104 def value(object)
100 105 if assoc = object.send(@association)
101 106 super(assoc)
102 107 end
103 108 end
104 109
105 110 def css_classes
106 111 @css_classes ||= "#{@association}_cf_#{@cf.id} #{@cf.field_format}"
107 112 end
108 113 end
109 114
110 115 class Query < ActiveRecord::Base
111 116 class StatementInvalid < ::ActiveRecord::StatementInvalid
112 117 end
113 118
114 119 belongs_to :project
115 120 belongs_to :user
116 121 serialize :filters
117 122 serialize :column_names
118 123 serialize :sort_criteria, Array
119 124
120 125 attr_protected :project_id, :user_id
121 126
122 127 validates_presence_of :name
123 128 validates_length_of :name, :maximum => 255
124 129 validate :validate_query_filters
125 130
126 131 class_attribute :operators
127 132 self.operators = {
128 133 "=" => :label_equals,
129 134 "!" => :label_not_equals,
130 135 "o" => :label_open_issues,
131 136 "c" => :label_closed_issues,
132 137 "!*" => :label_none,
133 138 "*" => :label_any,
134 139 ">=" => :label_greater_or_equal,
135 140 "<=" => :label_less_or_equal,
136 141 "><" => :label_between,
137 142 "<t+" => :label_in_less_than,
138 143 ">t+" => :label_in_more_than,
139 144 "><t+"=> :label_in_the_next_days,
140 145 "t+" => :label_in,
141 146 "t" => :label_today,
142 147 "ld" => :label_yesterday,
143 148 "w" => :label_this_week,
144 149 "lw" => :label_last_week,
145 150 "l2w" => [:label_last_n_weeks, {:count => 2}],
146 151 "m" => :label_this_month,
147 152 "lm" => :label_last_month,
148 153 "y" => :label_this_year,
149 154 ">t-" => :label_less_than_ago,
150 155 "<t-" => :label_more_than_ago,
151 156 "><t-"=> :label_in_the_past_days,
152 157 "t-" => :label_ago,
153 158 "~" => :label_contains,
154 159 "!~" => :label_not_contains,
155 160 "=p" => :label_any_issues_in_project,
156 161 "=!p" => :label_any_issues_not_in_project,
157 162 "!p" => :label_no_issues_in_project
158 163 }
159 164
160 165 class_attribute :operators_by_filter_type
161 166 self.operators_by_filter_type = {
162 167 :list => [ "=", "!" ],
163 168 :list_status => [ "o", "=", "!", "c", "*" ],
164 169 :list_optional => [ "=", "!", "!*", "*" ],
165 170 :list_subprojects => [ "*", "!*", "=" ],
166 171 :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "><t+", "t+", "t", "ld", "w", "lw", "l2w", "m", "lm", "y", ">t-", "<t-", "><t-", "t-", "!*", "*" ],
167 172 :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "><t-", "t-", "t", "ld", "w", "lw", "l2w", "m", "lm", "y", "!*", "*" ],
168 173 :string => [ "=", "~", "!", "!~", "!*", "*" ],
169 174 :text => [ "~", "!~", "!*", "*" ],
170 175 :integer => [ "=", ">=", "<=", "><", "!*", "*" ],
171 176 :float => [ "=", ">=", "<=", "><", "!*", "*" ],
172 177 :relation => ["=", "=p", "=!p", "!p", "!*", "*"]
173 178 }
174 179
175 180 class_attribute :available_columns
176 181 self.available_columns = []
177 182
178 183 class_attribute :queried_class
179 184
180 185 def queried_table_name
181 186 @queried_table_name ||= self.class.queried_class.table_name
182 187 end
183 188
184 189 def initialize(attributes=nil, *args)
185 190 super attributes
186 191 @is_for_all = project.nil?
187 192 end
188 193
189 194 # Builds the query from the given params
190 195 def build_from_params(params)
191 196 if params[:fields] || params[:f]
192 197 self.filters = {}
193 198 add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
194 199 else
195 200 available_filters.keys.each do |field|
196 201 add_short_filter(field, params[field]) if params[field]
197 202 end
198 203 end
199 204 self.group_by = params[:group_by] || (params[:query] && params[:query][:group_by])
200 205 self.column_names = params[:c] || (params[:query] && params[:query][:column_names])
201 206 self
202 207 end
203 208
204 209 # Builds a new query from the given params and attributes
205 210 def self.build_from_params(params, attributes={})
206 211 new(attributes).build_from_params(params)
207 212 end
208 213
209 214 def validate_query_filters
210 215 filters.each_key do |field|
211 216 if values_for(field)
212 217 case type_for(field)
213 218 when :integer
214 219 add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) }
215 220 when :float
216 221 add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) }
217 222 when :date, :date_past
218 223 case operator_for(field)
219 224 when "=", ">=", "<=", "><"
220 225 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?) }
221 226 when ">t-", "<t-", "t-", ">t+", "<t+", "t+", "><t+", "><t-"
222 227 add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
223 228 end
224 229 end
225 230 end
226 231
227 232 add_filter_error(field, :blank) unless
228 233 # filter requires one or more values
229 234 (values_for(field) and !values_for(field).first.blank?) or
230 235 # filter doesn't require any value
231 236 ["o", "c", "!*", "*", "t", "ld", "w", "lw", "l2w", "m", "lm", "y"].include? operator_for(field)
232 237 end if filters
233 238 end
234 239
235 240 def add_filter_error(field, message)
236 241 m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages')
237 242 errors.add(:base, m)
238 243 end
239 244
240 245 def editable_by?(user)
241 246 return false unless user
242 247 # Admin can edit them all and regular users can edit their private queries
243 248 return true if user.admin? || (!is_public && self.user_id == user.id)
244 249 # Members can not edit public queries that are for all project (only admin is allowed to)
245 250 is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project)
246 251 end
247 252
248 253 def trackers
249 254 @trackers ||= project.nil? ? Tracker.sorted.all : project.rolled_up_trackers
250 255 end
251 256
252 257 # Returns a hash of localized labels for all filter operators
253 258 def self.operators_labels
254 259 operators.inject({}) {|h, operator| h[operator.first] = l(*operator.last); h}
255 260 end
256 261
257 262 # Returns a representation of the available filters for JSON serialization
258 263 def available_filters_as_json
259 264 json = {}
260 265 available_filters.each do |field, options|
261 266 json[field] = options.slice(:type, :name, :values).stringify_keys
262 267 end
263 268 json
264 269 end
265 270
266 271 def all_projects
267 272 @all_projects ||= Project.visible.all
268 273 end
269 274
270 275 def all_projects_values
271 276 return @all_projects_values if @all_projects_values
272 277
273 278 values = []
274 279 Project.project_tree(all_projects) do |p, level|
275 280 prefix = (level > 0 ? ('--' * level + ' ') : '')
276 281 values << ["#{prefix}#{p.name}", p.id.to_s]
277 282 end
278 283 @all_projects_values = values
279 284 end
280 285
281 286 # Adds available filters
282 287 def initialize_available_filters
283 288 # implemented by sub-classes
284 289 end
285 290 protected :initialize_available_filters
286 291
287 292 # Adds an available filter
288 293 def add_available_filter(field, options)
289 294 @available_filters ||= ActiveSupport::OrderedHash.new
290 295 @available_filters[field] = options
291 296 @available_filters
292 297 end
293 298
294 299 # Removes an available filter
295 300 def delete_available_filter(field)
296 301 if @available_filters
297 302 @available_filters.delete(field)
298 303 end
299 304 end
300 305
301 306 # Return a hash of available filters
302 307 def available_filters
303 308 unless @available_filters
304 309 initialize_available_filters
305 310 @available_filters.each do |field, options|
306 311 options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, ''))
307 312 end
308 313 end
309 314 @available_filters
310 315 end
311 316
312 317 def add_filter(field, operator, values=nil)
313 318 # values must be an array
314 319 return unless values.nil? || values.is_a?(Array)
315 320 # check if field is defined as an available filter
316 321 if available_filters.has_key? field
317 322 filter_options = available_filters[field]
318 323 filters[field] = {:operator => operator, :values => (values || [''])}
319 324 end
320 325 end
321 326
322 327 def add_short_filter(field, expression)
323 328 return unless expression && available_filters.has_key?(field)
324 329 field_type = available_filters[field][:type]
325 330 operators_by_filter_type[field_type].sort.reverse.detect do |operator|
326 331 next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/
327 332 values = $1
328 333 add_filter field, operator, values.present? ? values.split('|') : ['']
329 334 end || add_filter(field, '=', expression.split('|'))
330 335 end
331 336
332 337 # Add multiple filters using +add_filter+
333 338 def add_filters(fields, operators, values)
334 339 if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash))
335 340 fields.each do |field|
336 341 add_filter(field, operators[field], values && values[field])
337 342 end
338 343 end
339 344 end
340 345
341 346 def has_filter?(field)
342 347 filters and filters[field]
343 348 end
344 349
345 350 def type_for(field)
346 351 available_filters[field][:type] if available_filters.has_key?(field)
347 352 end
348 353
349 354 def operator_for(field)
350 355 has_filter?(field) ? filters[field][:operator] : nil
351 356 end
352 357
353 358 def values_for(field)
354 359 has_filter?(field) ? filters[field][:values] : nil
355 360 end
356 361
357 362 def value_for(field, index=0)
358 363 (values_for(field) || [])[index]
359 364 end
360 365
361 366 def label_for(field)
362 367 label = available_filters[field][:name] if available_filters.has_key?(field)
363 368 label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field)
364 369 end
365 370
366 371 def self.add_available_column(column)
367 372 self.available_columns << (column) if column.is_a?(QueryColumn)
368 373 end
369 374
370 375 # Returns an array of columns that can be used to group the results
371 376 def groupable_columns
372 377 available_columns.select {|c| c.groupable}
373 378 end
374 379
375 380 # Returns a Hash of columns and the key for sorting
376 381 def sortable_columns
377 382 available_columns.inject({}) {|h, column|
378 383 h[column.name.to_s] = column.sortable
379 384 h
380 385 }
381 386 end
382 387
383 388 def columns
384 389 # preserve the column_names order
385 (has_default_columns? ? default_columns_names : column_names).collect do |name|
390 cols = (has_default_columns? ? default_columns_names : column_names).collect do |name|
386 391 available_columns.find { |col| col.name == name }
387 392 end.compact
393 available_columns.select(&:frozen?) | cols
388 394 end
389 395
390 396 def inline_columns
391 397 columns.select(&:inline?)
392 398 end
393 399
394 400 def block_columns
395 401 columns.reject(&:inline?)
396 402 end
397 403
398 404 def available_inline_columns
399 405 available_columns.select(&:inline?)
400 406 end
401 407
402 408 def available_block_columns
403 409 available_columns.reject(&:inline?)
404 410 end
405 411
406 412 def default_columns_names
407 413 []
408 414 end
409 415
410 416 def column_names=(names)
411 417 if names
412 418 names = names.select {|n| n.is_a?(Symbol) || !n.blank? }
413 419 names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym }
414 420 # Set column_names to nil if default columns
415 421 if names == default_columns_names
416 422 names = nil
417 423 end
418 424 end
419 425 write_attribute(:column_names, names)
420 426 end
421 427
422 428 def has_column?(column)
423 429 column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column)
424 430 end
425 431
426 432 def has_default_columns?
427 433 column_names.nil? || column_names.empty?
428 434 end
429 435
430 436 def sort_criteria=(arg)
431 437 c = []
432 438 if arg.is_a?(Hash)
433 439 arg = arg.keys.sort.collect {|k| arg[k]}
434 440 end
435 441 c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']}
436 442 write_attribute(:sort_criteria, c)
437 443 end
438 444
439 445 def sort_criteria
440 446 read_attribute(:sort_criteria) || []
441 447 end
442 448
443 449 def sort_criteria_key(arg)
444 450 sort_criteria && sort_criteria[arg] && sort_criteria[arg].first
445 451 end
446 452
447 453 def sort_criteria_order(arg)
448 454 sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
449 455 end
450 456
451 457 def sort_criteria_order_for(key)
452 458 sort_criteria.detect {|k, order| key.to_s == k}.try(:last)
453 459 end
454 460
455 461 # Returns the SQL sort order that should be prepended for grouping
456 462 def group_by_sort_order
457 463 if grouped? && (column = group_by_column)
458 464 order = sort_criteria_order_for(column.name) || column.default_order
459 465 column.sortable.is_a?(Array) ?
460 466 column.sortable.collect {|s| "#{s} #{order}"}.join(',') :
461 467 "#{column.sortable} #{order}"
462 468 end
463 469 end
464 470
465 471 # Returns true if the query is a grouped query
466 472 def grouped?
467 473 !group_by_column.nil?
468 474 end
469 475
470 476 def group_by_column
471 477 groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by}
472 478 end
473 479
474 480 def group_by_statement
475 481 group_by_column.try(:groupable)
476 482 end
477 483
478 484 def project_statement
479 485 project_clauses = []
480 486 if project && !project.descendants.active.empty?
481 487 ids = [project.id]
482 488 if has_filter?("subproject_id")
483 489 case operator_for("subproject_id")
484 490 when '='
485 491 # include the selected subprojects
486 492 ids += values_for("subproject_id").each(&:to_i)
487 493 when '!*'
488 494 # main project only
489 495 else
490 496 # all subprojects
491 497 ids += project.descendants.collect(&:id)
492 498 end
493 499 elsif Setting.display_subprojects_issues?
494 500 ids += project.descendants.collect(&:id)
495 501 end
496 502 project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',')
497 503 elsif project
498 504 project_clauses << "#{Project.table_name}.id = %d" % project.id
499 505 end
500 506 project_clauses.any? ? project_clauses.join(' AND ') : nil
501 507 end
502 508
503 509 def statement
504 510 # filters clauses
505 511 filters_clauses = []
506 512 filters.each_key do |field|
507 513 next if field == "subproject_id"
508 514 v = values_for(field).clone
509 515 next unless v and !v.empty?
510 516 operator = operator_for(field)
511 517
512 518 # "me" value subsitution
513 519 if %w(assigned_to_id author_id user_id watcher_id).include?(field)
514 520 if v.delete("me")
515 521 if User.current.logged?
516 522 v.push(User.current.id.to_s)
517 523 v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id'
518 524 else
519 525 v.push("0")
520 526 end
521 527 end
522 528 end
523 529
524 530 if field == 'project_id'
525 531 if v.delete('mine')
526 532 v += User.current.memberships.map(&:project_id).map(&:to_s)
527 533 end
528 534 end
529 535
530 536 if field =~ /cf_(\d+)$/
531 537 # custom field
532 538 filters_clauses << sql_for_custom_field(field, operator, v, $1)
533 539 elsif respond_to?("sql_for_#{field}_field")
534 540 # specific statement
535 541 filters_clauses << send("sql_for_#{field}_field", field, operator, v)
536 542 else
537 543 # regular field
538 544 filters_clauses << '(' + sql_for_field(field, operator, v, queried_table_name, field) + ')'
539 545 end
540 546 end if filters and valid?
541 547
542 548 filters_clauses << project_statement
543 549 filters_clauses.reject!(&:blank?)
544 550
545 551 filters_clauses.any? ? filters_clauses.join(' AND ') : nil
546 552 end
547 553
548 554 private
549 555
550 556 def sql_for_custom_field(field, operator, value, custom_field_id)
551 557 db_table = CustomValue.table_name
552 558 db_field = 'value'
553 559 filter = @available_filters[field]
554 560 return nil unless filter
555 561 if filter[:format] == 'user'
556 562 if value.delete('me')
557 563 value.push User.current.id.to_s
558 564 end
559 565 end
560 566 not_in = nil
561 567 if operator == '!'
562 568 # Makes ! operator work for custom fields with multiple values
563 569 operator = '='
564 570 not_in = 'NOT'
565 571 end
566 572 customized_key = "id"
567 573 customized_class = queried_class
568 574 if field =~ /^(.+)\.cf_/
569 575 assoc = $1
570 576 customized_key = "#{assoc}_id"
571 577 customized_class = queried_class.reflect_on_association(assoc.to_sym).klass.base_class rescue nil
572 578 raise "Unknown #{queried_class.name} association #{assoc}" unless customized_class
573 579 end
574 580 "#{queried_table_name}.#{customized_key} #{not_in} IN (SELECT #{customized_class.table_name}.id FROM #{customized_class.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='#{customized_class}' AND #{db_table}.customized_id=#{customized_class.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " +
575 581 sql_for_field(field, operator, value, db_table, db_field, true) + ')'
576 582 end
577 583
578 584 # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+
579 585 def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false)
580 586 sql = ''
581 587 case operator
582 588 when "="
583 589 if value.any?
584 590 case type_for(field)
585 591 when :date, :date_past
586 592 sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil))
587 593 when :integer
588 594 if is_custom_filter
589 595 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) = #{value.first.to_i})"
590 596 else
591 597 sql = "#{db_table}.#{db_field} = #{value.first.to_i}"
592 598 end
593 599 when :float
594 600 if is_custom_filter
595 601 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})"
596 602 else
597 603 sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}"
598 604 end
599 605 else
600 606 sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
601 607 end
602 608 else
603 609 # IN an empty set
604 610 sql = "1=0"
605 611 end
606 612 when "!"
607 613 if value.any?
608 614 sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
609 615 else
610 616 # NOT IN an empty set
611 617 sql = "1=1"
612 618 end
613 619 when "!*"
614 620 sql = "#{db_table}.#{db_field} IS NULL"
615 621 sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter
616 622 when "*"
617 623 sql = "#{db_table}.#{db_field} IS NOT NULL"
618 624 sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
619 625 when ">="
620 626 if [:date, :date_past].include?(type_for(field))
621 627 sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
622 628 else
623 629 if is_custom_filter
624 630 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) >= #{value.first.to_f})"
625 631 else
626 632 sql = "#{db_table}.#{db_field} >= #{value.first.to_f}"
627 633 end
628 634 end
629 635 when "<="
630 636 if [:date, :date_past].include?(type_for(field))
631 637 sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
632 638 else
633 639 if is_custom_filter
634 640 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) <= #{value.first.to_f})"
635 641 else
636 642 sql = "#{db_table}.#{db_field} <= #{value.first.to_f}"
637 643 end
638 644 end
639 645 when "><"
640 646 if [:date, :date_past].include?(type_for(field))
641 647 sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil))
642 648 else
643 649 if is_custom_filter
644 650 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})"
645 651 else
646 652 sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
647 653 end
648 654 end
649 655 when "o"
650 656 sql = "#{queried_table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id"
651 657 when "c"
652 658 sql = "#{queried_table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id"
653 659 when "><t-"
654 660 # between today - n days and today
655 661 sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0)
656 662 when ">t-"
657 663 # >= today - n days
658 664 sql = relative_date_clause(db_table, db_field, - value.first.to_i, nil)
659 665 when "<t-"
660 666 # <= today - n days
661 667 sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i)
662 668 when "t-"
663 669 # = n days in past
664 670 sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i)
665 671 when "><t+"
666 672 # between today and today + n days
667 673 sql = relative_date_clause(db_table, db_field, 0, value.first.to_i)
668 674 when ">t+"
669 675 # >= today + n days
670 676 sql = relative_date_clause(db_table, db_field, value.first.to_i, nil)
671 677 when "<t+"
672 678 # <= today + n days
673 679 sql = relative_date_clause(db_table, db_field, nil, value.first.to_i)
674 680 when "t+"
675 681 # = today + n days
676 682 sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i)
677 683 when "t"
678 684 # = today
679 685 sql = relative_date_clause(db_table, db_field, 0, 0)
680 686 when "ld"
681 687 # = yesterday
682 688 sql = relative_date_clause(db_table, db_field, -1, -1)
683 689 when "w"
684 690 # = this week
685 691 first_day_of_week = l(:general_first_day_of_week).to_i
686 692 day_of_week = Date.today.cwday
687 693 days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
688 694 sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6)
689 695 when "lw"
690 696 # = last week
691 697 first_day_of_week = l(:general_first_day_of_week).to_i
692 698 day_of_week = Date.today.cwday
693 699 days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
694 700 sql = relative_date_clause(db_table, db_field, - days_ago - 7, - days_ago - 1)
695 701 when "l2w"
696 702 # = last 2 weeks
697 703 first_day_of_week = l(:general_first_day_of_week).to_i
698 704 day_of_week = Date.today.cwday
699 705 days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
700 706 sql = relative_date_clause(db_table, db_field, - days_ago - 14, - days_ago - 1)
701 707 when "m"
702 708 # = this month
703 709 date = Date.today
704 710 sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month)
705 711 when "lm"
706 712 # = last month
707 713 date = Date.today.prev_month
708 714 sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month)
709 715 when "y"
710 716 # = this year
711 717 date = Date.today
712 718 sql = date_clause(db_table, db_field, date.beginning_of_year, date.end_of_year)
713 719 when "~"
714 720 sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
715 721 when "!~"
716 722 sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
717 723 else
718 724 raise "Unknown query operator #{operator}"
719 725 end
720 726
721 727 return sql
722 728 end
723 729
724 730 def add_custom_fields_filters(custom_fields, assoc=nil)
725 731 return unless custom_fields.present?
726 732
727 733 custom_fields.select(&:is_filter?).sort.each do |field|
728 734 case field.field_format
729 735 when "text"
730 736 options = { :type => :text }
731 737 when "list"
732 738 options = { :type => :list_optional, :values => field.possible_values }
733 739 when "date"
734 740 options = { :type => :date }
735 741 when "bool"
736 742 options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] }
737 743 when "int"
738 744 options = { :type => :integer }
739 745 when "float"
740 746 options = { :type => :float }
741 747 when "user", "version"
742 748 next unless project
743 749 values = field.possible_values_options(project)
744 750 if User.current.logged? && field.field_format == 'user'
745 751 values.unshift ["<< #{l(:label_me)} >>", "me"]
746 752 end
747 753 options = { :type => :list_optional, :values => values }
748 754 else
749 755 options = { :type => :string }
750 756 end
751 757 filter_id = "cf_#{field.id}"
752 758 filter_name = field.name
753 759 if assoc.present?
754 760 filter_id = "#{assoc}.#{filter_id}"
755 761 filter_name = l("label_attribute_of_#{assoc}", :name => filter_name)
756 762 end
757 763 add_available_filter filter_id, options.merge({
758 764 :name => filter_name,
759 765 :format => field.field_format,
760 766 :field => field
761 767 })
762 768 end
763 769 end
764 770
765 771 def add_associations_custom_fields_filters(*associations)
766 772 fields_by_class = CustomField.where(:is_filter => true).group_by(&:class)
767 773 associations.each do |assoc|
768 774 association_klass = queried_class.reflect_on_association(assoc).klass
769 775 fields_by_class.each do |field_class, fields|
770 776 if field_class.customized_class <= association_klass
771 777 add_custom_fields_filters(fields, assoc)
772 778 end
773 779 end
774 780 end
775 781 end
776 782
777 783 # Returns a SQL clause for a date or datetime field.
778 784 def date_clause(table, field, from, to)
779 785 s = []
780 786 if from
781 787 from_yesterday = from - 1
782 788 from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day)
783 789 if self.class.default_timezone == :utc
784 790 from_yesterday_time = from_yesterday_time.utc
785 791 end
786 792 s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)])
787 793 end
788 794 if to
789 795 to_time = Time.local(to.year, to.month, to.day)
790 796 if self.class.default_timezone == :utc
791 797 to_time = to_time.utc
792 798 end
793 799 s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)])
794 800 end
795 801 s.join(' AND ')
796 802 end
797 803
798 804 # Returns a SQL clause for a date or datetime field using relative dates.
799 805 def relative_date_clause(table, field, days_from, days_to)
800 806 date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil))
801 807 end
802 808
803 809 # Additional joins required for the given sort options
804 810 def joins_for_order_statement(order_options)
805 811 joins = []
806 812
807 813 if order_options
808 814 if order_options.include?('authors')
809 815 joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{queried_table_name}.author_id"
810 816 end
811 817 order_options.scan(/cf_\d+/).uniq.each do |name|
812 818 column = available_columns.detect {|c| c.name.to_s == name}
813 819 join = column && column.custom_field.join_for_order_statement
814 820 if join
815 821 joins << join
816 822 end
817 823 end
818 824 end
819 825
820 826 joins.any? ? joins.join(' ') : nil
821 827 end
822 828 end
@@ -1,49 +1,47
1 1 <%= form_tag({}) do -%>
2 2 <%= hidden_field_tag 'back_url', url_for(params), :id => nil %>
3 3 <div class="autoscroll">
4 4 <table class="list issues">
5 5 <thead>
6 6 <tr>
7 7 <th class="checkbox hide-when-print">
8 8 <%= link_to image_tag('toggle_check.png'), {},
9 9 :onclick => 'toggleIssuesSelection(this); return false;',
10 10 :title => "#{l(:button_check_all)}/#{l(:button_uncheck_all)}" %>
11 11 </th>
12 <%= sort_header_tag('id', :caption => '#', :default_order => 'desc') %>
13 12 <% query.inline_columns.each do |column| %>
14 13 <%= column_header(column) %>
15 14 <% end %>
16 15 </tr>
17 16 </thead>
18 17 <% previous_group = false %>
19 18 <tbody>
20 19 <% issue_list(issues) do |issue, level| -%>
21 20 <% if @query.grouped? && (group = @query.group_by_column.value(issue)) != previous_group %>
22 21 <% reset_cycle %>
23 22 <tr class="group open">
24 23 <td colspan="<%= query.inline_columns.size + 2 %>">
25 24 <span class="expander" onclick="toggleRowGroup(this);">&nbsp;</span>
26 25 <%= group.blank? ? l(:label_none) : column_content(@query.group_by_column, issue) %> <span class="count"><%= @issue_count_by_group[group] %></span>
27 26 <%= link_to_function("#{l(:button_collapse_all)}/#{l(:button_expand_all)}",
28 27 "toggleAllRowGroups(this)", :class => 'toggle-all') %>
29 28 </td>
30 29 </tr>
31 30 <% previous_group = group %>
32 31 <% end %>
33 32 <tr id="issue-<%= issue.id %>" class="hascontextmenu <%= cycle('odd', 'even') %> <%= issue.css_classes %> <%= level > 0 ? "idnt idnt-#{level}" : nil %>">
34 33 <td class="checkbox hide-when-print"><%= check_box_tag("ids[]", issue.id, false, :id => nil) %></td>
35 <td class="id"><%= link_to issue.id, issue_path(issue) %></td>
36 34 <%= raw query.inline_columns.map {|column| "<td class=\"#{column.css_classes}\">#{column_content(column, issue)}</td>"}.join %>
37 35 </tr>
38 36 <% @query.block_columns.each do |column|
39 37 if (text = column_content(column, issue)) && text.present? -%>
40 38 <tr class="<%= current_cycle %>">
41 <td colspan="<%= @query.inline_columns.size + 2 %>" class="<%= column.css_classes %>"><%= text %></td>
39 <td colspan="<%= @query.inline_columns.size + 1 %>" class="<%= column.css_classes %>"><%= text %></td>
42 40 </tr>
43 41 <% end -%>
44 42 <% end -%>
45 43 <% end -%>
46 44 </tbody>
47 45 </table>
48 46 </div>
49 47 <% end -%>
@@ -1,34 +1,34
1 1 <table class="query-columns">
2 2 <tr>
3 3 <td style="padding-left:0">
4 4 <%= label_tag "available_columns", l(:description_available_columns) %>
5 5 <br />
6 6 <%= select_tag 'available_columns',
7 options_for_select((query.available_inline_columns - query.columns).collect {|column| [column.caption, column.name]}),
7 options_for_select((query.available_inline_columns - query.columns).reject(&:frozen?).collect {|column| [column.caption, column.name]}),
8 8 :multiple => true, :size => 10, :style => "width:150px",
9 9 :ondblclick => "moveOptions(this.form.available_columns, this.form.selected_columns);" %>
10 10 </td>
11 11 <td class="buttons">
12 12 <input type="button" value="&#8594;"
13 13 onclick="moveOptions(this.form.available_columns, this.form.selected_columns);" /><br />
14 14 <input type="button" value="&#8592;"
15 15 onclick="moveOptions(this.form.selected_columns, this.form.available_columns);" />
16 16 </td>
17 17 <td>
18 18 <%= label_tag "selected_columns", l(:description_selected_columns) %>
19 19 <br />
20 20 <%= select_tag((defined?(tag_name) ? tag_name : 'c[]'),
21 options_for_select(query.inline_columns.collect {|column| [column.caption, column.name]}),
21 options_for_select((query.inline_columns & query.available_inline_columns).reject(&:frozen?).collect {|column| [column.caption, column.name]}),
22 22 :id => 'selected_columns', :multiple => true, :size => 10, :style => "width:150px",
23 23 :ondblclick => "moveOptions(this.form.selected_columns, this.form.available_columns);") %>
24 24 </td>
25 25 <td class="buttons">
26 26 <input type="button" value="&#8593;" onclick="moveOptionUp(this.form.selected_columns);" /><br />
27 27 <input type="button" value="&#8595;" onclick="moveOptionDown(this.form.selected_columns);" />
28 28 </td>
29 29 </tr>
30 30 </table>
31 31
32 32 <% content_for :header_tags do %>
33 33 <%= javascript_include_tag 'select_list_move' %>
34 34 <% end %>
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,290 +1,290
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 QueriesControllerTest < ActionController::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries, :enabled_modules
22 22
23 23 def setup
24 24 User.current = nil
25 25 end
26 26
27 27 def test_index
28 28 get :index
29 29 # HTML response not implemented
30 30 assert_response 406
31 31 end
32 32
33 33 def test_new_project_query
34 34 @request.session[:user_id] = 2
35 35 get :new, :project_id => 1
36 36 assert_response :success
37 37 assert_template 'new'
38 38 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
39 39 :name => 'query[is_public]',
40 40 :checked => nil }
41 41 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
42 42 :name => 'query_is_for_all',
43 43 :checked => nil,
44 44 :disabled => nil }
45 45 assert_select 'select[name=?]', 'c[]' do
46 46 assert_select 'option[value=tracker]'
47 47 assert_select 'option[value=subject]'
48 48 end
49 49 end
50 50
51 51 def test_new_global_query
52 52 @request.session[:user_id] = 2
53 53 get :new
54 54 assert_response :success
55 55 assert_template 'new'
56 56 assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
57 57 :name => 'query[is_public]' }
58 58 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
59 59 :name => 'query_is_for_all',
60 60 :checked => 'checked',
61 61 :disabled => nil }
62 62 end
63 63
64 64 def test_new_on_invalid_project
65 65 @request.session[:user_id] = 2
66 66 get :new, :project_id => 'invalid'
67 67 assert_response 404
68 68 end
69 69
70 70 def test_create_project_public_query
71 71 @request.session[:user_id] = 2
72 72 post :create,
73 73 :project_id => 'ecookbook',
74 74 :default_columns => '1',
75 75 :f => ["status_id", "assigned_to_id"],
76 76 :op => {"assigned_to_id" => "=", "status_id" => "o"},
77 77 :v => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
78 78 :query => {"name" => "test_new_project_public_query", "is_public" => "1"}
79 79
80 80 q = Query.find_by_name('test_new_project_public_query')
81 81 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
82 82 assert q.is_public?
83 83 assert q.has_default_columns?
84 84 assert q.valid?
85 85 end
86 86
87 87 def test_create_project_private_query
88 88 @request.session[:user_id] = 3
89 89 post :create,
90 90 :project_id => 'ecookbook',
91 91 :default_columns => '1',
92 92 :fields => ["status_id", "assigned_to_id"],
93 93 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
94 94 :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
95 95 :query => {"name" => "test_new_project_private_query", "is_public" => "1"}
96 96
97 97 q = Query.find_by_name('test_new_project_private_query')
98 98 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
99 99 assert !q.is_public?
100 100 assert q.has_default_columns?
101 101 assert q.valid?
102 102 end
103 103
104 104 def test_create_global_private_query_with_custom_columns
105 105 @request.session[:user_id] = 3
106 106 post :create,
107 107 :fields => ["status_id", "assigned_to_id"],
108 108 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
109 109 :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
110 110 :query => {"name" => "test_new_global_private_query", "is_public" => "1"},
111 111 :c => ["", "tracker", "subject", "priority", "category"]
112 112
113 113 q = Query.find_by_name('test_new_global_private_query')
114 114 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
115 115 assert !q.is_public?
116 116 assert !q.has_default_columns?
117 assert_equal [:tracker, :subject, :priority, :category], q.columns.collect {|c| c.name}
117 assert_equal [:id, :tracker, :subject, :priority, :category], q.columns.collect {|c| c.name}
118 118 assert q.valid?
119 119 end
120 120
121 121 def test_create_global_query_with_custom_filters
122 122 @request.session[:user_id] = 3
123 123 post :create,
124 124 :fields => ["assigned_to_id"],
125 125 :operators => {"assigned_to_id" => "="},
126 126 :values => { "assigned_to_id" => ["me"]},
127 127 :query => {"name" => "test_new_global_query"}
128 128
129 129 q = Query.find_by_name('test_new_global_query')
130 130 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
131 131 assert !q.has_filter?(:status_id)
132 132 assert_equal ['assigned_to_id'], q.filters.keys
133 133 assert q.valid?
134 134 end
135 135
136 136 def test_create_with_sort
137 137 @request.session[:user_id] = 1
138 138 post :create,
139 139 :default_columns => '1',
140 140 :operators => {"status_id" => "o"},
141 141 :values => {"status_id" => ["1"]},
142 142 :query => {:name => "test_new_with_sort",
143 143 :is_public => "1",
144 144 :sort_criteria => {"0" => ["due_date", "desc"], "1" => ["tracker", ""]}}
145 145
146 146 query = Query.find_by_name("test_new_with_sort")
147 147 assert_not_nil query
148 148 assert_equal [['due_date', 'desc'], ['tracker', 'asc']], query.sort_criteria
149 149 end
150 150
151 151 def test_create_with_failure
152 152 @request.session[:user_id] = 2
153 153 assert_no_difference '::Query.count' do
154 154 post :create, :project_id => 'ecookbook', :query => {:name => ''}
155 155 end
156 156 assert_response :success
157 157 assert_template 'new'
158 158 assert_select 'input[name=?]', 'query[name]'
159 159 end
160 160
161 161 def test_edit_global_public_query
162 162 @request.session[:user_id] = 1
163 163 get :edit, :id => 4
164 164 assert_response :success
165 165 assert_template 'edit'
166 166 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
167 167 :name => 'query[is_public]',
168 168 :checked => 'checked' }
169 169 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
170 170 :name => 'query_is_for_all',
171 171 :checked => 'checked',
172 172 :disabled => 'disabled' }
173 173 end
174 174
175 175 def test_edit_global_private_query
176 176 @request.session[:user_id] = 3
177 177 get :edit, :id => 3
178 178 assert_response :success
179 179 assert_template 'edit'
180 180 assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
181 181 :name => 'query[is_public]' }
182 182 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
183 183 :name => 'query_is_for_all',
184 184 :checked => 'checked',
185 185 :disabled => 'disabled' }
186 186 end
187 187
188 188 def test_edit_project_private_query
189 189 @request.session[:user_id] = 3
190 190 get :edit, :id => 2
191 191 assert_response :success
192 192 assert_template 'edit'
193 193 assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
194 194 :name => 'query[is_public]' }
195 195 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
196 196 :name => 'query_is_for_all',
197 197 :checked => nil,
198 198 :disabled => nil }
199 199 end
200 200
201 201 def test_edit_project_public_query
202 202 @request.session[:user_id] = 2
203 203 get :edit, :id => 1
204 204 assert_response :success
205 205 assert_template 'edit'
206 206 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
207 207 :name => 'query[is_public]',
208 208 :checked => 'checked'
209 209 }
210 210 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
211 211 :name => 'query_is_for_all',
212 212 :checked => nil,
213 213 :disabled => 'disabled' }
214 214 end
215 215
216 216 def test_edit_sort_criteria
217 217 @request.session[:user_id] = 1
218 218 get :edit, :id => 5
219 219 assert_response :success
220 220 assert_template 'edit'
221 221 assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' },
222 222 :child => { :tag => 'option', :attributes => { :value => 'priority',
223 223 :selected => 'selected' } }
224 224 assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' },
225 225 :child => { :tag => 'option', :attributes => { :value => 'desc',
226 226 :selected => 'selected' } }
227 227 end
228 228
229 229 def test_edit_invalid_query
230 230 @request.session[:user_id] = 2
231 231 get :edit, :id => 99
232 232 assert_response 404
233 233 end
234 234
235 235 def test_udpate_global_private_query
236 236 @request.session[:user_id] = 3
237 237 put :update,
238 238 :id => 3,
239 239 :default_columns => '1',
240 240 :fields => ["status_id", "assigned_to_id"],
241 241 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
242 242 :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
243 243 :query => {"name" => "test_edit_global_private_query", "is_public" => "1"}
244 244
245 245 assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 3
246 246 q = Query.find_by_name('test_edit_global_private_query')
247 247 assert !q.is_public?
248 248 assert q.has_default_columns?
249 249 assert q.valid?
250 250 end
251 251
252 252 def test_update_global_public_query
253 253 @request.session[:user_id] = 1
254 254 put :update,
255 255 :id => 4,
256 256 :default_columns => '1',
257 257 :fields => ["status_id", "assigned_to_id"],
258 258 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
259 259 :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
260 260 :query => {"name" => "test_edit_global_public_query", "is_public" => "1"}
261 261
262 262 assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 4
263 263 q = Query.find_by_name('test_edit_global_public_query')
264 264 assert q.is_public?
265 265 assert q.has_default_columns?
266 266 assert q.valid?
267 267 end
268 268
269 269 def test_update_with_failure
270 270 @request.session[:user_id] = 1
271 271 put :update, :id => 4, :query => {:name => ''}
272 272 assert_response :success
273 273 assert_template 'edit'
274 274 end
275 275
276 276 def test_destroy
277 277 @request.session[:user_id] = 2
278 278 delete :destroy, :id => 1
279 279 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :set_filter => 1, :query_id => nil
280 280 assert_nil Query.find_by_id(1)
281 281 end
282 282
283 283 def test_backslash_should_be_escaped_in_filters
284 284 @request.session[:user_id] = 2
285 285 get :new, :subject => 'foo/bar'
286 286 assert_response :success
287 287 assert_template 'new'
288 288 assert_include 'addFilter("subject", "=", ["foo\/bar"]);', response.body
289 289 end
290 290 end
@@ -1,1216 +1,1234
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 include Redmine::I18n
22 22
23 23 fixtures :projects, :enabled_modules, :users, :members,
24 24 :member_roles, :roles, :trackers, :issue_statuses,
25 25 :issue_categories, :enumerations, :issues,
26 26 :watchers, :custom_fields, :custom_values, :versions,
27 27 :queries,
28 28 :projects_trackers,
29 29 :custom_fields_trackers
30 30
31 31 def test_available_filters_should_be_ordered
32 32 query = IssueQuery.new
33 33 assert_equal 0, query.available_filters.keys.index('status_id')
34 34 end
35 35
36 36 def test_custom_fields_for_all_projects_should_be_available_in_global_queries
37 37 query = IssueQuery.new(:project => nil, :name => '_')
38 38 assert query.available_filters.has_key?('cf_1')
39 39 assert !query.available_filters.has_key?('cf_3')
40 40 end
41 41
42 42 def test_system_shared_versions_should_be_available_in_global_queries
43 43 Version.find(2).update_attribute :sharing, 'system'
44 44 query = IssueQuery.new(:project => nil, :name => '_')
45 45 assert query.available_filters.has_key?('fixed_version_id')
46 46 assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'}
47 47 end
48 48
49 49 def test_project_filter_in_global_queries
50 50 query = IssueQuery.new(:project => nil, :name => '_')
51 51 project_filter = query.available_filters["project_id"]
52 52 assert_not_nil project_filter
53 53 project_ids = project_filter[:values].map{|p| p[1]}
54 54 assert project_ids.include?("1") #public project
55 55 assert !project_ids.include?("2") #private project user cannot see
56 56 end
57 57
58 58 def find_issues_with_query(query)
59 59 Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where(
60 60 query.statement
61 61 ).all
62 62 end
63 63
64 64 def assert_find_issues_with_query_is_successful(query)
65 65 assert_nothing_raised do
66 66 find_issues_with_query(query)
67 67 end
68 68 end
69 69
70 70 def assert_query_statement_includes(query, condition)
71 71 assert_include condition, query.statement
72 72 end
73 73
74 74 def assert_query_result(expected, query)
75 75 assert_nothing_raised do
76 76 assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort
77 77 assert_equal expected.size, query.issue_count
78 78 end
79 79 end
80 80
81 81 def test_query_should_allow_shared_versions_for_a_project_query
82 82 subproject_version = Version.find(4)
83 83 query = IssueQuery.new(:project => Project.find(1), :name => '_')
84 84 query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s])
85 85
86 86 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')")
87 87 end
88 88
89 89 def test_query_with_multiple_custom_fields
90 90 query = IssueQuery.find(1)
91 91 assert query.valid?
92 92 assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')")
93 93 issues = find_issues_with_query(query)
94 94 assert_equal 1, issues.length
95 95 assert_equal Issue.find(3), issues.first
96 96 end
97 97
98 98 def test_operator_none
99 99 query = IssueQuery.new(:project => Project.find(1), :name => '_')
100 100 query.add_filter('fixed_version_id', '!*', [''])
101 101 query.add_filter('cf_1', '!*', [''])
102 102 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL")
103 103 assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''")
104 104 find_issues_with_query(query)
105 105 end
106 106
107 107 def test_operator_none_for_integer
108 108 query = IssueQuery.new(:project => Project.find(1), :name => '_')
109 109 query.add_filter('estimated_hours', '!*', [''])
110 110 issues = find_issues_with_query(query)
111 111 assert !issues.empty?
112 112 assert issues.all? {|i| !i.estimated_hours}
113 113 end
114 114
115 115 def test_operator_none_for_date
116 116 query = IssueQuery.new(:project => Project.find(1), :name => '_')
117 117 query.add_filter('start_date', '!*', [''])
118 118 issues = find_issues_with_query(query)
119 119 assert !issues.empty?
120 120 assert issues.all? {|i| i.start_date.nil?}
121 121 end
122 122
123 123 def test_operator_none_for_string_custom_field
124 124 query = IssueQuery.new(:project => Project.find(1), :name => '_')
125 125 query.add_filter('cf_2', '!*', [''])
126 126 assert query.has_filter?('cf_2')
127 127 issues = find_issues_with_query(query)
128 128 assert !issues.empty?
129 129 assert issues.all? {|i| i.custom_field_value(2).blank?}
130 130 end
131 131
132 132 def test_operator_all
133 133 query = IssueQuery.new(:project => Project.find(1), :name => '_')
134 134 query.add_filter('fixed_version_id', '*', [''])
135 135 query.add_filter('cf_1', '*', [''])
136 136 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL")
137 137 assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''")
138 138 find_issues_with_query(query)
139 139 end
140 140
141 141 def test_operator_all_for_date
142 142 query = IssueQuery.new(:project => Project.find(1), :name => '_')
143 143 query.add_filter('start_date', '*', [''])
144 144 issues = find_issues_with_query(query)
145 145 assert !issues.empty?
146 146 assert issues.all? {|i| i.start_date.present?}
147 147 end
148 148
149 149 def test_operator_all_for_string_custom_field
150 150 query = IssueQuery.new(:project => Project.find(1), :name => '_')
151 151 query.add_filter('cf_2', '*', [''])
152 152 assert query.has_filter?('cf_2')
153 153 issues = find_issues_with_query(query)
154 154 assert !issues.empty?
155 155 assert issues.all? {|i| i.custom_field_value(2).present?}
156 156 end
157 157
158 158 def test_numeric_filter_should_not_accept_non_numeric_values
159 159 query = IssueQuery.new(:name => '_')
160 160 query.add_filter('estimated_hours', '=', ['a'])
161 161
162 162 assert query.has_filter?('estimated_hours')
163 163 assert !query.valid?
164 164 end
165 165
166 166 def test_operator_is_on_float
167 167 Issue.update_all("estimated_hours = 171.2", "id=2")
168 168
169 169 query = IssueQuery.new(:name => '_')
170 170 query.add_filter('estimated_hours', '=', ['171.20'])
171 171 issues = find_issues_with_query(query)
172 172 assert_equal 1, issues.size
173 173 assert_equal 2, issues.first.id
174 174 end
175 175
176 176 def test_operator_is_on_integer_custom_field
177 177 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true)
178 178 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
179 179 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
180 180 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
181 181
182 182 query = IssueQuery.new(:name => '_')
183 183 query.add_filter("cf_#{f.id}", '=', ['12'])
184 184 issues = find_issues_with_query(query)
185 185 assert_equal 1, issues.size
186 186 assert_equal 2, issues.first.id
187 187 end
188 188
189 189 def test_operator_is_on_integer_custom_field_should_accept_negative_value
190 190 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true)
191 191 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
192 192 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12')
193 193 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
194 194
195 195 query = IssueQuery.new(:name => '_')
196 196 query.add_filter("cf_#{f.id}", '=', ['-12'])
197 197 assert query.valid?
198 198 issues = find_issues_with_query(query)
199 199 assert_equal 1, issues.size
200 200 assert_equal 2, issues.first.id
201 201 end
202 202
203 203 def test_operator_is_on_float_custom_field
204 204 f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true)
205 205 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3')
206 206 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7')
207 207 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
208 208
209 209 query = IssueQuery.new(:name => '_')
210 210 query.add_filter("cf_#{f.id}", '=', ['12.7'])
211 211 issues = find_issues_with_query(query)
212 212 assert_equal 1, issues.size
213 213 assert_equal 2, issues.first.id
214 214 end
215 215
216 216 def test_operator_is_on_float_custom_field_should_accept_negative_value
217 217 f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true)
218 218 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3')
219 219 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12.7')
220 220 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
221 221
222 222 query = IssueQuery.new(:name => '_')
223 223 query.add_filter("cf_#{f.id}", '=', ['-12.7'])
224 224 assert query.valid?
225 225 issues = find_issues_with_query(query)
226 226 assert_equal 1, issues.size
227 227 assert_equal 2, issues.first.id
228 228 end
229 229
230 230 def test_operator_is_on_multi_list_custom_field
231 231 f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true,
232 232 :possible_values => ['value1', 'value2', 'value3'], :multiple => true)
233 233 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1')
234 234 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2')
235 235 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1')
236 236
237 237 query = IssueQuery.new(:name => '_')
238 238 query.add_filter("cf_#{f.id}", '=', ['value1'])
239 239 issues = find_issues_with_query(query)
240 240 assert_equal [1, 3], issues.map(&:id).sort
241 241
242 242 query = IssueQuery.new(:name => '_')
243 243 query.add_filter("cf_#{f.id}", '=', ['value2'])
244 244 issues = find_issues_with_query(query)
245 245 assert_equal [1], issues.map(&:id).sort
246 246 end
247 247
248 248 def test_operator_is_not_on_multi_list_custom_field
249 249 f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true,
250 250 :possible_values => ['value1', 'value2', 'value3'], :multiple => true)
251 251 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1')
252 252 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2')
253 253 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1')
254 254
255 255 query = IssueQuery.new(:name => '_')
256 256 query.add_filter("cf_#{f.id}", '!', ['value1'])
257 257 issues = find_issues_with_query(query)
258 258 assert !issues.map(&:id).include?(1)
259 259 assert !issues.map(&:id).include?(3)
260 260
261 261 query = IssueQuery.new(:name => '_')
262 262 query.add_filter("cf_#{f.id}", '!', ['value2'])
263 263 issues = find_issues_with_query(query)
264 264 assert !issues.map(&:id).include?(1)
265 265 assert issues.map(&:id).include?(3)
266 266 end
267 267
268 268 def test_operator_is_on_is_private_field
269 269 # is_private filter only available for those who can set issues private
270 270 User.current = User.find(2)
271 271
272 272 query = IssueQuery.new(:name => '_')
273 273 assert query.available_filters.key?('is_private')
274 274
275 275 query.add_filter("is_private", '=', ['1'])
276 276 issues = find_issues_with_query(query)
277 277 assert issues.any?
278 278 assert_nil issues.detect {|issue| !issue.is_private?}
279 279 ensure
280 280 User.current = nil
281 281 end
282 282
283 283 def test_operator_is_not_on_is_private_field
284 284 # is_private filter only available for those who can set issues private
285 285 User.current = User.find(2)
286 286
287 287 query = IssueQuery.new(:name => '_')
288 288 assert query.available_filters.key?('is_private')
289 289
290 290 query.add_filter("is_private", '!', ['1'])
291 291 issues = find_issues_with_query(query)
292 292 assert issues.any?
293 293 assert_nil issues.detect {|issue| issue.is_private?}
294 294 ensure
295 295 User.current = nil
296 296 end
297 297
298 298 def test_operator_greater_than
299 299 query = IssueQuery.new(:project => Project.find(1), :name => '_')
300 300 query.add_filter('done_ratio', '>=', ['40'])
301 301 assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0")
302 302 find_issues_with_query(query)
303 303 end
304 304
305 305 def test_operator_greater_than_a_float
306 306 query = IssueQuery.new(:project => Project.find(1), :name => '_')
307 307 query.add_filter('estimated_hours', '>=', ['40.5'])
308 308 assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5")
309 309 find_issues_with_query(query)
310 310 end
311 311
312 312 def test_operator_greater_than_on_int_custom_field
313 313 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
314 314 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
315 315 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
316 316 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
317 317
318 318 query = IssueQuery.new(:project => Project.find(1), :name => '_')
319 319 query.add_filter("cf_#{f.id}", '>=', ['8'])
320 320 issues = find_issues_with_query(query)
321 321 assert_equal 1, issues.size
322 322 assert_equal 2, issues.first.id
323 323 end
324 324
325 325 def test_operator_lesser_than
326 326 query = IssueQuery.new(:project => Project.find(1), :name => '_')
327 327 query.add_filter('done_ratio', '<=', ['30'])
328 328 assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0")
329 329 find_issues_with_query(query)
330 330 end
331 331
332 332 def test_operator_lesser_than_on_custom_field
333 333 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
334 334 query = IssueQuery.new(:project => Project.find(1), :name => '_')
335 335 query.add_filter("cf_#{f.id}", '<=', ['30'])
336 336 assert_match /CAST.+ <= 30\.0/, query.statement
337 337 find_issues_with_query(query)
338 338 end
339 339
340 340 def test_operator_between
341 341 query = IssueQuery.new(:project => Project.find(1), :name => '_')
342 342 query.add_filter('done_ratio', '><', ['30', '40'])
343 343 assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement
344 344 find_issues_with_query(query)
345 345 end
346 346
347 347 def test_operator_between_on_custom_field
348 348 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
349 349 query = IssueQuery.new(:project => Project.find(1), :name => '_')
350 350 query.add_filter("cf_#{f.id}", '><', ['30', '40'])
351 351 assert_match /CAST.+ BETWEEN 30.0 AND 40.0/, query.statement
352 352 find_issues_with_query(query)
353 353 end
354 354
355 355 def test_date_filter_should_not_accept_non_date_values
356 356 query = IssueQuery.new(:name => '_')
357 357 query.add_filter('created_on', '=', ['a'])
358 358
359 359 assert query.has_filter?('created_on')
360 360 assert !query.valid?
361 361 end
362 362
363 363 def test_date_filter_should_not_accept_invalid_date_values
364 364 query = IssueQuery.new(:name => '_')
365 365 query.add_filter('created_on', '=', ['2011-01-34'])
366 366
367 367 assert query.has_filter?('created_on')
368 368 assert !query.valid?
369 369 end
370 370
371 371 def test_relative_date_filter_should_not_accept_non_integer_values
372 372 query = IssueQuery.new(:name => '_')
373 373 query.add_filter('created_on', '>t-', ['a'])
374 374
375 375 assert query.has_filter?('created_on')
376 376 assert !query.valid?
377 377 end
378 378
379 379 def test_operator_date_equals
380 380 query = IssueQuery.new(:name => '_')
381 381 query.add_filter('due_date', '=', ['2011-07-10'])
382 382 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
383 383 find_issues_with_query(query)
384 384 end
385 385
386 386 def test_operator_date_lesser_than
387 387 query = IssueQuery.new(:name => '_')
388 388 query.add_filter('due_date', '<=', ['2011-07-10'])
389 389 assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
390 390 find_issues_with_query(query)
391 391 end
392 392
393 393 def test_operator_date_greater_than
394 394 query = IssueQuery.new(:name => '_')
395 395 query.add_filter('due_date', '>=', ['2011-07-10'])
396 396 assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement
397 397 find_issues_with_query(query)
398 398 end
399 399
400 400 def test_operator_date_between
401 401 query = IssueQuery.new(:name => '_')
402 402 query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10'])
403 403 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
404 404 find_issues_with_query(query)
405 405 end
406 406
407 407 def test_operator_in_more_than
408 408 Issue.find(7).update_attribute(:due_date, (Date.today + 15))
409 409 query = IssueQuery.new(:project => Project.find(1), :name => '_')
410 410 query.add_filter('due_date', '>t+', ['15'])
411 411 issues = find_issues_with_query(query)
412 412 assert !issues.empty?
413 413 issues.each {|issue| assert(issue.due_date >= (Date.today + 15))}
414 414 end
415 415
416 416 def test_operator_in_less_than
417 417 query = IssueQuery.new(:project => Project.find(1), :name => '_')
418 418 query.add_filter('due_date', '<t+', ['15'])
419 419 issues = find_issues_with_query(query)
420 420 assert !issues.empty?
421 421 issues.each {|issue| assert(issue.due_date <= (Date.today + 15))}
422 422 end
423 423
424 424 def test_operator_in_the_next_days
425 425 query = IssueQuery.new(:project => Project.find(1), :name => '_')
426 426 query.add_filter('due_date', '><t+', ['15'])
427 427 issues = find_issues_with_query(query)
428 428 assert !issues.empty?
429 429 issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))}
430 430 end
431 431
432 432 def test_operator_less_than_ago
433 433 Issue.find(7).update_attribute(:due_date, (Date.today - 3))
434 434 query = IssueQuery.new(:project => Project.find(1), :name => '_')
435 435 query.add_filter('due_date', '>t-', ['3'])
436 436 issues = find_issues_with_query(query)
437 437 assert !issues.empty?
438 438 issues.each {|issue| assert(issue.due_date >= (Date.today - 3))}
439 439 end
440 440
441 441 def test_operator_in_the_past_days
442 442 Issue.find(7).update_attribute(:due_date, (Date.today - 3))
443 443 query = IssueQuery.new(:project => Project.find(1), :name => '_')
444 444 query.add_filter('due_date', '><t-', ['3'])
445 445 issues = find_issues_with_query(query)
446 446 assert !issues.empty?
447 447 issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)}
448 448 end
449 449
450 450 def test_operator_more_than_ago
451 451 Issue.find(7).update_attribute(:due_date, (Date.today - 10))
452 452 query = IssueQuery.new(:project => Project.find(1), :name => '_')
453 453 query.add_filter('due_date', '<t-', ['10'])
454 454 assert query.statement.include?("#{Issue.table_name}.due_date <=")
455 455 issues = find_issues_with_query(query)
456 456 assert !issues.empty?
457 457 issues.each {|issue| assert(issue.due_date <= (Date.today - 10))}
458 458 end
459 459
460 460 def test_operator_in
461 461 Issue.find(7).update_attribute(:due_date, (Date.today + 2))
462 462 query = IssueQuery.new(:project => Project.find(1), :name => '_')
463 463 query.add_filter('due_date', 't+', ['2'])
464 464 issues = find_issues_with_query(query)
465 465 assert !issues.empty?
466 466 issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)}
467 467 end
468 468
469 469 def test_operator_ago
470 470 Issue.find(7).update_attribute(:due_date, (Date.today - 3))
471 471 query = IssueQuery.new(:project => Project.find(1), :name => '_')
472 472 query.add_filter('due_date', 't-', ['3'])
473 473 issues = find_issues_with_query(query)
474 474 assert !issues.empty?
475 475 issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)}
476 476 end
477 477
478 478 def test_operator_today
479 479 query = IssueQuery.new(:project => Project.find(1), :name => '_')
480 480 query.add_filter('due_date', 't', [''])
481 481 issues = find_issues_with_query(query)
482 482 assert !issues.empty?
483 483 issues.each {|issue| assert_equal Date.today, issue.due_date}
484 484 end
485 485
486 486 def test_operator_this_week_on_date
487 487 query = IssueQuery.new(:project => Project.find(1), :name => '_')
488 488 query.add_filter('due_date', 'w', [''])
489 489 find_issues_with_query(query)
490 490 end
491 491
492 492 def test_operator_this_week_on_datetime
493 493 query = IssueQuery.new(:project => Project.find(1), :name => '_')
494 494 query.add_filter('created_on', 'w', [''])
495 495 find_issues_with_query(query)
496 496 end
497 497
498 498 def test_operator_contains
499 499 query = IssueQuery.new(:project => Project.find(1), :name => '_')
500 500 query.add_filter('subject', '~', ['uNable'])
501 501 assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'")
502 502 result = find_issues_with_query(query)
503 503 assert result.empty?
504 504 result.each {|issue| assert issue.subject.downcase.include?('unable') }
505 505 end
506 506
507 507 def test_range_for_this_week_with_week_starting_on_monday
508 508 I18n.locale = :fr
509 509 assert_equal '1', I18n.t(:general_first_day_of_week)
510 510
511 511 Date.stubs(:today).returns(Date.parse('2011-04-29'))
512 512
513 513 query = IssueQuery.new(:project => Project.find(1), :name => '_')
514 514 query.add_filter('due_date', 'w', [''])
515 515 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}"
516 516 I18n.locale = :en
517 517 end
518 518
519 519 def test_range_for_this_week_with_week_starting_on_sunday
520 520 I18n.locale = :en
521 521 assert_equal '7', I18n.t(:general_first_day_of_week)
522 522
523 523 Date.stubs(:today).returns(Date.parse('2011-04-29'))
524 524
525 525 query = IssueQuery.new(:project => Project.find(1), :name => '_')
526 526 query.add_filter('due_date', 'w', [''])
527 527 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}"
528 528 end
529 529
530 530 def test_operator_does_not_contains
531 531 query = IssueQuery.new(:project => Project.find(1), :name => '_')
532 532 query.add_filter('subject', '!~', ['uNable'])
533 533 assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'")
534 534 find_issues_with_query(query)
535 535 end
536 536
537 537 def test_filter_assigned_to_me
538 538 user = User.find(2)
539 539 group = Group.find(10)
540 540 User.current = user
541 541 i1 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => user)
542 542 i2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => group)
543 543 i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11))
544 544 group.users << user
545 545
546 546 query = IssueQuery.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}})
547 547 result = query.issues
548 548 assert_equal Issue.visible.all(:conditions => {:assigned_to_id => ([2] + user.reload.group_ids)}).sort_by(&:id), result.sort_by(&:id)
549 549
550 550 assert result.include?(i1)
551 551 assert result.include?(i2)
552 552 assert !result.include?(i3)
553 553 end
554 554
555 555 def test_user_custom_field_filtered_on_me
556 556 User.current = User.find(2)
557 557 cf = IssueCustomField.create!(:field_format => 'user', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1])
558 558 issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'}, :subject => 'Test', :author_id => 1)
559 559 issue2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '3'})
560 560
561 561 query = IssueQuery.new(:name => '_', :project => Project.find(1))
562 562 filter = query.available_filters["cf_#{cf.id}"]
563 563 assert_not_nil filter
564 564 assert_include 'me', filter[:values].map{|v| v[1]}
565 565
566 566 query.filters = { "cf_#{cf.id}" => {:operator => '=', :values => ['me']}}
567 567 result = query.issues
568 568 assert_equal 1, result.size
569 569 assert_equal issue1, result.first
570 570 end
571 571
572 572 def test_filter_my_projects
573 573 User.current = User.find(2)
574 574 query = IssueQuery.new(:name => '_')
575 575 filter = query.available_filters['project_id']
576 576 assert_not_nil filter
577 577 assert_include 'mine', filter[:values].map{|v| v[1]}
578 578
579 579 query.filters = { 'project_id' => {:operator => '=', :values => ['mine']}}
580 580 result = query.issues
581 581 assert_nil result.detect {|issue| !User.current.member_of?(issue.project)}
582 582 end
583 583
584 584 def test_filter_watched_issues
585 585 User.current = User.find(1)
586 586 query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}})
587 587 result = find_issues_with_query(query)
588 588 assert_not_nil result
589 589 assert !result.empty?
590 590 assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id)
591 591 User.current = nil
592 592 end
593 593
594 594 def test_filter_unwatched_issues
595 595 User.current = User.find(1)
596 596 query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}})
597 597 result = find_issues_with_query(query)
598 598 assert_not_nil result
599 599 assert !result.empty?
600 600 assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size)
601 601 User.current = nil
602 602 end
603 603
604 604 def test_filter_on_project_custom_field
605 605 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
606 606 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
607 607 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
608 608
609 609 query = IssueQuery.new(:name => '_')
610 610 filter_name = "project.cf_#{field.id}"
611 611 assert_include filter_name, query.available_filters.keys
612 612 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
613 613 assert_equal [3, 5], find_issues_with_query(query).map(&:project_id).uniq.sort
614 614 end
615 615
616 616 def test_filter_on_author_custom_field
617 617 field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
618 618 CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo')
619 619
620 620 query = IssueQuery.new(:name => '_')
621 621 filter_name = "author.cf_#{field.id}"
622 622 assert_include filter_name, query.available_filters.keys
623 623 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
624 624 assert_equal [3], find_issues_with_query(query).map(&:author_id).uniq.sort
625 625 end
626 626
627 627 def test_filter_on_assigned_to_custom_field
628 628 field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
629 629 CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo')
630 630
631 631 query = IssueQuery.new(:name => '_')
632 632 filter_name = "assigned_to.cf_#{field.id}"
633 633 assert_include filter_name, query.available_filters.keys
634 634 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
635 635 assert_equal [3], find_issues_with_query(query).map(&:assigned_to_id).uniq.sort
636 636 end
637 637
638 638 def test_filter_on_fixed_version_custom_field
639 639 field = VersionCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
640 640 CustomValue.create!(:custom_field => field, :customized => Version.find(2), :value => 'Foo')
641 641
642 642 query = IssueQuery.new(:name => '_')
643 643 filter_name = "fixed_version.cf_#{field.id}"
644 644 assert_include filter_name, query.available_filters.keys
645 645 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
646 646 assert_equal [2], find_issues_with_query(query).map(&:fixed_version_id).uniq.sort
647 647 end
648 648
649 649 def test_filter_on_relations_with_a_specific_issue
650 650 IssueRelation.delete_all
651 651 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
652 652 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
653 653
654 654 query = IssueQuery.new(:name => '_')
655 655 query.filters = {"relates" => {:operator => '=', :values => ['1']}}
656 656 assert_equal [2, 3], find_issues_with_query(query).map(&:id).sort
657 657
658 658 query = IssueQuery.new(:name => '_')
659 659 query.filters = {"relates" => {:operator => '=', :values => ['2']}}
660 660 assert_equal [1], find_issues_with_query(query).map(&:id).sort
661 661 end
662 662
663 663 def test_filter_on_relations_with_any_issues_in_a_project
664 664 IssueRelation.delete_all
665 665 with_settings :cross_project_issue_relations => '1' do
666 666 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first)
667 667 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(2).issues.first)
668 668 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first)
669 669 end
670 670
671 671 query = IssueQuery.new(:name => '_')
672 672 query.filters = {"relates" => {:operator => '=p', :values => ['2']}}
673 673 assert_equal [1, 2], find_issues_with_query(query).map(&:id).sort
674 674
675 675 query = IssueQuery.new(:name => '_')
676 676 query.filters = {"relates" => {:operator => '=p', :values => ['3']}}
677 677 assert_equal [1], find_issues_with_query(query).map(&:id).sort
678 678
679 679 query = IssueQuery.new(:name => '_')
680 680 query.filters = {"relates" => {:operator => '=p', :values => ['4']}}
681 681 assert_equal [], find_issues_with_query(query).map(&:id).sort
682 682 end
683 683
684 684 def test_filter_on_relations_with_any_issues_not_in_a_project
685 685 IssueRelation.delete_all
686 686 with_settings :cross_project_issue_relations => '1' do
687 687 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first)
688 688 #IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(1).issues.first)
689 689 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first)
690 690 end
691 691
692 692 query = IssueQuery.new(:name => '_')
693 693 query.filters = {"relates" => {:operator => '=!p', :values => ['1']}}
694 694 assert_equal [1], find_issues_with_query(query).map(&:id).sort
695 695 end
696 696
697 697 def test_filter_on_relations_with_no_issues_in_a_project
698 698 IssueRelation.delete_all
699 699 with_settings :cross_project_issue_relations => '1' do
700 700 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first)
701 701 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(3).issues.first)
702 702 IssueRelation.create!(:relation_type => "relates", :issue_to => Project.find(2).issues.first, :issue_from => Issue.find(3))
703 703 end
704 704
705 705 query = IssueQuery.new(:name => '_')
706 706 query.filters = {"relates" => {:operator => '!p', :values => ['2']}}
707 707 ids = find_issues_with_query(query).map(&:id).sort
708 708 assert_include 2, ids
709 709 assert_not_include 1, ids
710 710 assert_not_include 3, ids
711 711 end
712 712
713 713 def test_filter_on_relations_with_no_issues
714 714 IssueRelation.delete_all
715 715 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
716 716 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
717 717
718 718 query = IssueQuery.new(:name => '_')
719 719 query.filters = {"relates" => {:operator => '!*', :values => ['']}}
720 720 ids = find_issues_with_query(query).map(&:id)
721 721 assert_equal [], ids & [1, 2, 3]
722 722 assert_include 4, ids
723 723 end
724 724
725 725 def test_filter_on_relations_with_any_issues
726 726 IssueRelation.delete_all
727 727 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
728 728 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
729 729
730 730 query = IssueQuery.new(:name => '_')
731 731 query.filters = {"relates" => {:operator => '*', :values => ['']}}
732 732 assert_equal [1, 2, 3], find_issues_with_query(query).map(&:id).sort
733 733 end
734 734
735 735 def test_statement_should_be_nil_with_no_filters
736 736 q = IssueQuery.new(:name => '_')
737 737 q.filters = {}
738 738
739 739 assert q.valid?
740 740 assert_nil q.statement
741 741 end
742 742
743 743 def test_default_columns
744 744 q = IssueQuery.new
745 745 assert q.columns.any?
746 746 assert q.inline_columns.any?
747 747 assert q.block_columns.empty?
748 748 end
749 749
750 750 def test_set_column_names
751 751 q = IssueQuery.new
752 752 q.column_names = ['tracker', :subject, '', 'unknonw_column']
753 assert_equal [:tracker, :subject], q.columns.collect {|c| c.name}
754 c = q.columns.first
755 assert q.has_column?(c)
753 assert_equal [:id, :tracker, :subject], q.columns.collect {|c| c.name}
754 end
755
756 def test_has_column_should_accept_a_column_name
757 q = IssueQuery.new
758 q.column_names = ['tracker', :subject]
759 assert q.has_column?(:tracker)
760 assert !q.has_column?(:category)
761 end
762
763 def test_has_column_should_accept_a_column
764 q = IssueQuery.new
765 q.column_names = ['tracker', :subject]
766
767 tracker_column = q.available_columns.detect {|c| c.name==:tracker}
768 assert_kind_of QueryColumn, tracker_column
769 category_column = q.available_columns.detect {|c| c.name==:category}
770 assert_kind_of QueryColumn, category_column
771
772 assert q.has_column?(tracker_column)
773 assert !q.has_column?(category_column)
756 774 end
757 775
758 776 def test_inline_and_block_columns
759 777 q = IssueQuery.new
760 778 q.column_names = ['subject', 'description', 'tracker']
761 779
762 assert_equal [:subject, :tracker], q.inline_columns.map(&:name)
780 assert_equal [:id, :subject, :tracker], q.inline_columns.map(&:name)
763 781 assert_equal [:description], q.block_columns.map(&:name)
764 782 end
765 783
766 784 def test_custom_field_columns_should_be_inline
767 785 q = IssueQuery.new
768 786 columns = q.available_columns.select {|column| column.is_a? QueryCustomFieldColumn}
769 787 assert columns.any?
770 788 assert_nil columns.detect {|column| !column.inline?}
771 789 end
772 790
773 791 def test_query_should_preload_spent_hours
774 792 q = IssueQuery.new(:name => '_', :column_names => [:subject, :spent_hours])
775 793 assert q.has_column?(:spent_hours)
776 794 issues = q.issues
777 795 assert_not_nil issues.first.instance_variable_get("@spent_hours")
778 796 end
779 797
780 798 def test_groupable_columns_should_include_custom_fields
781 799 q = IssueQuery.new
782 800 column = q.groupable_columns.detect {|c| c.name == :cf_1}
783 801 assert_not_nil column
784 802 assert_kind_of QueryCustomFieldColumn, column
785 803 end
786 804
787 805 def test_groupable_columns_should_not_include_multi_custom_fields
788 806 field = CustomField.find(1)
789 807 field.update_attribute :multiple, true
790 808
791 809 q = IssueQuery.new
792 810 column = q.groupable_columns.detect {|c| c.name == :cf_1}
793 811 assert_nil column
794 812 end
795 813
796 814 def test_groupable_columns_should_include_user_custom_fields
797 815 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'user')
798 816
799 817 q = IssueQuery.new
800 818 assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym}
801 819 end
802 820
803 821 def test_groupable_columns_should_include_version_custom_fields
804 822 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'version')
805 823
806 824 q = IssueQuery.new
807 825 assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym}
808 826 end
809 827
810 828 def test_grouped_with_valid_column
811 829 q = IssueQuery.new(:group_by => 'status')
812 830 assert q.grouped?
813 831 assert_not_nil q.group_by_column
814 832 assert_equal :status, q.group_by_column.name
815 833 assert_not_nil q.group_by_statement
816 834 assert_equal 'status', q.group_by_statement
817 835 end
818 836
819 837 def test_grouped_with_invalid_column
820 838 q = IssueQuery.new(:group_by => 'foo')
821 839 assert !q.grouped?
822 840 assert_nil q.group_by_column
823 841 assert_nil q.group_by_statement
824 842 end
825 843
826 844 def test_sortable_columns_should_sort_assignees_according_to_user_format_setting
827 845 with_settings :user_format => 'lastname_coma_firstname' do
828 846 q = IssueQuery.new
829 847 assert q.sortable_columns.has_key?('assigned_to')
830 848 assert_equal %w(users.lastname users.firstname users.id), q.sortable_columns['assigned_to']
831 849 end
832 850 end
833 851
834 852 def test_sortable_columns_should_sort_authors_according_to_user_format_setting
835 853 with_settings :user_format => 'lastname_coma_firstname' do
836 854 q = IssueQuery.new
837 855 assert q.sortable_columns.has_key?('author')
838 856 assert_equal %w(authors.lastname authors.firstname authors.id), q.sortable_columns['author']
839 857 end
840 858 end
841 859
842 860 def test_sortable_columns_should_include_custom_field
843 861 q = IssueQuery.new
844 862 assert q.sortable_columns['cf_1']
845 863 end
846 864
847 865 def test_sortable_columns_should_not_include_multi_custom_field
848 866 field = CustomField.find(1)
849 867 field.update_attribute :multiple, true
850 868
851 869 q = IssueQuery.new
852 870 assert !q.sortable_columns['cf_1']
853 871 end
854 872
855 873 def test_default_sort
856 874 q = IssueQuery.new
857 875 assert_equal [], q.sort_criteria
858 876 end
859 877
860 878 def test_set_sort_criteria_with_hash
861 879 q = IssueQuery.new
862 880 q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']}
863 881 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
864 882 end
865 883
866 884 def test_set_sort_criteria_with_array
867 885 q = IssueQuery.new
868 886 q.sort_criteria = [['priority', 'desc'], 'tracker']
869 887 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
870 888 end
871 889
872 890 def test_create_query_with_sort
873 891 q = IssueQuery.new(:name => 'Sorted')
874 892 q.sort_criteria = [['priority', 'desc'], 'tracker']
875 893 assert q.save
876 894 q.reload
877 895 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
878 896 end
879 897
880 898 def test_sort_by_string_custom_field_asc
881 899 q = IssueQuery.new
882 900 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
883 901 assert c
884 902 assert c.sortable
885 903 issues = q.issues(:order => "#{c.sortable} ASC")
886 904 values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
887 905 assert !values.empty?
888 906 assert_equal values.sort, values
889 907 end
890 908
891 909 def test_sort_by_string_custom_field_desc
892 910 q = IssueQuery.new
893 911 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
894 912 assert c
895 913 assert c.sortable
896 914 issues = q.issues(:order => "#{c.sortable} DESC")
897 915 values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
898 916 assert !values.empty?
899 917 assert_equal values.sort.reverse, values
900 918 end
901 919
902 920 def test_sort_by_float_custom_field_asc
903 921 q = IssueQuery.new
904 922 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' }
905 923 assert c
906 924 assert c.sortable
907 925 issues = q.issues(:order => "#{c.sortable} ASC")
908 926 values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact
909 927 assert !values.empty?
910 928 assert_equal values.sort, values
911 929 end
912 930
913 931 def test_invalid_query_should_raise_query_statement_invalid_error
914 932 q = IssueQuery.new
915 933 assert_raise Query::StatementInvalid do
916 934 q.issues(:conditions => "foo = 1")
917 935 end
918 936 end
919 937
920 938 def test_issue_count
921 939 q = IssueQuery.new(:name => '_')
922 940 issue_count = q.issue_count
923 941 assert_equal q.issues.size, issue_count
924 942 end
925 943
926 944 def test_issue_count_with_archived_issues
927 945 p = Project.generate! do |project|
928 946 project.status = Project::STATUS_ARCHIVED
929 947 end
930 948 i = Issue.generate!( :project => p, :tracker => p.trackers.first )
931 949 assert !i.visible?
932 950
933 951 test_issue_count
934 952 end
935 953
936 954 def test_issue_count_by_association_group
937 955 q = IssueQuery.new(:name => '_', :group_by => 'assigned_to')
938 956 count_by_group = q.issue_count_by_group
939 957 assert_kind_of Hash, count_by_group
940 958 assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
941 959 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq
942 960 assert count_by_group.has_key?(User.find(3))
943 961 end
944 962
945 963 def test_issue_count_by_list_custom_field_group
946 964 q = IssueQuery.new(:name => '_', :group_by => 'cf_1')
947 965 count_by_group = q.issue_count_by_group
948 966 assert_kind_of Hash, count_by_group
949 967 assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
950 968 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq
951 969 assert count_by_group.has_key?('MySQL')
952 970 end
953 971
954 972 def test_issue_count_by_date_custom_field_group
955 973 q = IssueQuery.new(:name => '_', :group_by => 'cf_8')
956 974 count_by_group = q.issue_count_by_group
957 975 assert_kind_of Hash, count_by_group
958 976 assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
959 977 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq
960 978 end
961 979
962 980 def test_issue_count_with_nil_group_only
963 981 Issue.update_all("assigned_to_id = NULL")
964 982
965 983 q = IssueQuery.new(:name => '_', :group_by => 'assigned_to')
966 984 count_by_group = q.issue_count_by_group
967 985 assert_kind_of Hash, count_by_group
968 986 assert_equal 1, count_by_group.keys.size
969 987 assert_nil count_by_group.keys.first
970 988 end
971 989
972 990 def test_issue_ids
973 991 q = IssueQuery.new(:name => '_')
974 992 order = "issues.subject, issues.id"
975 993 issues = q.issues(:order => order)
976 994 assert_equal issues.map(&:id), q.issue_ids(:order => order)
977 995 end
978 996
979 997 def test_label_for
980 998 set_language_if_valid 'en'
981 999 q = IssueQuery.new
982 1000 assert_equal 'Assignee', q.label_for('assigned_to_id')
983 1001 end
984 1002
985 1003 def test_label_for_fr
986 1004 set_language_if_valid 'fr'
987 1005 q = IssueQuery.new
988 1006 s = "Assign\xc3\xa9 \xc3\xa0"
989 1007 s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
990 1008 assert_equal s, q.label_for('assigned_to_id')
991 1009 end
992 1010
993 1011 def test_editable_by
994 1012 admin = User.find(1)
995 1013 manager = User.find(2)
996 1014 developer = User.find(3)
997 1015
998 1016 # Public query on project 1
999 1017 q = IssueQuery.find(1)
1000 1018 assert q.editable_by?(admin)
1001 1019 assert q.editable_by?(manager)
1002 1020 assert !q.editable_by?(developer)
1003 1021
1004 1022 # Private query on project 1
1005 1023 q = IssueQuery.find(2)
1006 1024 assert q.editable_by?(admin)
1007 1025 assert !q.editable_by?(manager)
1008 1026 assert q.editable_by?(developer)
1009 1027
1010 1028 # Private query for all projects
1011 1029 q = IssueQuery.find(3)
1012 1030 assert q.editable_by?(admin)
1013 1031 assert !q.editable_by?(manager)
1014 1032 assert q.editable_by?(developer)
1015 1033
1016 1034 # Public query for all projects
1017 1035 q = IssueQuery.find(4)
1018 1036 assert q.editable_by?(admin)
1019 1037 assert !q.editable_by?(manager)
1020 1038 assert !q.editable_by?(developer)
1021 1039 end
1022 1040
1023 1041 def test_visible_scope
1024 1042 query_ids = IssueQuery.visible(User.anonymous).map(&:id)
1025 1043
1026 1044 assert query_ids.include?(1), 'public query on public project was not visible'
1027 1045 assert query_ids.include?(4), 'public query for all projects was not visible'
1028 1046 assert !query_ids.include?(2), 'private query on public project was visible'
1029 1047 assert !query_ids.include?(3), 'private query for all projects was visible'
1030 1048 assert !query_ids.include?(7), 'public query on private project was visible'
1031 1049 end
1032 1050
1033 1051 test "#available_filters should include users of visible projects in cross-project view" do
1034 1052 users = IssueQuery.new.available_filters["assigned_to_id"]
1035 1053 assert_not_nil users
1036 1054 assert users[:values].map{|u|u[1]}.include?("3")
1037 1055 end
1038 1056
1039 1057 test "#available_filters should include users of subprojects" do
1040 1058 user1 = User.generate!
1041 1059 user2 = User.generate!
1042 1060 project = Project.find(1)
1043 1061 Member.create!(:principal => user1, :project => project.children.visible.first, :role_ids => [1])
1044 1062
1045 1063 users = IssueQuery.new(:project => project).available_filters["assigned_to_id"]
1046 1064 assert_not_nil users
1047 1065 assert users[:values].map{|u|u[1]}.include?(user1.id.to_s)
1048 1066 assert !users[:values].map{|u|u[1]}.include?(user2.id.to_s)
1049 1067 end
1050 1068
1051 1069 test "#available_filters should include visible projects in cross-project view" do
1052 1070 projects = IssueQuery.new.available_filters["project_id"]
1053 1071 assert_not_nil projects
1054 1072 assert projects[:values].map{|u|u[1]}.include?("1")
1055 1073 end
1056 1074
1057 1075 test "#available_filters should include 'member_of_group' filter" do
1058 1076 query = IssueQuery.new
1059 1077 assert query.available_filters.keys.include?("member_of_group")
1060 1078 assert_equal :list_optional, query.available_filters["member_of_group"][:type]
1061 1079 assert query.available_filters["member_of_group"][:values].present?
1062 1080 assert_equal Group.all.sort.map {|g| [g.name, g.id.to_s]},
1063 1081 query.available_filters["member_of_group"][:values].sort
1064 1082 end
1065 1083
1066 1084 test "#available_filters should include 'assigned_to_role' filter" do
1067 1085 query = IssueQuery.new
1068 1086 assert query.available_filters.keys.include?("assigned_to_role")
1069 1087 assert_equal :list_optional, query.available_filters["assigned_to_role"][:type]
1070 1088
1071 1089 assert query.available_filters["assigned_to_role"][:values].include?(['Manager','1'])
1072 1090 assert query.available_filters["assigned_to_role"][:values].include?(['Developer','2'])
1073 1091 assert query.available_filters["assigned_to_role"][:values].include?(['Reporter','3'])
1074 1092
1075 1093 assert ! query.available_filters["assigned_to_role"][:values].include?(['Non member','4'])
1076 1094 assert ! query.available_filters["assigned_to_role"][:values].include?(['Anonymous','5'])
1077 1095 end
1078 1096
1079 1097 context "#statement" do
1080 1098 context "with 'member_of_group' filter" do
1081 1099 setup do
1082 1100 Group.destroy_all # No fixtures
1083 1101 @user_in_group = User.generate!
1084 1102 @second_user_in_group = User.generate!
1085 1103 @user_in_group2 = User.generate!
1086 1104 @user_not_in_group = User.generate!
1087 1105
1088 1106 @group = Group.generate!.reload
1089 1107 @group.users << @user_in_group
1090 1108 @group.users << @second_user_in_group
1091 1109
1092 1110 @group2 = Group.generate!.reload
1093 1111 @group2.users << @user_in_group2
1094 1112
1095 1113 end
1096 1114
1097 1115 should "search assigned to for users in the group" do
1098 1116 @query = IssueQuery.new(:name => '_')
1099 1117 @query.add_filter('member_of_group', '=', [@group.id.to_s])
1100 1118
1101 1119 assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@group.id}')"
1102 1120 assert_find_issues_with_query_is_successful @query
1103 1121 end
1104 1122
1105 1123 should "search not assigned to any group member (none)" do
1106 1124 @query = IssueQuery.new(:name => '_')
1107 1125 @query.add_filter('member_of_group', '!*', [''])
1108 1126
1109 1127 # Users not in a group
1110 1128 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}','#{@group.id}','#{@group2.id}')"
1111 1129 assert_find_issues_with_query_is_successful @query
1112 1130 end
1113 1131
1114 1132 should "search assigned to any group member (all)" do
1115 1133 @query = IssueQuery.new(:name => '_')
1116 1134 @query.add_filter('member_of_group', '*', [''])
1117 1135
1118 1136 # Only users in a group
1119 1137 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}','#{@group.id}','#{@group2.id}')"
1120 1138 assert_find_issues_with_query_is_successful @query
1121 1139 end
1122 1140
1123 1141 should "return an empty set with = empty group" do
1124 1142 @empty_group = Group.generate!
1125 1143 @query = IssueQuery.new(:name => '_')
1126 1144 @query.add_filter('member_of_group', '=', [@empty_group.id.to_s])
1127 1145
1128 1146 assert_equal [], find_issues_with_query(@query)
1129 1147 end
1130 1148
1131 1149 should "return issues with ! empty group" do
1132 1150 @empty_group = Group.generate!
1133 1151 @query = IssueQuery.new(:name => '_')
1134 1152 @query.add_filter('member_of_group', '!', [@empty_group.id.to_s])
1135 1153
1136 1154 assert_find_issues_with_query_is_successful @query
1137 1155 end
1138 1156 end
1139 1157
1140 1158 context "with 'assigned_to_role' filter" do
1141 1159 setup do
1142 1160 @manager_role = Role.find_by_name('Manager')
1143 1161 @developer_role = Role.find_by_name('Developer')
1144 1162
1145 1163 @project = Project.generate!
1146 1164 @manager = User.generate!
1147 1165 @developer = User.generate!
1148 1166 @boss = User.generate!
1149 1167 @guest = User.generate!
1150 1168 User.add_to_project(@manager, @project, @manager_role)
1151 1169 User.add_to_project(@developer, @project, @developer_role)
1152 1170 User.add_to_project(@boss, @project, [@manager_role, @developer_role])
1153 1171
1154 1172 @issue1 = Issue.generate!(:project => @project, :assigned_to_id => @manager.id)
1155 1173 @issue2 = Issue.generate!(:project => @project, :assigned_to_id => @developer.id)
1156 1174 @issue3 = Issue.generate!(:project => @project, :assigned_to_id => @boss.id)
1157 1175 @issue4 = Issue.generate!(:project => @project, :assigned_to_id => @guest.id)
1158 1176 @issue5 = Issue.generate!(:project => @project)
1159 1177 end
1160 1178
1161 1179 should "search assigned to for users with the Role" do
1162 1180 @query = IssueQuery.new(:name => '_', :project => @project)
1163 1181 @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s])
1164 1182
1165 1183 assert_query_result [@issue1, @issue3], @query
1166 1184 end
1167 1185
1168 1186 should "search assigned to for users with the Role on the issue project" do
1169 1187 other_project = Project.generate!
1170 1188 User.add_to_project(@developer, other_project, @manager_role)
1171 1189
1172 1190 @query = IssueQuery.new(:name => '_', :project => @project)
1173 1191 @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s])
1174 1192
1175 1193 assert_query_result [@issue1, @issue3], @query
1176 1194 end
1177 1195
1178 1196 should "return an empty set with empty role" do
1179 1197 @empty_role = Role.generate!
1180 1198 @query = IssueQuery.new(:name => '_', :project => @project)
1181 1199 @query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s])
1182 1200
1183 1201 assert_query_result [], @query
1184 1202 end
1185 1203
1186 1204 should "search assigned to for users without the Role" do
1187 1205 @query = IssueQuery.new(:name => '_', :project => @project)
1188 1206 @query.add_filter('assigned_to_role', '!', [@manager_role.id.to_s])
1189 1207
1190 1208 assert_query_result [@issue2, @issue4, @issue5], @query
1191 1209 end
1192 1210
1193 1211 should "search assigned to for users not assigned to any Role (none)" do
1194 1212 @query = IssueQuery.new(:name => '_', :project => @project)
1195 1213 @query.add_filter('assigned_to_role', '!*', [''])
1196 1214
1197 1215 assert_query_result [@issue4, @issue5], @query
1198 1216 end
1199 1217
1200 1218 should "search assigned to for users assigned to any Role (all)" do
1201 1219 @query = IssueQuery.new(:name => '_', :project => @project)
1202 1220 @query.add_filter('assigned_to_role', '*', [''])
1203 1221
1204 1222 assert_query_result [@issue1, @issue2, @issue3], @query
1205 1223 end
1206 1224
1207 1225 should "return issues with ! empty role" do
1208 1226 @empty_role = Role.generate!
1209 1227 @query = IssueQuery.new(:name => '_', :project => @project)
1210 1228 @query.add_filter('assigned_to_role', '!', [@empty_role.id.to_s])
1211 1229
1212 1230 assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query
1213 1231 end
1214 1232 end
1215 1233 end
1216 1234 end
General Comments 0
You need to be logged in to leave comments. Login now