@@ -1,382 +1,383 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class QueryColumn |
|
18 | class QueryColumn | |
19 | attr_accessor :name, :sortable, :default_order |
|
19 | attr_accessor :name, :sortable, :default_order | |
20 | include GLoc |
|
20 | include GLoc | |
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] | |
25 | self.default_order = options[:default_order] |
|
25 | self.default_order = options[:default_order] | |
26 | end |
|
26 | end | |
27 |
|
27 | |||
28 | def caption |
|
28 | def caption | |
29 | set_language_if_valid(User.current.language) |
|
29 | set_language_if_valid(User.current.language) | |
30 | l("field_#{name}") |
|
30 | l("field_#{name}") | |
31 | end |
|
31 | end | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | class QueryCustomFieldColumn < QueryColumn |
|
34 | class QueryCustomFieldColumn < QueryColumn | |
35 |
|
35 | |||
36 | def initialize(custom_field) |
|
36 | def initialize(custom_field) | |
37 | self.name = "cf_#{custom_field.id}".to_sym |
|
37 | self.name = "cf_#{custom_field.id}".to_sym | |
38 | self.sortable = false |
|
38 | self.sortable = false | |
39 | @cf = custom_field |
|
39 | @cf = custom_field | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def caption |
|
42 | def caption | |
43 | @cf.name |
|
43 | @cf.name | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def custom_field |
|
46 | def custom_field | |
47 | @cf |
|
47 | @cf | |
48 | end |
|
48 | end | |
49 | end |
|
49 | end | |
50 |
|
50 | |||
51 | class Query < ActiveRecord::Base |
|
51 | class Query < ActiveRecord::Base | |
52 | belongs_to :project |
|
52 | belongs_to :project | |
53 | belongs_to :user |
|
53 | belongs_to :user | |
54 | serialize :filters |
|
54 | serialize :filters | |
55 | serialize :column_names |
|
55 | serialize :column_names | |
56 |
|
56 | |||
57 | attr_protected :project_id, :user_id |
|
57 | attr_protected :project_id, :user_id | |
58 |
|
58 | |||
59 | validates_presence_of :name, :on => :save |
|
59 | validates_presence_of :name, :on => :save | |
60 | validates_length_of :name, :maximum => 255 |
|
60 | validates_length_of :name, :maximum => 255 | |
61 |
|
61 | |||
62 | @@operators = { "=" => :label_equals, |
|
62 | @@operators = { "=" => :label_equals, | |
63 | "!" => :label_not_equals, |
|
63 | "!" => :label_not_equals, | |
64 | "o" => :label_open_issues, |
|
64 | "o" => :label_open_issues, | |
65 | "c" => :label_closed_issues, |
|
65 | "c" => :label_closed_issues, | |
66 | "!*" => :label_none, |
|
66 | "!*" => :label_none, | |
67 | "*" => :label_all, |
|
67 | "*" => :label_all, | |
68 | ">=" => '>=', |
|
68 | ">=" => '>=', | |
69 | "<=" => '<=', |
|
69 | "<=" => '<=', | |
70 | "<t+" => :label_in_less_than, |
|
70 | "<t+" => :label_in_less_than, | |
71 | ">t+" => :label_in_more_than, |
|
71 | ">t+" => :label_in_more_than, | |
72 | "t+" => :label_in, |
|
72 | "t+" => :label_in, | |
73 | "t" => :label_today, |
|
73 | "t" => :label_today, | |
74 | "w" => :label_this_week, |
|
74 | "w" => :label_this_week, | |
75 | ">t-" => :label_less_than_ago, |
|
75 | ">t-" => :label_less_than_ago, | |
76 | "<t-" => :label_more_than_ago, |
|
76 | "<t-" => :label_more_than_ago, | |
77 | "t-" => :label_ago, |
|
77 | "t-" => :label_ago, | |
78 | "~" => :label_contains, |
|
78 | "~" => :label_contains, | |
79 | "!~" => :label_not_contains } |
|
79 | "!~" => :label_not_contains } | |
80 |
|
80 | |||
81 | cattr_reader :operators |
|
81 | cattr_reader :operators | |
82 |
|
82 | |||
83 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
83 | @@operators_by_filter_type = { :list => [ "=", "!" ], | |
84 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
84 | :list_status => [ "o", "=", "!", "c", "*" ], | |
85 | :list_optional => [ "=", "!", "!*", "*" ], |
|
85 | :list_optional => [ "=", "!", "!*", "*" ], | |
86 | :list_subprojects => [ "*", "!*", "=" ], |
|
86 | :list_subprojects => [ "*", "!*", "=" ], | |
87 | :date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ], |
|
87 | :date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ], | |
88 | :date_past => [ ">t-", "<t-", "t-", "t", "w" ], |
|
88 | :date_past => [ ">t-", "<t-", "t-", "t", "w" ], | |
89 | :string => [ "=", "~", "!", "!~" ], |
|
89 | :string => [ "=", "~", "!", "!~" ], | |
90 | :text => [ "~", "!~" ], |
|
90 | :text => [ "~", "!~" ], | |
91 | :integer => [ "=", ">=", "<=" ] } |
|
91 | :integer => [ "=", ">=", "<=", "!*", "*" ] } | |
92 |
|
92 | |||
93 | cattr_reader :operators_by_filter_type |
|
93 | cattr_reader :operators_by_filter_type | |
94 |
|
94 | |||
95 | @@available_columns = [ |
|
95 | @@available_columns = [ | |
96 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position"), |
|
96 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position"), | |
97 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position"), |
|
97 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position"), | |
98 | QueryColumn.new(:priority, :sortable => "#{Enumeration.table_name}.position", :default_order => 'desc'), |
|
98 | QueryColumn.new(:priority, :sortable => "#{Enumeration.table_name}.position", :default_order => 'desc'), | |
99 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
99 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), | |
100 | QueryColumn.new(:author), |
|
100 | QueryColumn.new(:author), | |
101 | QueryColumn.new(:assigned_to, :sortable => "#{User.table_name}.lastname"), |
|
101 | QueryColumn.new(:assigned_to, :sortable => "#{User.table_name}.lastname"), | |
102 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
102 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), | |
103 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name"), |
|
103 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name"), | |
104 | QueryColumn.new(:fixed_version, :sortable => "#{Version.table_name}.effective_date", :default_order => 'desc'), |
|
104 | QueryColumn.new(:fixed_version, :sortable => "#{Version.table_name}.effective_date", :default_order => 'desc'), | |
105 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
105 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), | |
106 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
106 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), | |
107 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
107 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), | |
108 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio"), |
|
108 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio"), | |
109 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
109 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), | |
110 | ] |
|
110 | ] | |
111 | cattr_reader :available_columns |
|
111 | cattr_reader :available_columns | |
112 |
|
112 | |||
113 | def initialize(attributes = nil) |
|
113 | def initialize(attributes = nil) | |
114 | super attributes |
|
114 | super attributes | |
115 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
115 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } | |
116 | set_language_if_valid(User.current.language) |
|
116 | set_language_if_valid(User.current.language) | |
117 | end |
|
117 | end | |
118 |
|
118 | |||
119 | def after_initialize |
|
119 | def after_initialize | |
120 | # Store the fact that project is nil (used in #editable_by?) |
|
120 | # Store the fact that project is nil (used in #editable_by?) | |
121 | @is_for_all = project.nil? |
|
121 | @is_for_all = project.nil? | |
122 | end |
|
122 | end | |
123 |
|
123 | |||
124 | def validate |
|
124 | def validate | |
125 | filters.each_key do |field| |
|
125 | filters.each_key do |field| | |
126 | errors.add label_for(field), :activerecord_error_blank unless |
|
126 | errors.add label_for(field), :activerecord_error_blank unless | |
127 | # filter requires one or more values |
|
127 | # filter requires one or more values | |
128 | (values_for(field) and !values_for(field).first.blank?) or |
|
128 | (values_for(field) and !values_for(field).first.blank?) or | |
129 | # filter doesn't require any value |
|
129 | # filter doesn't require any value | |
130 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
130 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) | |
131 | end if filters |
|
131 | end if filters | |
132 | end |
|
132 | end | |
133 |
|
133 | |||
134 | def editable_by?(user) |
|
134 | def editable_by?(user) | |
135 | return false unless user |
|
135 | return false unless user | |
136 | # Admin can edit them all and regular users can edit their private queries |
|
136 | # Admin can edit them all and regular users can edit their private queries | |
137 | return true if user.admin? || (!is_public && self.user_id == user.id) |
|
137 | return true if user.admin? || (!is_public && self.user_id == user.id) | |
138 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
138 | # Members can not edit public queries that are for all project (only admin is allowed to) | |
139 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
139 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) | |
140 | end |
|
140 | end | |
141 |
|
141 | |||
142 | def available_filters |
|
142 | def available_filters | |
143 | return @available_filters if @available_filters |
|
143 | return @available_filters if @available_filters | |
144 |
|
144 | |||
145 | trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
145 | trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers | |
146 |
|
146 | |||
147 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
147 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, | |
148 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, |
|
148 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, | |
149 | "priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI'], :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
149 | "priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI'], :order => 'position').collect{|s| [s.name, s.id.to_s] } }, | |
150 | "subject" => { :type => :text, :order => 8 }, |
|
150 | "subject" => { :type => :text, :order => 8 }, | |
151 | "created_on" => { :type => :date_past, :order => 9 }, |
|
151 | "created_on" => { :type => :date_past, :order => 9 }, | |
152 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
152 | "updated_on" => { :type => :date_past, :order => 10 }, | |
153 | "start_date" => { :type => :date, :order => 11 }, |
|
153 | "start_date" => { :type => :date, :order => 11 }, | |
154 | "due_date" => { :type => :date, :order => 12 }, |
|
154 | "due_date" => { :type => :date, :order => 12 }, | |
155 |
" |
|
155 | "estimated_hours" => { :type => :integer, :order => 13 }, | |
|
156 | "done_ratio" => { :type => :integer, :order => 14 }} | |||
156 |
|
157 | |||
157 | user_values = [] |
|
158 | user_values = [] | |
158 | user_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
159 | user_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
159 | if project |
|
160 | if project | |
160 | user_values += project.users.sort.collect{|s| [s.name, s.id.to_s] } |
|
161 | user_values += project.users.sort.collect{|s| [s.name, s.id.to_s] } | |
161 | else |
|
162 | else | |
162 | # members of the user's projects |
|
163 | # members of the user's projects | |
163 | user_values += User.current.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] } |
|
164 | user_values += User.current.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] } | |
164 | end |
|
165 | end | |
165 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty? |
|
166 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty? | |
166 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty? |
|
167 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty? | |
167 |
|
168 | |||
168 | if project |
|
169 | if project | |
169 | # project specific filters |
|
170 | # project specific filters | |
170 | unless @project.issue_categories.empty? |
|
171 | unless @project.issue_categories.empty? | |
171 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } } |
|
172 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } } | |
172 | end |
|
173 | end | |
173 | unless @project.versions.empty? |
|
174 | unless @project.versions.empty? | |
174 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } } |
|
175 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } } | |
175 | end |
|
176 | end | |
176 | unless @project.active_children.empty? |
|
177 | unless @project.active_children.empty? | |
177 | @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } } |
|
178 | @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } } | |
178 | end |
|
179 | end | |
179 | add_custom_fields_filters(@project.all_issue_custom_fields) |
|
180 | add_custom_fields_filters(@project.all_issue_custom_fields) | |
180 | else |
|
181 | else | |
181 | # global filters for cross project issue list |
|
182 | # global filters for cross project issue list | |
182 | add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true})) |
|
183 | add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true})) | |
183 | end |
|
184 | end | |
184 | @available_filters |
|
185 | @available_filters | |
185 | end |
|
186 | end | |
186 |
|
187 | |||
187 | def add_filter(field, operator, values) |
|
188 | def add_filter(field, operator, values) | |
188 | # values must be an array |
|
189 | # values must be an array | |
189 | return unless values and values.is_a? Array # and !values.first.empty? |
|
190 | return unless values and values.is_a? Array # and !values.first.empty? | |
190 | # check if field is defined as an available filter |
|
191 | # check if field is defined as an available filter | |
191 | if available_filters.has_key? field |
|
192 | if available_filters.has_key? field | |
192 | filter_options = available_filters[field] |
|
193 | filter_options = available_filters[field] | |
193 | # check if operator is allowed for that filter |
|
194 | # check if operator is allowed for that filter | |
194 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
195 | #if @@operators_by_filter_type[filter_options[:type]].include? operator | |
195 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
196 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) | |
196 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
197 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator | |
197 | #end |
|
198 | #end | |
198 | filters[field] = {:operator => operator, :values => values } |
|
199 | filters[field] = {:operator => operator, :values => values } | |
199 | end |
|
200 | end | |
200 | end |
|
201 | end | |
201 |
|
202 | |||
202 | def add_short_filter(field, expression) |
|
203 | def add_short_filter(field, expression) | |
203 | return unless expression |
|
204 | return unless expression | |
204 | parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first |
|
205 | parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first | |
205 | add_filter field, (parms[0] || "="), [parms[1] || ""] |
|
206 | add_filter field, (parms[0] || "="), [parms[1] || ""] | |
206 | end |
|
207 | end | |
207 |
|
208 | |||
208 | def has_filter?(field) |
|
209 | def has_filter?(field) | |
209 | filters and filters[field] |
|
210 | filters and filters[field] | |
210 | end |
|
211 | end | |
211 |
|
212 | |||
212 | def operator_for(field) |
|
213 | def operator_for(field) | |
213 | has_filter?(field) ? filters[field][:operator] : nil |
|
214 | has_filter?(field) ? filters[field][:operator] : nil | |
214 | end |
|
215 | end | |
215 |
|
216 | |||
216 | def values_for(field) |
|
217 | def values_for(field) | |
217 | has_filter?(field) ? filters[field][:values] : nil |
|
218 | has_filter?(field) ? filters[field][:values] : nil | |
218 | end |
|
219 | end | |
219 |
|
220 | |||
220 | def label_for(field) |
|
221 | def label_for(field) | |
221 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
222 | label = available_filters[field][:name] if available_filters.has_key?(field) | |
222 | label ||= field.gsub(/\_id$/, "") |
|
223 | label ||= field.gsub(/\_id$/, "") | |
223 | end |
|
224 | end | |
224 |
|
225 | |||
225 | def available_columns |
|
226 | def available_columns | |
226 | return @available_columns if @available_columns |
|
227 | return @available_columns if @available_columns | |
227 | @available_columns = Query.available_columns |
|
228 | @available_columns = Query.available_columns | |
228 | @available_columns += (project ? |
|
229 | @available_columns += (project ? | |
229 | project.all_issue_custom_fields : |
|
230 | project.all_issue_custom_fields : | |
230 | IssueCustomField.find(:all, :conditions => {:is_for_all => true}) |
|
231 | IssueCustomField.find(:all, :conditions => {:is_for_all => true}) | |
231 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
232 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } | |
232 | end |
|
233 | end | |
233 |
|
234 | |||
234 | def columns |
|
235 | def columns | |
235 | if has_default_columns? |
|
236 | if has_default_columns? | |
236 | available_columns.select {|c| Setting.issue_list_default_columns.include?(c.name.to_s) } |
|
237 | available_columns.select {|c| Setting.issue_list_default_columns.include?(c.name.to_s) } | |
237 | else |
|
238 | else | |
238 | # preserve the column_names order |
|
239 | # preserve the column_names order | |
239 | column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact |
|
240 | column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact | |
240 | end |
|
241 | end | |
241 | end |
|
242 | end | |
242 |
|
243 | |||
243 | def column_names=(names) |
|
244 | def column_names=(names) | |
244 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } if names |
|
245 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } if names | |
245 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } if names |
|
246 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } if names | |
246 | write_attribute(:column_names, names) |
|
247 | write_attribute(:column_names, names) | |
247 | end |
|
248 | end | |
248 |
|
249 | |||
249 | def has_column?(column) |
|
250 | def has_column?(column) | |
250 | column_names && column_names.include?(column.name) |
|
251 | column_names && column_names.include?(column.name) | |
251 | end |
|
252 | end | |
252 |
|
253 | |||
253 | def has_default_columns? |
|
254 | def has_default_columns? | |
254 | column_names.nil? || column_names.empty? |
|
255 | column_names.nil? || column_names.empty? | |
255 | end |
|
256 | end | |
256 |
|
257 | |||
257 | def statement |
|
258 | def statement | |
258 | # project/subprojects clause |
|
259 | # project/subprojects clause | |
259 | project_clauses = [] |
|
260 | project_clauses = [] | |
260 | if project && !@project.active_children.empty? |
|
261 | if project && !@project.active_children.empty? | |
261 | ids = [project.id] |
|
262 | ids = [project.id] | |
262 | if has_filter?("subproject_id") |
|
263 | if has_filter?("subproject_id") | |
263 | case operator_for("subproject_id") |
|
264 | case operator_for("subproject_id") | |
264 | when '=' |
|
265 | when '=' | |
265 | # include the selected subprojects |
|
266 | # include the selected subprojects | |
266 | ids += values_for("subproject_id").each(&:to_i) |
|
267 | ids += values_for("subproject_id").each(&:to_i) | |
267 | when '!*' |
|
268 | when '!*' | |
268 | # main project only |
|
269 | # main project only | |
269 | else |
|
270 | else | |
270 | # all subprojects |
|
271 | # all subprojects | |
271 | ids += project.child_ids |
|
272 | ids += project.child_ids | |
272 | end |
|
273 | end | |
273 | elsif Setting.display_subprojects_issues? |
|
274 | elsif Setting.display_subprojects_issues? | |
274 | ids += project.child_ids |
|
275 | ids += project.child_ids | |
275 | end |
|
276 | end | |
276 | project_clauses << "#{Issue.table_name}.project_id IN (%s)" % ids.join(',') |
|
277 | project_clauses << "#{Issue.table_name}.project_id IN (%s)" % ids.join(',') | |
277 | elsif project |
|
278 | elsif project | |
278 | project_clauses << "#{Issue.table_name}.project_id = %d" % project.id |
|
279 | project_clauses << "#{Issue.table_name}.project_id = %d" % project.id | |
279 | end |
|
280 | end | |
280 | project_clauses << Project.visible_by(User.current) |
|
281 | project_clauses << Project.visible_by(User.current) | |
281 |
|
282 | |||
282 | # filters clauses |
|
283 | # filters clauses | |
283 | filters_clauses = [] |
|
284 | filters_clauses = [] | |
284 | filters.each_key do |field| |
|
285 | filters.each_key do |field| | |
285 | next if field == "subproject_id" |
|
286 | next if field == "subproject_id" | |
286 | v = values_for(field).clone |
|
287 | v = values_for(field).clone | |
287 | next unless v and !v.empty? |
|
288 | next unless v and !v.empty? | |
288 |
|
289 | |||
289 | sql = '' |
|
290 | sql = '' | |
290 | is_custom_filter = false |
|
291 | is_custom_filter = false | |
291 | if field =~ /^cf_(\d+)$/ |
|
292 | if field =~ /^cf_(\d+)$/ | |
292 | # custom field |
|
293 | # custom field | |
293 | db_table = CustomValue.table_name |
|
294 | db_table = CustomValue.table_name | |
294 | db_field = 'value' |
|
295 | db_field = 'value' | |
295 | is_custom_filter = true |
|
296 | is_custom_filter = true | |
296 | sql << "#{Issue.table_name}.id IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} WHERE " |
|
297 | sql << "#{Issue.table_name}.id IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} WHERE " | |
297 | else |
|
298 | else | |
298 | # regular field |
|
299 | # regular field | |
299 | db_table = Issue.table_name |
|
300 | db_table = Issue.table_name | |
300 | db_field = field |
|
301 | db_field = field | |
301 | sql << '(' |
|
302 | sql << '(' | |
302 | end |
|
303 | end | |
303 |
|
304 | |||
304 | # "me" value subsitution |
|
305 | # "me" value subsitution | |
305 | if %w(assigned_to_id author_id).include?(field) |
|
306 | if %w(assigned_to_id author_id).include?(field) | |
306 | v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me") |
|
307 | v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me") | |
307 | end |
|
308 | end | |
308 |
|
309 | |||
309 | case operator_for field |
|
310 | case operator_for field | |
310 | when "=" |
|
311 | when "=" | |
311 | sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
312 | sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" | |
312 | when "!" |
|
313 | when "!" | |
313 | sql = sql + "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
314 | sql = sql + "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" | |
314 | when "!*" |
|
315 | when "!*" | |
315 | sql = sql + "#{db_table}.#{db_field} IS NULL" |
|
316 | sql = sql + "#{db_table}.#{db_field} IS NULL" | |
316 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
317 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter | |
317 | when "*" |
|
318 | when "*" | |
318 | sql = sql + "#{db_table}.#{db_field} IS NOT NULL" |
|
319 | sql = sql + "#{db_table}.#{db_field} IS NOT NULL" | |
319 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
320 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter | |
320 | when ">=" |
|
321 | when ">=" | |
321 | sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}" |
|
322 | sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}" | |
322 | when "<=" |
|
323 | when "<=" | |
323 | sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}" |
|
324 | sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}" | |
324 | when "o" |
|
325 | when "o" | |
325 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id" |
|
326 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id" | |
326 | when "c" |
|
327 | when "c" | |
327 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id" |
|
328 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id" | |
328 | when ">t-" |
|
329 | when ">t-" | |
329 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)] |
|
330 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)] | |
330 | when "<t-" |
|
331 | when "<t-" | |
331 | sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time) |
|
332 | sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time) | |
332 | when "t-" |
|
333 | when "t-" | |
333 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)] |
|
334 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)] | |
334 | when ">t+" |
|
335 | when ">t+" | |
335 | sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time) |
|
336 | sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time) | |
336 | when "<t+" |
|
337 | when "<t+" | |
337 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] |
|
338 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] | |
338 | when "t+" |
|
339 | when "t+" | |
339 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] |
|
340 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] | |
340 | when "t" |
|
341 | when "t" | |
341 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)] |
|
342 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)] | |
342 | when "w" |
|
343 | when "w" | |
343 | from = l(:general_first_day_of_week) == '7' ? |
|
344 | from = l(:general_first_day_of_week) == '7' ? | |
344 | # week starts on sunday |
|
345 | # week starts on sunday | |
345 | ((Date.today.cwday == 7) ? Time.now.at_beginning_of_day : Time.now.at_beginning_of_week - 1.day) : |
|
346 | ((Date.today.cwday == 7) ? Time.now.at_beginning_of_day : Time.now.at_beginning_of_week - 1.day) : | |
346 | # week starts on monday (Rails default) |
|
347 | # week starts on monday (Rails default) | |
347 | Time.now.at_beginning_of_week |
|
348 | Time.now.at_beginning_of_week | |
348 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(from), connection.quoted_date(from + 7.days)] |
|
349 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(from), connection.quoted_date(from + 7.days)] | |
349 | when "~" |
|
350 | when "~" | |
350 | sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'" |
|
351 | sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'" | |
351 | when "!~" |
|
352 | when "!~" | |
352 | sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'" |
|
353 | sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'" | |
353 | end |
|
354 | end | |
354 | sql << ')' |
|
355 | sql << ')' | |
355 | filters_clauses << sql |
|
356 | filters_clauses << sql | |
356 | end if filters and valid? |
|
357 | end if filters and valid? | |
357 |
|
358 | |||
358 | (project_clauses + filters_clauses).join(' AND ') |
|
359 | (project_clauses + filters_clauses).join(' AND ') | |
359 | end |
|
360 | end | |
360 |
|
361 | |||
361 | private |
|
362 | private | |
362 |
|
363 | |||
363 | def add_custom_fields_filters(custom_fields) |
|
364 | def add_custom_fields_filters(custom_fields) | |
364 | @available_filters ||= {} |
|
365 | @available_filters ||= {} | |
365 |
|
366 | |||
366 | custom_fields.select(&:is_filter?).each do |field| |
|
367 | custom_fields.select(&:is_filter?).each do |field| | |
367 | case field.field_format |
|
368 | case field.field_format | |
368 | when "text" |
|
369 | when "text" | |
369 | options = { :type => :text, :order => 20 } |
|
370 | options = { :type => :text, :order => 20 } | |
370 | when "list" |
|
371 | when "list" | |
371 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
372 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} | |
372 | when "date" |
|
373 | when "date" | |
373 | options = { :type => :date, :order => 20 } |
|
374 | options = { :type => :date, :order => 20 } | |
374 | when "bool" |
|
375 | when "bool" | |
375 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
376 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } | |
376 | else |
|
377 | else | |
377 | options = { :type => :string, :order => 20 } |
|
378 | options = { :type => :string, :order => 20 } | |
378 | end |
|
379 | end | |
379 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) |
|
380 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) | |
380 | end |
|
381 | end | |
381 | end |
|
382 | end | |
382 | end |
|
383 | end |
@@ -1,159 +1,173 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 |
|
19 | |||
20 | class QueryTest < Test::Unit::TestCase |
|
20 | class QueryTest < Test::Unit::TestCase | |
21 | fixtures :projects, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries |
|
21 | fixtures :projects, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries | |
22 |
|
22 | |||
23 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries |
|
23 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries | |
24 | query = Query.new(:project => nil, :name => '_') |
|
24 | query = Query.new(:project => nil, :name => '_') | |
25 | assert query.available_filters.has_key?('cf_1') |
|
25 | assert query.available_filters.has_key?('cf_1') | |
26 | assert !query.available_filters.has_key?('cf_3') |
|
26 | assert !query.available_filters.has_key?('cf_3') | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
|
29 | def find_issues_with_query(query) | |||
|
30 | Issue.find :all, | |||
|
31 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], | |||
|
32 | :conditions => query.statement | |||
|
33 | end | |||
|
34 | ||||
29 | def test_query_with_multiple_custom_fields |
|
35 | def test_query_with_multiple_custom_fields | |
30 | query = Query.find(1) |
|
36 | query = Query.find(1) | |
31 | assert query.valid? |
|
37 | assert query.valid? | |
32 | assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") |
|
38 | assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") | |
33 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
39 | issues = find_issues_with_query(query) | |
34 | assert_equal 1, issues.length |
|
40 | assert_equal 1, issues.length | |
35 | assert_equal Issue.find(3), issues.first |
|
41 | assert_equal Issue.find(3), issues.first | |
36 | end |
|
42 | end | |
37 |
|
43 | |||
38 | def test_operator_none |
|
44 | def test_operator_none | |
39 | query = Query.new(:project => Project.find(1), :name => '_') |
|
45 | query = Query.new(:project => Project.find(1), :name => '_') | |
40 | query.add_filter('fixed_version_id', '!*', ['']) |
|
46 | query.add_filter('fixed_version_id', '!*', ['']) | |
41 | query.add_filter('cf_1', '!*', ['']) |
|
47 | query.add_filter('cf_1', '!*', ['']) | |
42 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") |
|
48 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") | |
43 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") |
|
49 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") | |
44 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
50 | find_issues_with_query(query) | |
45 | end |
|
51 | end | |
46 |
|
52 | |||
|
53 | def test_operator_none_for_integer | |||
|
54 | query = Query.new(:project => Project.find(1), :name => '_') | |||
|
55 | query.add_filter('estimated_hours', '!*', ['']) | |||
|
56 | issues = find_issues_with_query(query) | |||
|
57 | assert !issues.empty? | |||
|
58 | assert issues.all? {|i| !i.estimated_hours} | |||
|
59 | end | |||
|
60 | ||||
47 | def test_operator_all |
|
61 | def test_operator_all | |
48 | query = Query.new(:project => Project.find(1), :name => '_') |
|
62 | query = Query.new(:project => Project.find(1), :name => '_') | |
49 | query.add_filter('fixed_version_id', '*', ['']) |
|
63 | query.add_filter('fixed_version_id', '*', ['']) | |
50 | query.add_filter('cf_1', '*', ['']) |
|
64 | query.add_filter('cf_1', '*', ['']) | |
51 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") |
|
65 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") | |
52 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") |
|
66 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") | |
53 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
67 | find_issues_with_query(query) | |
54 | end |
|
68 | end | |
55 |
|
69 | |||
56 | def test_operator_greater_than |
|
70 | def test_operator_greater_than | |
57 | query = Query.new(:project => Project.find(1), :name => '_') |
|
71 | query = Query.new(:project => Project.find(1), :name => '_') | |
58 | query.add_filter('done_ratio', '>=', ['40']) |
|
72 | query.add_filter('done_ratio', '>=', ['40']) | |
59 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40") |
|
73 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40") | |
60 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
74 | find_issues_with_query(query) | |
61 | end |
|
75 | end | |
62 |
|
76 | |||
63 | def test_operator_in_more_than |
|
77 | def test_operator_in_more_than | |
64 | query = Query.new(:project => Project.find(1), :name => '_') |
|
78 | query = Query.new(:project => Project.find(1), :name => '_') | |
65 | query.add_filter('due_date', '>t+', ['15']) |
|
79 | query.add_filter('due_date', '>t+', ['15']) | |
66 | assert query.statement.include?("#{Issue.table_name}.due_date >=") |
|
80 | assert query.statement.include?("#{Issue.table_name}.due_date >=") | |
67 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
81 | find_issues_with_query(query) | |
68 | end |
|
82 | end | |
69 |
|
83 | |||
70 | def test_operator_in_less_than |
|
84 | def test_operator_in_less_than | |
71 | query = Query.new(:project => Project.find(1), :name => '_') |
|
85 | query = Query.new(:project => Project.find(1), :name => '_') | |
72 | query.add_filter('due_date', '<t+', ['15']) |
|
86 | query.add_filter('due_date', '<t+', ['15']) | |
73 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") |
|
87 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |
74 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
88 | find_issues_with_query(query) | |
75 | end |
|
89 | end | |
76 |
|
90 | |||
77 | def test_operator_today |
|
91 | def test_operator_today | |
78 | query = Query.new(:project => Project.find(1), :name => '_') |
|
92 | query = Query.new(:project => Project.find(1), :name => '_') | |
79 | query.add_filter('due_date', 't', ['']) |
|
93 | query.add_filter('due_date', 't', ['']) | |
80 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") |
|
94 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |
81 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
95 | find_issues_with_query(query) | |
82 | end |
|
96 | end | |
83 |
|
97 | |||
84 | def test_operator_this_week_on_date |
|
98 | def test_operator_this_week_on_date | |
85 | query = Query.new(:project => Project.find(1), :name => '_') |
|
99 | query = Query.new(:project => Project.find(1), :name => '_') | |
86 | query.add_filter('due_date', 'w', ['']) |
|
100 | query.add_filter('due_date', 'w', ['']) | |
87 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") |
|
101 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |
88 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
102 | find_issues_with_query(query) | |
89 | end |
|
103 | end | |
90 |
|
104 | |||
91 | def test_operator_this_week_on_datetime |
|
105 | def test_operator_this_week_on_datetime | |
92 | query = Query.new(:project => Project.find(1), :name => '_') |
|
106 | query = Query.new(:project => Project.find(1), :name => '_') | |
93 | query.add_filter('created_on', 'w', ['']) |
|
107 | query.add_filter('created_on', 'w', ['']) | |
94 | assert query.statement.include?("#{Issue.table_name}.created_on BETWEEN") |
|
108 | assert query.statement.include?("#{Issue.table_name}.created_on BETWEEN") | |
95 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
109 | find_issues_with_query(query) | |
96 | end |
|
110 | end | |
97 |
|
111 | |||
98 | def test_operator_contains |
|
112 | def test_operator_contains | |
99 | query = Query.new(:project => Project.find(1), :name => '_') |
|
113 | query = Query.new(:project => Project.find(1), :name => '_') | |
100 | query.add_filter('subject', '~', ['string']) |
|
114 | query.add_filter('subject', '~', ['string']) | |
101 | assert query.statement.include?("#{Issue.table_name}.subject LIKE '%string%'") |
|
115 | assert query.statement.include?("#{Issue.table_name}.subject LIKE '%string%'") | |
102 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
116 | find_issues_with_query(query) | |
103 | end |
|
117 | end | |
104 |
|
118 | |||
105 | def test_operator_does_not_contains |
|
119 | def test_operator_does_not_contains | |
106 | query = Query.new(:project => Project.find(1), :name => '_') |
|
120 | query = Query.new(:project => Project.find(1), :name => '_') | |
107 | query.add_filter('subject', '!~', ['string']) |
|
121 | query.add_filter('subject', '!~', ['string']) | |
108 | assert query.statement.include?("#{Issue.table_name}.subject NOT LIKE '%string%'") |
|
122 | assert query.statement.include?("#{Issue.table_name}.subject NOT LIKE '%string%'") | |
109 | issues = Issue.find :all,:include => [ :assigned_to, :status, :tracker, :project, :priority ], :conditions => query.statement |
|
123 | find_issues_with_query(query) | |
110 | end |
|
124 | end | |
111 |
|
125 | |||
112 | def test_default_columns |
|
126 | def test_default_columns | |
113 | q = Query.new |
|
127 | q = Query.new | |
114 | assert !q.columns.empty? |
|
128 | assert !q.columns.empty? | |
115 | end |
|
129 | end | |
116 |
|
130 | |||
117 | def test_set_column_names |
|
131 | def test_set_column_names | |
118 | q = Query.new |
|
132 | q = Query.new | |
119 | q.column_names = ['tracker', :subject, '', 'unknonw_column'] |
|
133 | q.column_names = ['tracker', :subject, '', 'unknonw_column'] | |
120 | assert_equal [:tracker, :subject], q.columns.collect {|c| c.name} |
|
134 | assert_equal [:tracker, :subject], q.columns.collect {|c| c.name} | |
121 | c = q.columns.first |
|
135 | c = q.columns.first | |
122 | assert q.has_column?(c) |
|
136 | assert q.has_column?(c) | |
123 | end |
|
137 | end | |
124 |
|
138 | |||
125 | def test_label_for |
|
139 | def test_label_for | |
126 | q = Query.new |
|
140 | q = Query.new | |
127 | assert_equal 'assigned_to', q.label_for('assigned_to_id') |
|
141 | assert_equal 'assigned_to', q.label_for('assigned_to_id') | |
128 | end |
|
142 | end | |
129 |
|
143 | |||
130 | def test_editable_by |
|
144 | def test_editable_by | |
131 | admin = User.find(1) |
|
145 | admin = User.find(1) | |
132 | manager = User.find(2) |
|
146 | manager = User.find(2) | |
133 | developer = User.find(3) |
|
147 | developer = User.find(3) | |
134 |
|
148 | |||
135 | # Public query on project 1 |
|
149 | # Public query on project 1 | |
136 | q = Query.find(1) |
|
150 | q = Query.find(1) | |
137 | assert q.editable_by?(admin) |
|
151 | assert q.editable_by?(admin) | |
138 | assert q.editable_by?(manager) |
|
152 | assert q.editable_by?(manager) | |
139 | assert !q.editable_by?(developer) |
|
153 | assert !q.editable_by?(developer) | |
140 |
|
154 | |||
141 | # Private query on project 1 |
|
155 | # Private query on project 1 | |
142 | q = Query.find(2) |
|
156 | q = Query.find(2) | |
143 | assert q.editable_by?(admin) |
|
157 | assert q.editable_by?(admin) | |
144 | assert !q.editable_by?(manager) |
|
158 | assert !q.editable_by?(manager) | |
145 | assert q.editable_by?(developer) |
|
159 | assert q.editable_by?(developer) | |
146 |
|
160 | |||
147 | # Private query for all projects |
|
161 | # Private query for all projects | |
148 | q = Query.find(3) |
|
162 | q = Query.find(3) | |
149 | assert q.editable_by?(admin) |
|
163 | assert q.editable_by?(admin) | |
150 | assert !q.editable_by?(manager) |
|
164 | assert !q.editable_by?(manager) | |
151 | assert q.editable_by?(developer) |
|
165 | assert q.editable_by?(developer) | |
152 |
|
166 | |||
153 | # Public query for all projects |
|
167 | # Public query for all projects | |
154 | q = Query.find(4) |
|
168 | q = Query.find(4) | |
155 | assert q.editable_by?(admin) |
|
169 | assert q.editable_by?(admin) | |
156 | assert !q.editable_by?(manager) |
|
170 | assert !q.editable_by?(manager) | |
157 | assert !q.editable_by?(developer) |
|
171 | assert !q.editable_by?(developer) | |
158 | end |
|
172 | end | |
159 | end |
|
173 | end |
General Comments 0
You need to be logged in to leave comments.
Login now