@@ -5,20 +5,20 | |||||
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 |
# |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 |
# |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 |
class QueryColumn |
|
18 | class QueryColumn | |
19 | attr_accessor :name, :sortable, :groupable, :default_order |
|
19 | attr_accessor :name, :sortable, :groupable, :default_order | |
20 | include Redmine::I18n |
|
20 | include Redmine::I18n | |
21 |
|
21 | |||
22 | def initialize(name, options={}) |
|
22 | def initialize(name, options={}) | |
23 | self.name = name |
|
23 | self.name = name | |
24 | self.sortable = options[:sortable] |
|
24 | self.sortable = options[:sortable] | |
@@ -29,20 +29,20 class QueryColumn | |||||
29 | self.default_order = options[:default_order] |
|
29 | self.default_order = options[:default_order] | |
30 | @caption_key = options[:caption] || "field_#{name}" |
|
30 | @caption_key = options[:caption] || "field_#{name}" | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def caption |
|
33 | def caption | |
34 | l(@caption_key) |
|
34 | l(@caption_key) | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | # Returns true if the column is sortable, otherwise false |
|
37 | # Returns true if the column is sortable, otherwise false | |
38 | def sortable? |
|
38 | def sortable? | |
39 | !sortable.nil? |
|
39 | !sortable.nil? | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def value(issue) |
|
42 | def value(issue) | |
43 | issue.send name |
|
43 | issue.send name | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def css_classes |
|
46 | def css_classes | |
47 | name |
|
47 | name | |
48 | end |
|
48 | end | |
@@ -59,20 +59,20 class QueryCustomFieldColumn < QueryColumn | |||||
59 | self.groupable ||= false |
|
59 | self.groupable ||= false | |
60 | @cf = custom_field |
|
60 | @cf = custom_field | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def caption |
|
63 | def caption | |
64 | @cf.name |
|
64 | @cf.name | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | def custom_field |
|
67 | def custom_field | |
68 | @cf |
|
68 | @cf | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | def value(issue) |
|
71 | def value(issue) | |
72 | cv = issue.custom_values.detect {|v| v.custom_field_id == @cf.id} |
|
72 | cv = issue.custom_values.detect {|v| v.custom_field_id == @cf.id} | |
73 | cv && @cf.cast_value(cv.value) |
|
73 | cv && @cf.cast_value(cv.value) | |
74 | end |
|
74 | end | |
75 |
|
75 | |||
76 | def css_classes |
|
76 | def css_classes | |
77 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
77 | @css_classes ||= "#{name} #{@cf.field_format}" | |
78 | end |
|
78 | end | |
@@ -81,19 +81,19 end | |||||
81 | class Query < ActiveRecord::Base |
|
81 | class Query < ActiveRecord::Base | |
82 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
82 | class StatementInvalid < ::ActiveRecord::StatementInvalid | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | belongs_to :project |
|
85 | belongs_to :project | |
86 | belongs_to :user |
|
86 | belongs_to :user | |
87 | serialize :filters |
|
87 | serialize :filters | |
88 | serialize :column_names |
|
88 | serialize :column_names | |
89 | serialize :sort_criteria, Array |
|
89 | serialize :sort_criteria, Array | |
90 |
|
90 | |||
91 | attr_protected :project_id, :user_id |
|
91 | attr_protected :project_id, :user_id | |
92 |
|
92 | |||
93 | validates_presence_of :name, :on => :save |
|
93 | validates_presence_of :name, :on => :save | |
94 | validates_length_of :name, :maximum => 255 |
|
94 | validates_length_of :name, :maximum => 255 | |
95 |
|
95 | |||
96 |
@@operators = { "=" => :label_equals, |
|
96 | @@operators = { "=" => :label_equals, | |
97 | "!" => :label_not_equals, |
|
97 | "!" => :label_not_equals, | |
98 | "o" => :label_open_issues, |
|
98 | "o" => :label_open_issues, | |
99 | "c" => :label_closed_issues, |
|
99 | "c" => :label_closed_issues, | |
@@ -113,7 +113,7 class Query < ActiveRecord::Base | |||||
113 | "!~" => :label_not_contains } |
|
113 | "!~" => :label_not_contains } | |
114 |
|
114 | |||
115 | cattr_reader :operators |
|
115 | cattr_reader :operators | |
116 |
|
116 | |||
117 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
117 | @@operators_by_filter_type = { :list => [ "=", "!" ], | |
118 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
118 | :list_status => [ "o", "=", "!", "c", "*" ], | |
119 | :list_optional => [ "=", "!", "!*", "*" ], |
|
119 | :list_optional => [ "=", "!", "!*", "*" ], | |
@@ -145,27 +145,27 class Query < ActiveRecord::Base | |||||
145 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
145 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), | |
146 | ] |
|
146 | ] | |
147 | cattr_reader :available_columns |
|
147 | cattr_reader :available_columns | |
148 |
|
148 | |||
149 | def initialize(attributes = nil) |
|
149 | def initialize(attributes = nil) | |
150 | super attributes |
|
150 | super attributes | |
151 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
151 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } | |
152 | end |
|
152 | end | |
153 |
|
153 | |||
154 | def after_initialize |
|
154 | def after_initialize | |
155 | # Store the fact that project is nil (used in #editable_by?) |
|
155 | # Store the fact that project is nil (used in #editable_by?) | |
156 | @is_for_all = project.nil? |
|
156 | @is_for_all = project.nil? | |
157 | end |
|
157 | end | |
158 |
|
158 | |||
159 | def validate |
|
159 | def validate | |
160 | filters.each_key do |field| |
|
160 | filters.each_key do |field| | |
161 |
errors.add label_for(field), :blank unless |
|
161 | errors.add label_for(field), :blank unless | |
162 | # filter requires one or more values |
|
162 | # filter requires one or more values | |
163 |
(values_for(field) and !values_for(field).first.blank?) or |
|
163 | (values_for(field) and !values_for(field).first.blank?) or | |
164 | # filter doesn't require any value |
|
164 | # filter doesn't require any value | |
165 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
165 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) | |
166 | end if filters |
|
166 | end if filters | |
167 | end |
|
167 | end | |
168 |
|
168 | |||
169 | def editable_by?(user) |
|
169 | def editable_by?(user) | |
170 | return false unless user |
|
170 | return false unless user | |
171 | # Admin can edit them all and regular users can edit their private queries |
|
171 | # Admin can edit them all and regular users can edit their private queries | |
@@ -173,23 +173,23 class Query < ActiveRecord::Base | |||||
173 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
173 | # Members can not edit public queries that are for all project (only admin is allowed to) | |
174 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
174 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) | |
175 | end |
|
175 | end | |
176 |
|
176 | |||
177 | def available_filters |
|
177 | def available_filters | |
178 | return @available_filters if @available_filters |
|
178 | return @available_filters if @available_filters | |
179 |
|
179 | |||
180 | trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
180 | trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers | |
181 |
|
181 | |||
182 |
@available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
182 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, | |
183 |
"tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, |
|
183 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, | |
184 | "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } }, |
|
184 | "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } }, | |
185 |
"subject" => { :type => :text, :order => 8 }, |
|
185 | "subject" => { :type => :text, :order => 8 }, | |
186 |
"created_on" => { :type => :date_past, :order => 9 }, |
|
186 | "created_on" => { :type => :date_past, :order => 9 }, | |
187 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
187 | "updated_on" => { :type => :date_past, :order => 10 }, | |
188 | "start_date" => { :type => :date, :order => 11 }, |
|
188 | "start_date" => { :type => :date, :order => 11 }, | |
189 | "due_date" => { :type => :date, :order => 12 }, |
|
189 | "due_date" => { :type => :date, :order => 12 }, | |
190 | "estimated_hours" => { :type => :integer, :order => 13 }, |
|
190 | "estimated_hours" => { :type => :integer, :order => 13 }, | |
191 | "done_ratio" => { :type => :integer, :order => 14 }} |
|
191 | "done_ratio" => { :type => :integer, :order => 14 }} | |
192 |
|
192 | |||
193 | user_values = [] |
|
193 | user_values = [] | |
194 | user_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
194 | user_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
195 | if project |
|
195 | if project | |
@@ -199,7 +199,7 class Query < ActiveRecord::Base | |||||
199 | if all_projects.any? |
|
199 | if all_projects.any? | |
200 | # members of visible projects |
|
200 | # members of visible projects | |
201 | user_values += User.active.find(:all, :conditions => ["#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id IN (?))", all_projects.collect(&:id)]).sort.collect{|s| [s.name, s.id.to_s] } |
|
201 | user_values += User.active.find(:all, :conditions => ["#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id IN (?))", all_projects.collect(&:id)]).sort.collect{|s| [s.name, s.id.to_s] } | |
202 |
|
202 | |||
203 | # project filter |
|
203 | # project filter | |
204 | project_values = [] |
|
204 | project_values = [] | |
205 | Project.project_tree(all_projects) do |p, level| |
|
205 | Project.project_tree(all_projects) do |p, level| | |
@@ -217,11 +217,11 class Query < ActiveRecord::Base | |||||
217 |
|
217 | |||
218 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
218 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } | |
219 | @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty? |
|
219 | @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty? | |
220 |
|
220 | |||
221 | if User.current.logged? |
|
221 | if User.current.logged? | |
222 | @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } |
|
222 | @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } | |
223 | end |
|
223 | end | |
224 |
|
224 | |||
225 | if project |
|
225 | if project | |
226 | # project specific filters |
|
226 | # project specific filters | |
227 | categories = @project.issue_categories.all |
|
227 | categories = @project.issue_categories.all | |
@@ -249,7 +249,7 class Query < ActiveRecord::Base | |||||
249 | end |
|
249 | end | |
250 | @available_filters |
|
250 | @available_filters | |
251 | end |
|
251 | end | |
252 |
|
252 | |||
253 | def add_filter(field, operator, values) |
|
253 | def add_filter(field, operator, values) | |
254 | # values must be an array |
|
254 | # values must be an array | |
255 | return unless values and values.is_a? Array # and !values.first.empty? |
|
255 | return unless values and values.is_a? Array # and !values.first.empty? | |
@@ -264,7 +264,7 class Query < ActiveRecord::Base | |||||
264 | filters[field] = {:operator => operator, :values => values } |
|
264 | filters[field] = {:operator => operator, :values => values } | |
265 | end |
|
265 | end | |
266 | end |
|
266 | end | |
267 |
|
267 | |||
268 | def add_short_filter(field, expression) |
|
268 | def add_short_filter(field, expression) | |
269 | return unless expression |
|
269 | return unless expression | |
270 | parms = expression.scan(/^(o|c|!\*|!|\*)?(.*)$/).first |
|
270 | parms = expression.scan(/^(o|c|!\*|!|\*)?(.*)$/).first | |
@@ -279,19 +279,19 class Query < ActiveRecord::Base | |||||
279 | end |
|
279 | end | |
280 | end |
|
280 | end | |
281 | end |
|
281 | end | |
282 |
|
282 | |||
283 | def has_filter?(field) |
|
283 | def has_filter?(field) | |
284 | filters and filters[field] |
|
284 | filters and filters[field] | |
285 | end |
|
285 | end | |
286 |
|
286 | |||
287 | def operator_for(field) |
|
287 | def operator_for(field) | |
288 | has_filter?(field) ? filters[field][:operator] : nil |
|
288 | has_filter?(field) ? filters[field][:operator] : nil | |
289 | end |
|
289 | end | |
290 |
|
290 | |||
291 | def values_for(field) |
|
291 | def values_for(field) | |
292 | has_filter?(field) ? filters[field][:values] : nil |
|
292 | has_filter?(field) ? filters[field][:values] : nil | |
293 | end |
|
293 | end | |
294 |
|
294 | |||
295 | def label_for(field) |
|
295 | def label_for(field) | |
296 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
296 | label = available_filters[field][:name] if available_filters.has_key?(field) | |
297 | label ||= field.gsub(/\_id$/, "") |
|
297 | label ||= field.gsub(/\_id$/, "") | |
@@ -303,17 +303,17 class Query < ActiveRecord::Base | |||||
303 | @available_columns += (project ? |
|
303 | @available_columns += (project ? | |
304 | project.all_issue_custom_fields : |
|
304 | project.all_issue_custom_fields : | |
305 | IssueCustomField.find(:all) |
|
305 | IssueCustomField.find(:all) | |
306 |
).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
306 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } | |
307 | end |
|
307 | end | |
308 |
|
308 | |||
309 | def self.available_columns=(v) |
|
309 | def self.available_columns=(v) | |
310 | self.available_columns = (v) |
|
310 | self.available_columns = (v) | |
311 | end |
|
311 | end | |
312 |
|
312 | |||
313 | def self.add_available_column(column) |
|
313 | def self.add_available_column(column) | |
314 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
314 | self.available_columns << (column) if column.is_a?(QueryColumn) | |
315 | end |
|
315 | end | |
316 |
|
316 | |||
317 | # Returns an array of columns that can be used to group the results |
|
317 | # Returns an array of columns that can be used to group the results | |
318 | def groupable_columns |
|
318 | def groupable_columns | |
319 | available_columns.select {|c| c.groupable} |
|
319 | available_columns.select {|c| c.groupable} | |
@@ -326,7 +326,7 class Query < ActiveRecord::Base | |||||
326 | h |
|
326 | h | |
327 | }) |
|
327 | }) | |
328 | end |
|
328 | end | |
329 |
|
329 | |||
330 | def columns |
|
330 | def columns | |
331 | if has_default_columns? |
|
331 | if has_default_columns? | |
332 | available_columns.select do |c| |
|
332 | available_columns.select do |c| | |
@@ -338,7 +338,7 class Query < ActiveRecord::Base | |||||
338 | column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact |
|
338 | column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact | |
339 | end |
|
339 | end | |
340 | end |
|
340 | end | |
341 |
|
341 | |||
342 | def column_names=(names) |
|
342 | def column_names=(names) | |
343 | if names |
|
343 | if names | |
344 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
344 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } | |
@@ -350,15 +350,15 class Query < ActiveRecord::Base | |||||
350 | end |
|
350 | end | |
351 | write_attribute(:column_names, names) |
|
351 | write_attribute(:column_names, names) | |
352 | end |
|
352 | end | |
353 |
|
353 | |||
354 | def has_column?(column) |
|
354 | def has_column?(column) | |
355 | column_names && column_names.include?(column.name) |
|
355 | column_names && column_names.include?(column.name) | |
356 | end |
|
356 | end | |
357 |
|
357 | |||
358 | def has_default_columns? |
|
358 | def has_default_columns? | |
359 | column_names.nil? || column_names.empty? |
|
359 | column_names.nil? || column_names.empty? | |
360 | end |
|
360 | end | |
361 |
|
361 | |||
362 | def sort_criteria=(arg) |
|
362 | def sort_criteria=(arg) | |
363 | c = [] |
|
363 | c = [] | |
364 | if arg.is_a?(Hash) |
|
364 | if arg.is_a?(Hash) | |
@@ -367,19 +367,19 class Query < ActiveRecord::Base | |||||
367 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} |
|
367 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} | |
368 | write_attribute(:sort_criteria, c) |
|
368 | write_attribute(:sort_criteria, c) | |
369 | end |
|
369 | end | |
370 |
|
370 | |||
371 | def sort_criteria |
|
371 | def sort_criteria | |
372 | read_attribute(:sort_criteria) || [] |
|
372 | read_attribute(:sort_criteria) || [] | |
373 | end |
|
373 | end | |
374 |
|
374 | |||
375 | def sort_criteria_key(arg) |
|
375 | def sort_criteria_key(arg) | |
376 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
376 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first | |
377 | end |
|
377 | end | |
378 |
|
378 | |||
379 | def sort_criteria_order(arg) |
|
379 | def sort_criteria_order(arg) | |
380 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
380 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last | |
381 | end |
|
381 | end | |
382 |
|
382 | |||
383 | # Returns the SQL sort order that should be prepended for grouping |
|
383 | # Returns the SQL sort order that should be prepended for grouping | |
384 | def group_by_sort_order |
|
384 | def group_by_sort_order | |
385 | if grouped? && (column = group_by_column) |
|
385 | if grouped? && (column = group_by_column) | |
@@ -388,20 +388,20 class Query < ActiveRecord::Base | |||||
388 | "#{column.sortable} #{column.default_order}" |
|
388 | "#{column.sortable} #{column.default_order}" | |
389 | end |
|
389 | end | |
390 | end |
|
390 | end | |
391 |
|
391 | |||
392 | # Returns true if the query is a grouped query |
|
392 | # Returns true if the query is a grouped query | |
393 | def grouped? |
|
393 | def grouped? | |
394 | !group_by_column.nil? |
|
394 | !group_by_column.nil? | |
395 | end |
|
395 | end | |
396 |
|
396 | |||
397 | def group_by_column |
|
397 | def group_by_column | |
398 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
398 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} | |
399 | end |
|
399 | end | |
400 |
|
400 | |||
401 | def group_by_statement |
|
401 | def group_by_statement | |
402 | group_by_column.try(:groupable) |
|
402 | group_by_column.try(:groupable) | |
403 | end |
|
403 | end | |
404 |
|
404 | |||
405 | def project_statement |
|
405 | def project_statement | |
406 | project_clauses = [] |
|
406 | project_clauses = [] | |
407 | if project && !@project.descendants.active.empty? |
|
407 | if project && !@project.descendants.active.empty? | |
@@ -435,12 +435,12 class Query < ActiveRecord::Base | |||||
435 | v = values_for(field).clone |
|
435 | v = values_for(field).clone | |
436 | next unless v and !v.empty? |
|
436 | next unless v and !v.empty? | |
437 | operator = operator_for(field) |
|
437 | operator = operator_for(field) | |
438 |
|
438 | |||
439 | # "me" value subsitution |
|
439 | # "me" value subsitution | |
440 | if %w(assigned_to_id author_id watcher_id).include?(field) |
|
440 | if %w(assigned_to_id author_id watcher_id).include?(field) | |
441 | v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me") |
|
441 | v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me") | |
442 | end |
|
442 | end | |
443 |
|
443 | |||
444 | sql = '' |
|
444 | sql = '' | |
445 | if field =~ /^cf_(\d+)$/ |
|
445 | if field =~ /^cf_(\d+)$/ | |
446 | # custom field |
|
446 | # custom field | |
@@ -472,7 +472,7 class Query < ActiveRecord::Base | |||||
472 | end |
|
472 | end | |
473 | user_ids.flatten.uniq.compact |
|
473 | user_ids.flatten.uniq.compact | |
474 | }.sort.collect(&:to_s) |
|
474 | }.sort.collect(&:to_s) | |
475 |
|
475 | |||
476 | sql << '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
476 | sql << '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' | |
477 |
|
477 | |||
478 | elsif field == "assigned_to_role" # named field |
|
478 | elsif field == "assigned_to_role" # named field | |
@@ -486,14 +486,14 class Query < ActiveRecord::Base | |||||
486 | roles = Role.givable.find_all_by_id(v) |
|
486 | roles = Role.givable.find_all_by_id(v) | |
487 | end |
|
487 | end | |
488 | roles ||= [] |
|
488 | roles ||= [] | |
489 |
|
489 | |||
490 | members_of_roles = roles.inject([]) {|user_ids, role| |
|
490 | members_of_roles = roles.inject([]) {|user_ids, role| | |
491 | if role && role.members |
|
491 | if role && role.members | |
492 | user_ids << role.members.collect(&:user_id) |
|
492 | user_ids << role.members.collect(&:user_id) | |
493 | end |
|
493 | end | |
494 | user_ids.flatten.uniq.compact |
|
494 | user_ids.flatten.uniq.compact | |
495 | }.sort.collect(&:to_s) |
|
495 | }.sort.collect(&:to_s) | |
496 |
|
496 | |||
497 | sql << '(' + sql_for_field("assigned_to_id", operator, members_of_roles, Issue.table_name, "assigned_to_id", false) + ')' |
|
497 | sql << '(' + sql_for_field("assigned_to_id", operator, members_of_roles, Issue.table_name, "assigned_to_id", false) + ')' | |
498 | else |
|
498 | else | |
499 | # regular field |
|
499 | # regular field | |
@@ -502,22 +502,22 class Query < ActiveRecord::Base | |||||
502 | sql << '(' + sql_for_field(field, operator, v, db_table, db_field) + ')' |
|
502 | sql << '(' + sql_for_field(field, operator, v, db_table, db_field) + ')' | |
503 | end |
|
503 | end | |
504 | filters_clauses << sql |
|
504 | filters_clauses << sql | |
505 |
|
505 | |||
506 | end if filters and valid? |
|
506 | end if filters and valid? | |
507 |
|
507 | |||
508 | filters_clauses << project_statement |
|
508 | filters_clauses << project_statement | |
509 | filters_clauses.reject!(&:blank?) |
|
509 | filters_clauses.reject!(&:blank?) | |
510 |
|
510 | |||
511 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
511 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil | |
512 | end |
|
512 | end | |
513 |
|
513 | |||
514 | # Returns the issue count |
|
514 | # Returns the issue count | |
515 | def issue_count |
|
515 | def issue_count | |
516 | Issue.count(:include => [:status, :project], :conditions => statement) |
|
516 | Issue.count(:include => [:status, :project], :conditions => statement) | |
517 | rescue ::ActiveRecord::StatementInvalid => e |
|
517 | rescue ::ActiveRecord::StatementInvalid => e | |
518 | raise StatementInvalid.new(e.message) |
|
518 | raise StatementInvalid.new(e.message) | |
519 | end |
|
519 | end | |
520 |
|
520 | |||
521 | # Returns the issue count by group or nil if query is not grouped |
|
521 | # Returns the issue count by group or nil if query is not grouped | |
522 | def issue_count_by_group |
|
522 | def issue_count_by_group | |
523 | r = nil |
|
523 | r = nil | |
@@ -537,13 +537,13 class Query < ActiveRecord::Base | |||||
537 | rescue ::ActiveRecord::StatementInvalid => e |
|
537 | rescue ::ActiveRecord::StatementInvalid => e | |
538 | raise StatementInvalid.new(e.message) |
|
538 | raise StatementInvalid.new(e.message) | |
539 | end |
|
539 | end | |
540 |
|
540 | |||
541 | # Returns the issues |
|
541 | # Returns the issues | |
542 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
542 | # Valid options are :order, :offset, :limit, :include, :conditions | |
543 | def issues(options={}) |
|
543 | def issues(options={}) | |
544 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
544 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') | |
545 | order_option = nil if order_option.blank? |
|
545 | order_option = nil if order_option.blank? | |
546 |
|
546 | |||
547 | Issue.visible.find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, |
|
547 | Issue.visible.find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, | |
548 | :conditions => Query.merge_conditions(statement, options[:conditions]), |
|
548 | :conditions => Query.merge_conditions(statement, options[:conditions]), | |
549 | :order => order_option, |
|
549 | :order => order_option, | |
@@ -564,7 +564,7 class Query < ActiveRecord::Base | |||||
564 | rescue ::ActiveRecord::StatementInvalid => e |
|
564 | rescue ::ActiveRecord::StatementInvalid => e | |
565 | raise StatementInvalid.new(e.message) |
|
565 | raise StatementInvalid.new(e.message) | |
566 | end |
|
566 | end | |
567 |
|
567 | |||
568 | # Returns the versions |
|
568 | # Returns the versions | |
569 | # Valid options are :conditions |
|
569 | # Valid options are :conditions | |
570 | def versions(options={}) |
|
570 | def versions(options={}) | |
@@ -573,9 +573,9 class Query < ActiveRecord::Base | |||||
573 | rescue ::ActiveRecord::StatementInvalid => e |
|
573 | rescue ::ActiveRecord::StatementInvalid => e | |
574 | raise StatementInvalid.new(e.message) |
|
574 | raise StatementInvalid.new(e.message) | |
575 | end |
|
575 | end | |
576 |
|
576 | |||
577 | private |
|
577 | private | |
578 |
|
578 | |||
579 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
579 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ | |
580 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
580 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) | |
581 | sql = '' |
|
581 | sql = '' | |
@@ -632,13 +632,13 class Query < ActiveRecord::Base | |||||
632 | when "!~" |
|
632 | when "!~" | |
633 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
633 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" | |
634 | end |
|
634 | end | |
635 |
|
635 | |||
636 | return sql |
|
636 | return sql | |
637 | end |
|
637 | end | |
638 |
|
638 | |||
639 | def add_custom_fields_filters(custom_fields) |
|
639 | def add_custom_fields_filters(custom_fields) | |
640 | @available_filters ||= {} |
|
640 | @available_filters ||= {} | |
641 |
|
641 | |||
642 | custom_fields.select(&:is_filter?).each do |field| |
|
642 | custom_fields.select(&:is_filter?).each do |field| | |
643 | case field.field_format |
|
643 | case field.field_format | |
644 | when "text" |
|
644 | when "text" | |
@@ -658,7 +658,7 class Query < ActiveRecord::Base | |||||
658 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) |
|
658 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) | |
659 | end |
|
659 | end | |
660 | end |
|
660 | end | |
661 |
|
661 | |||
662 | # Returns a SQL clause for a date or datetime field. |
|
662 | # Returns a SQL clause for a date or datetime field. | |
663 | def date_range_clause(table, field, from, to) |
|
663 | def date_range_clause(table, field, from, to) | |
664 | s = [] |
|
664 | s = [] |
General Comments 0
You need to be logged in to leave comments.
Login now