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