##// END OF EJS Templates
Allow global versions to be shown outside of a project for version custom fields (#23083)....
Jean-Philippe Lang -
r15154:adb9980728c0
parent child
Show More
@@ -1,818 +1,825
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 module Redmine
19 19 module FieldFormat
20 20 def self.add(name, klass)
21 21 all[name.to_s] = klass.instance
22 22 end
23 23
24 24 def self.delete(name)
25 25 all.delete(name.to_s)
26 26 end
27 27
28 28 def self.all
29 29 @formats ||= Hash.new(Base.instance)
30 30 end
31 31
32 32 def self.available_formats
33 33 all.keys
34 34 end
35 35
36 36 def self.find(name)
37 37 all[name.to_s]
38 38 end
39 39
40 40 # Return an array of custom field formats which can be used in select_tag
41 41 def self.as_select(class_name=nil)
42 42 formats = all.values.select do |format|
43 43 format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name)
44 44 end
45 45 formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first)
46 46 end
47 47
48 48 class Base
49 49 include Singleton
50 50 include Redmine::I18n
51 51 include Redmine::Helpers::URL
52 52 include ERB::Util
53 53
54 54 class_attribute :format_name
55 55 self.format_name = nil
56 56
57 57 # Set this to true if the format supports multiple values
58 58 class_attribute :multiple_supported
59 59 self.multiple_supported = false
60 60
61 61 # Set this to true if the format supports textual search on custom values
62 62 class_attribute :searchable_supported
63 63 self.searchable_supported = false
64 64
65 65 # Set this to true if field values can be summed up
66 66 class_attribute :totalable_supported
67 67 self.totalable_supported = false
68 68
69 69 # Restricts the classes that the custom field can be added to
70 70 # Set to nil for no restrictions
71 71 class_attribute :customized_class_names
72 72 self.customized_class_names = nil
73 73
74 74 # Name of the partial for editing the custom field
75 75 class_attribute :form_partial
76 76 self.form_partial = nil
77 77
78 78 class_attribute :change_as_diff
79 79 self.change_as_diff = false
80 80
81 81 def self.add(name)
82 82 self.format_name = name
83 83 Redmine::FieldFormat.add(name, self)
84 84 end
85 85 private_class_method :add
86 86
87 87 def self.field_attributes(*args)
88 88 CustomField.store_accessor :format_store, *args
89 89 end
90 90
91 91 field_attributes :url_pattern
92 92
93 93 def name
94 94 self.class.format_name
95 95 end
96 96
97 97 def label
98 98 "label_#{name}"
99 99 end
100 100
101 101 def cast_custom_value(custom_value)
102 102 cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
103 103 end
104 104
105 105 def cast_value(custom_field, value, customized=nil)
106 106 if value.blank?
107 107 nil
108 108 elsif value.is_a?(Array)
109 109 casted = value.map do |v|
110 110 cast_single_value(custom_field, v, customized)
111 111 end
112 112 casted.compact.sort
113 113 else
114 114 cast_single_value(custom_field, value, customized)
115 115 end
116 116 end
117 117
118 118 def cast_single_value(custom_field, value, customized=nil)
119 119 value.to_s
120 120 end
121 121
122 122 def target_class
123 123 nil
124 124 end
125 125
126 126 def possible_custom_value_options(custom_value)
127 127 possible_values_options(custom_value.custom_field, custom_value.customized)
128 128 end
129 129
130 130 def possible_values_options(custom_field, object=nil)
131 131 []
132 132 end
133 133
134 134 def value_from_keyword(custom_field, keyword, object)
135 135 possible_values_options = possible_values_options(custom_field, object)
136 136 if possible_values_options.present?
137 137 keyword = keyword.to_s
138 138 if v = possible_values_options.detect {|text, id| keyword.casecmp(text) == 0}
139 139 if v.is_a?(Array)
140 140 v.last
141 141 else
142 142 v
143 143 end
144 144 end
145 145 else
146 146 keyword
147 147 end
148 148 end
149 149
150 150 # Returns the validation errors for custom_field
151 151 # Should return an empty array if custom_field is valid
152 152 def validate_custom_field(custom_field)
153 153 errors = []
154 154 pattern = custom_field.url_pattern
155 155 if pattern.present? && !uri_with_safe_scheme?(url_pattern_without_tokens(pattern))
156 156 errors << [:url_pattern, :invalid]
157 157 end
158 158 errors
159 159 end
160 160
161 161 # Returns the validation error messages for custom_value
162 162 # Should return an empty array if custom_value is valid
163 163 def validate_custom_value(custom_value)
164 164 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
165 165 errors = values.map do |value|
166 166 validate_single_value(custom_value.custom_field, value, custom_value.customized)
167 167 end
168 168 errors.flatten.uniq
169 169 end
170 170
171 171 def validate_single_value(custom_field, value, customized=nil)
172 172 []
173 173 end
174 174
175 175 def formatted_custom_value(view, custom_value, html=false)
176 176 formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
177 177 end
178 178
179 179 def formatted_value(view, custom_field, value, customized=nil, html=false)
180 180 casted = cast_value(custom_field, value, customized)
181 181 if html && custom_field.url_pattern.present?
182 182 texts_and_urls = Array.wrap(casted).map do |single_value|
183 183 text = view.format_object(single_value, false).to_s
184 184 url = url_from_pattern(custom_field, single_value, customized)
185 185 [text, url]
186 186 end
187 187 links = texts_and_urls.sort_by(&:first).map {|text, url| view.link_to_if uri_with_safe_scheme?(url), text, url}
188 188 links.join(', ').html_safe
189 189 else
190 190 casted
191 191 end
192 192 end
193 193
194 194 # Returns an URL generated with the custom field URL pattern
195 195 # and variables substitution:
196 196 # %value% => the custom field value
197 197 # %id% => id of the customized object
198 198 # %project_id% => id of the project of the customized object if defined
199 199 # %project_identifier% => identifier of the project of the customized object if defined
200 200 # %m1%, %m2%... => capture groups matches of the custom field regexp if defined
201 201 def url_from_pattern(custom_field, value, customized)
202 202 url = custom_field.url_pattern.to_s.dup
203 203 url.gsub!('%value%') {value.to_s}
204 204 url.gsub!('%id%') {customized.id.to_s}
205 205 url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
206 206 url.gsub!('%project_identifier%') {(customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
207 207 if custom_field.regexp.present?
208 208 url.gsub!(%r{%m(\d+)%}) do
209 209 m = $1.to_i
210 210 if matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
211 211 matches[m].to_s
212 212 end
213 213 end
214 214 end
215 215 url
216 216 end
217 217 protected :url_from_pattern
218 218
219 219 # Returns the URL pattern with substitution tokens removed,
220 220 # for validation purpose
221 221 def url_pattern_without_tokens(url_pattern)
222 222 url_pattern.to_s.gsub(/%(value|id|project_id|project_identifier|m\d+)%/, '')
223 223 end
224 224 protected :url_pattern_without_tokens
225 225
226 226 def edit_tag(view, tag_id, tag_name, custom_value, options={})
227 227 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
228 228 end
229 229
230 230 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
231 231 view.text_field_tag(tag_name, value, options.merge(:id => tag_id)) +
232 232 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
233 233 end
234 234
235 235 def bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
236 236 if custom_field.is_required?
237 237 ''.html_safe
238 238 else
239 239 view.content_tag('label',
240 240 view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear),
241 241 :class => 'inline'
242 242 )
243 243 end
244 244 end
245 245 protected :bulk_clear_tag
246 246
247 247 def query_filter_options(custom_field, query)
248 248 {:type => :string}
249 249 end
250 250
251 251 def before_custom_field_save(custom_field)
252 252 end
253 253
254 254 # Returns a ORDER BY clause that can used to sort customized
255 255 # objects by their value of the custom field.
256 256 # Returns nil if the custom field can not be used for sorting.
257 257 def order_statement(custom_field)
258 258 # COALESCE is here to make sure that blank and NULL values are sorted equally
259 259 "COALESCE(#{join_alias custom_field}.value, '')"
260 260 end
261 261
262 262 # Returns a GROUP BY clause that can used to group by custom value
263 263 # Returns nil if the custom field can not be used for grouping.
264 264 def group_statement(custom_field)
265 265 nil
266 266 end
267 267
268 268 # Returns a JOIN clause that is added to the query when sorting by custom values
269 269 def join_for_order_statement(custom_field)
270 270 alias_name = join_alias(custom_field)
271 271
272 272 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
273 273 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
274 274 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
275 275 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
276 276 " AND (#{custom_field.visibility_by_project_condition})" +
277 277 " AND #{alias_name}.value <> ''" +
278 278 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
279 279 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
280 280 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
281 281 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
282 282 end
283 283
284 284 def join_alias(custom_field)
285 285 "cf_#{custom_field.id}"
286 286 end
287 287 protected :join_alias
288 288 end
289 289
290 290 class Unbounded < Base
291 291 def validate_single_value(custom_field, value, customized=nil)
292 292 errs = super
293 293 value = value.to_s
294 294 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
295 295 errs << ::I18n.t('activerecord.errors.messages.invalid')
296 296 end
297 297 if custom_field.min_length && value.length < custom_field.min_length
298 298 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length)
299 299 end
300 300 if custom_field.max_length && custom_field.max_length > 0 && value.length > custom_field.max_length
301 301 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length)
302 302 end
303 303 errs
304 304 end
305 305 end
306 306
307 307 class StringFormat < Unbounded
308 308 add 'string'
309 309 self.searchable_supported = true
310 310 self.form_partial = 'custom_fields/formats/string'
311 311 field_attributes :text_formatting
312 312
313 313 def formatted_value(view, custom_field, value, customized=nil, html=false)
314 314 if html
315 315 if custom_field.url_pattern.present?
316 316 super
317 317 elsif custom_field.text_formatting == 'full'
318 318 view.textilizable(value, :object => customized)
319 319 else
320 320 value.to_s
321 321 end
322 322 else
323 323 value.to_s
324 324 end
325 325 end
326 326 end
327 327
328 328 class TextFormat < Unbounded
329 329 add 'text'
330 330 self.searchable_supported = true
331 331 self.form_partial = 'custom_fields/formats/text'
332 332 self.change_as_diff = true
333 333
334 334 def formatted_value(view, custom_field, value, customized=nil, html=false)
335 335 if html
336 336 if value.present?
337 337 if custom_field.text_formatting == 'full'
338 338 view.textilizable(value, :object => customized)
339 339 else
340 340 view.simple_format(html_escape(value))
341 341 end
342 342 else
343 343 ''
344 344 end
345 345 else
346 346 value.to_s
347 347 end
348 348 end
349 349
350 350 def edit_tag(view, tag_id, tag_name, custom_value, options={})
351 351 view.text_area_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :rows => 3))
352 352 end
353 353
354 354 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
355 355 view.text_area_tag(tag_name, value, options.merge(:id => tag_id, :rows => 3)) +
356 356 '<br />'.html_safe +
357 357 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
358 358 end
359 359
360 360 def query_filter_options(custom_field, query)
361 361 {:type => :text}
362 362 end
363 363 end
364 364
365 365 class LinkFormat < StringFormat
366 366 add 'link'
367 367 self.searchable_supported = false
368 368 self.form_partial = 'custom_fields/formats/link'
369 369
370 370 def formatted_value(view, custom_field, value, customized=nil, html=false)
371 371 if html && value.present?
372 372 if custom_field.url_pattern.present?
373 373 url = url_from_pattern(custom_field, value, customized)
374 374 else
375 375 url = value.to_s
376 376 unless url =~ %r{\A[a-z]+://}i
377 377 # no protocol found, use http by default
378 378 url = "http://" + url
379 379 end
380 380 end
381 381 view.link_to value.to_s.truncate(40), url
382 382 else
383 383 value.to_s
384 384 end
385 385 end
386 386 end
387 387
388 388 class Numeric < Unbounded
389 389 self.form_partial = 'custom_fields/formats/numeric'
390 390 self.totalable_supported = true
391 391
392 392 def order_statement(custom_field)
393 393 # Make the database cast values into numeric
394 394 # Postgresql will raise an error if a value can not be casted!
395 395 # CustomValue validations should ensure that it doesn't occur
396 396 "CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))"
397 397 end
398 398
399 399 # Returns totals for the given scope
400 400 def total_for_scope(custom_field, scope)
401 401 scope.joins(:custom_values).
402 402 where(:custom_values => {:custom_field_id => custom_field.id}).
403 403 where.not(:custom_values => {:value => ''}).
404 404 sum("CAST(#{CustomValue.table_name}.value AS decimal(30,3))")
405 405 end
406 406
407 407 def cast_total_value(custom_field, value)
408 408 cast_single_value(custom_field, value)
409 409 end
410 410 end
411 411
412 412 class IntFormat < Numeric
413 413 add 'int'
414 414
415 415 def label
416 416 "label_integer"
417 417 end
418 418
419 419 def cast_single_value(custom_field, value, customized=nil)
420 420 value.to_i
421 421 end
422 422
423 423 def validate_single_value(custom_field, value, customized=nil)
424 424 errs = super
425 425 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value.to_s =~ /^[+-]?\d+$/
426 426 errs
427 427 end
428 428
429 429 def query_filter_options(custom_field, query)
430 430 {:type => :integer}
431 431 end
432 432
433 433 def group_statement(custom_field)
434 434 order_statement(custom_field)
435 435 end
436 436 end
437 437
438 438 class FloatFormat < Numeric
439 439 add 'float'
440 440
441 441 def cast_single_value(custom_field, value, customized=nil)
442 442 value.to_f
443 443 end
444 444
445 445 def cast_total_value(custom_field, value)
446 446 value.to_f.round(2)
447 447 end
448 448
449 449 def validate_single_value(custom_field, value, customized=nil)
450 450 errs = super
451 451 errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
452 452 errs
453 453 end
454 454
455 455 def query_filter_options(custom_field, query)
456 456 {:type => :float}
457 457 end
458 458 end
459 459
460 460 class DateFormat < Unbounded
461 461 add 'date'
462 462 self.form_partial = 'custom_fields/formats/date'
463 463
464 464 def cast_single_value(custom_field, value, customized=nil)
465 465 value.to_date rescue nil
466 466 end
467 467
468 468 def validate_single_value(custom_field, value, customized=nil)
469 469 if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
470 470 []
471 471 else
472 472 [::I18n.t('activerecord.errors.messages.not_a_date')]
473 473 end
474 474 end
475 475
476 476 def edit_tag(view, tag_id, tag_name, custom_value, options={})
477 477 view.date_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) +
478 478 view.calendar_for(tag_id)
479 479 end
480 480
481 481 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
482 482 view.date_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) +
483 483 view.calendar_for(tag_id) +
484 484 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
485 485 end
486 486
487 487 def query_filter_options(custom_field, query)
488 488 {:type => :date}
489 489 end
490 490
491 491 def group_statement(custom_field)
492 492 order_statement(custom_field)
493 493 end
494 494 end
495 495
496 496 class List < Base
497 497 self.multiple_supported = true
498 498 field_attributes :edit_tag_style
499 499
500 500 def edit_tag(view, tag_id, tag_name, custom_value, options={})
501 501 if custom_value.custom_field.edit_tag_style == 'check_box'
502 502 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
503 503 else
504 504 select_edit_tag(view, tag_id, tag_name, custom_value, options)
505 505 end
506 506 end
507 507
508 508 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
509 509 opts = []
510 510 opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
511 511 opts << [l(:label_none), '__none__'] unless custom_field.is_required?
512 512 opts += possible_values_options(custom_field, objects)
513 513 view.select_tag(tag_name, view.options_for_select(opts, value), options.merge(:multiple => custom_field.multiple?))
514 514 end
515 515
516 516 def query_filter_options(custom_field, query)
517 517 {:type => :list_optional, :values => query_filter_values(custom_field, query)}
518 518 end
519 519
520 520 protected
521 521
522 522 # Returns the values that are available in the field filter
523 523 def query_filter_values(custom_field, query)
524 524 possible_values_options(custom_field, query.project)
525 525 end
526 526
527 527 # Renders the edit tag as a select tag
528 528 def select_edit_tag(view, tag_id, tag_name, custom_value, options={})
529 529 blank_option = ''.html_safe
530 530 unless custom_value.custom_field.multiple?
531 531 if custom_value.custom_field.is_required?
532 532 unless custom_value.custom_field.default_value.present?
533 533 blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
534 534 end
535 535 else
536 536 blank_option = view.content_tag('option', '&nbsp;'.html_safe, :value => '')
537 537 end
538 538 end
539 539 options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
540 540 s = view.select_tag(tag_name, options_tags, options.merge(:id => tag_id, :multiple => custom_value.custom_field.multiple?))
541 541 if custom_value.custom_field.multiple?
542 542 s << view.hidden_field_tag(tag_name, '')
543 543 end
544 544 s
545 545 end
546 546
547 547 # Renders the edit tag as check box or radio tags
548 548 def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
549 549 opts = []
550 550 unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
551 551 opts << ["(#{l(:label_none)})", '']
552 552 end
553 553 opts += possible_custom_value_options(custom_value)
554 554 s = ''.html_safe
555 555 tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
556 556 opts.each do |label, value|
557 557 value ||= label
558 558 checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
559 559 tag = view.send(tag_method, tag_name, value, checked, :id => tag_id)
560 560 # set the id on the first tag only
561 561 tag_id = nil
562 562 s << view.content_tag('label', tag + ' ' + label)
563 563 end
564 564 if custom_value.custom_field.multiple?
565 565 s << view.hidden_field_tag(tag_name, '')
566 566 end
567 567 css = "#{options[:class]} check_box_group"
568 568 view.content_tag('span', s, options.merge(:class => css))
569 569 end
570 570 end
571 571
572 572 class ListFormat < List
573 573 add 'list'
574 574 self.searchable_supported = true
575 575 self.form_partial = 'custom_fields/formats/list'
576 576
577 577 def possible_custom_value_options(custom_value)
578 578 options = possible_values_options(custom_value.custom_field)
579 579 missing = [custom_value.value].flatten.reject(&:blank?) - options
580 580 if missing.any?
581 581 options += missing
582 582 end
583 583 options
584 584 end
585 585
586 586 def possible_values_options(custom_field, object=nil)
587 587 custom_field.possible_values
588 588 end
589 589
590 590 def validate_custom_field(custom_field)
591 591 errors = []
592 592 errors << [:possible_values, :blank] if custom_field.possible_values.blank?
593 593 errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
594 594 errors
595 595 end
596 596
597 597 def validate_custom_value(custom_value)
598 598 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
599 599 invalid_values = values - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values
600 600 if invalid_values.any?
601 601 [::I18n.t('activerecord.errors.messages.inclusion')]
602 602 else
603 603 []
604 604 end
605 605 end
606 606
607 607 def group_statement(custom_field)
608 608 order_statement(custom_field)
609 609 end
610 610 end
611 611
612 612 class BoolFormat < List
613 613 add 'bool'
614 614 self.multiple_supported = false
615 615 self.form_partial = 'custom_fields/formats/bool'
616 616
617 617 def label
618 618 "label_boolean"
619 619 end
620 620
621 621 def cast_single_value(custom_field, value, customized=nil)
622 622 value == '1' ? true : false
623 623 end
624 624
625 625 def possible_values_options(custom_field, object=nil)
626 626 [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
627 627 end
628 628
629 629 def group_statement(custom_field)
630 630 order_statement(custom_field)
631 631 end
632 632
633 633 def edit_tag(view, tag_id, tag_name, custom_value, options={})
634 634 case custom_value.custom_field.edit_tag_style
635 635 when 'check_box'
636 636 single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
637 637 when 'radio'
638 638 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
639 639 else
640 640 select_edit_tag(view, tag_id, tag_name, custom_value, options)
641 641 end
642 642 end
643 643
644 644 # Renders the edit tag as a simple check box
645 645 def single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
646 646 s = ''.html_safe
647 647 s << view.hidden_field_tag(tag_name, '0', :id => nil)
648 648 s << view.check_box_tag(tag_name, '1', custom_value.value.to_s == '1', :id => tag_id)
649 649 view.content_tag('span', s, options)
650 650 end
651 651 end
652 652
653 653 class RecordList < List
654 654 self.customized_class_names = %w(Issue TimeEntry Version Document Project)
655 655
656 656 def cast_single_value(custom_field, value, customized=nil)
657 657 target_class.find_by_id(value.to_i) if value.present?
658 658 end
659 659
660 660 def target_class
661 661 @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil
662 662 end
663 663
664 664 def reset_target_class
665 665 @target_class = nil
666 666 end
667 667
668 668 def possible_custom_value_options(custom_value)
669 669 options = possible_values_options(custom_value.custom_field, custom_value.customized)
670 670 missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
671 671 if missing.any?
672 672 options += target_class.where(:id => missing.map(&:to_i)).map {|o| [o.to_s, o.id.to_s]}
673 673 end
674 674 options
675 675 end
676 676
677 677 def order_statement(custom_field)
678 678 if target_class.respond_to?(:fields_for_order_statement)
679 679 target_class.fields_for_order_statement(value_join_alias(custom_field))
680 680 end
681 681 end
682 682
683 683 def group_statement(custom_field)
684 684 "COALESCE(#{join_alias custom_field}.value, '')"
685 685 end
686 686
687 687 def join_for_order_statement(custom_field)
688 688 alias_name = join_alias(custom_field)
689 689
690 690 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
691 691 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
692 692 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
693 693 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
694 694 " AND (#{custom_field.visibility_by_project_condition})" +
695 695 " AND #{alias_name}.value <> ''" +
696 696 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
697 697 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
698 698 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
699 699 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" +
700 700 " LEFT OUTER JOIN #{target_class.table_name} #{value_join_alias custom_field}" +
701 701 " ON CAST(CASE #{alias_name}.value WHEN '' THEN '0' ELSE #{alias_name}.value END AS decimal(30,0)) = #{value_join_alias custom_field}.id"
702 702 end
703 703
704 704 def value_join_alias(custom_field)
705 705 join_alias(custom_field) + "_" + custom_field.field_format
706 706 end
707 707 protected :value_join_alias
708 708 end
709 709
710 710 class EnumerationFormat < RecordList
711 711 add 'enumeration'
712 712 self.form_partial = 'custom_fields/formats/enumeration'
713 713
714 714 def label
715 715 "label_field_format_enumeration"
716 716 end
717 717
718 718 def target_class
719 719 @target_class ||= CustomFieldEnumeration
720 720 end
721 721
722 722 def possible_values_options(custom_field, object=nil)
723 723 possible_values_records(custom_field, object).map {|u| [u.name, u.id.to_s]}
724 724 end
725 725
726 726 def possible_values_records(custom_field, object=nil)
727 727 custom_field.enumerations.active
728 728 end
729 729
730 730 def value_from_keyword(custom_field, keyword, object)
731 731 value = custom_field.enumerations.where("LOWER(name) LIKE LOWER(?)", keyword).first
732 732 value ? value.id : nil
733 733 end
734 734 end
735 735
736 736 class UserFormat < RecordList
737 737 add 'user'
738 738 self.form_partial = 'custom_fields/formats/user'
739 739 field_attributes :user_role
740 740
741 741 def possible_values_options(custom_field, object=nil)
742 742 possible_values_records(custom_field, object).map {|u| [u.name, u.id.to_s]}
743 743 end
744 744
745 745 def possible_values_records(custom_field, object=nil)
746 746 if object.is_a?(Array)
747 747 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
748 748 projects.map {|project| possible_values_records(custom_field, project)}.reduce(:&) || []
749 749 elsif object.respond_to?(:project) && object.project
750 750 scope = object.project.users
751 751 if custom_field.user_role.is_a?(Array)
752 752 role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
753 753 if role_ids.any?
754 754 scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids)
755 755 end
756 756 end
757 757 scope.sorted
758 758 else
759 759 []
760 760 end
761 761 end
762 762
763 763 def value_from_keyword(custom_field, keyword, object)
764 764 users = possible_values_records(custom_field, object).to_a
765 765 user = Principal.detect_by_keyword(users, keyword)
766 766 user ? user.id : nil
767 767 end
768 768
769 769 def before_custom_field_save(custom_field)
770 770 super
771 771 if custom_field.user_role.is_a?(Array)
772 772 custom_field.user_role.map!(&:to_s).reject!(&:blank?)
773 773 end
774 774 end
775 775 end
776 776
777 777 class VersionFormat < RecordList
778 778 add 'version'
779 779 self.form_partial = 'custom_fields/formats/version'
780 780 field_attributes :version_status
781 781
782 782 def possible_values_options(custom_field, object=nil)
783 783 versions_options(custom_field, object)
784 784 end
785 785
786 786 def before_custom_field_save(custom_field)
787 787 super
788 788 if custom_field.version_status.is_a?(Array)
789 789 custom_field.version_status.map!(&:to_s).reject!(&:blank?)
790 790 end
791 791 end
792 792
793 793 protected
794 794
795 795 def query_filter_values(custom_field, query)
796 796 versions_options(custom_field, query.project, true)
797 797 end
798 798
799 799 def versions_options(custom_field, object, all_statuses=false)
800 800 if object.is_a?(Array)
801 801 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
802 802 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
803 803 elsif object.respond_to?(:project) && object.project
804 804 scope = object.project.shared_versions
805 if !all_statuses && custom_field.version_status.is_a?(Array)
806 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
807 if statuses.any?
808 scope = scope.where(:status => statuses.map(&:to_s))
809 end
810 end
811 scope.sort.collect {|u| [u.to_s, u.id.to_s]}
805 filtered_versions_options(custom_field, scope, all_statuses)
806 elsif object.nil?
807 scope = Version.visible.where(:sharing => 'system')
808 filtered_versions_options(custom_field, scope, all_statuses)
812 809 else
813 810 []
814 811 end
815 812 end
813
814 def filtered_versions_options(custom_field, scope, all_statuses=false)
815 if !all_statuses && custom_field.version_status.is_a?(Array)
816 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
817 if statuses.any?
818 scope = scope.where(:status => statuses.map(&:to_s))
819 end
820 end
821 scope.sort.collect{|u| [u.to_s, u.id.to_s] }
822 end
816 823 end
817 824 end
818 825 end
@@ -1,79 +1,88
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../../../test_helper', __FILE__)
19 19 require 'redmine/field_format'
20 20
21 21 class Redmine::VersionFieldFormatTest < ActionView::TestCase
22 22 fixtures :projects, :versions, :trackers,
23 23 :roles, :users, :members, :member_roles,
24 24 :issue_statuses, :issue_categories, :issue_relations, :workflows,
25 25 :enumerations
26 26
27 27 def test_version_status_should_reject_blank_values
28 28 field = IssueCustomField.new(:name => 'Foo', :field_format => 'version', :version_status => ["open", ""])
29 29 field.save!
30 30 assert_equal ["open"], field.version_status
31 31 end
32 32
33 33 def test_existing_values_should_be_valid
34 34 field = IssueCustomField.create!(:name => 'Foo', :field_format => 'version', :is_for_all => true, :trackers => Tracker.all)
35 35 project = Project.generate!
36 36 version = Version.generate!(:project => project, :status => 'open')
37 37 issue = Issue.generate!(:project_id => project.id, :tracker_id => 1, :custom_field_values => {field.id => version.id})
38 38
39 39 field.version_status = ["open"]
40 40 field.save!
41 41
42 42 issue = Issue.order('id DESC').first
43 43 assert_include [version.name, version.id.to_s], field.possible_custom_value_options(issue.custom_value_for(field))
44 44 assert issue.valid?
45 45 end
46 46
47 47 def test_possible_values_options_should_return_project_versions
48 48 field = IssueCustomField.new(:field_format => 'version')
49 49 project = Project.find(1)
50 50 expected = project.shared_versions.sort.map(&:name)
51 51
52 52 assert_equal expected, field.possible_values_options(project).map(&:first)
53 53 end
54
55 def test_possible_values_options_should_return_system_shared_versions_without_project
56 field = IssueCustomField.new(:field_format => 'version')
57 version = Version.generate!(:project => Project.find(1), :status => 'open', :sharing => 'system')
58
59 expected = Version.visible.where(:sharing => 'system').sort.map(&:name)
60 assert_include version.name, expected
61 assert_equal expected, field.possible_values_options.map(&:first)
62 end
54 63
55 64 def test_possible_values_options_should_return_project_versions_with_selected_status
56 65 field = IssueCustomField.new(:field_format => 'version', :version_status => ["open"])
57 66 project = Project.find(1)
58 67 expected = project.shared_versions.sort.select {|v| v.status == "open"}.map(&:name)
59 68
60 69 assert_equal expected, field.possible_values_options(project).map(&:first)
61 70 end
62 71
63 72 def test_cast_value_should_not_raise_error_when_array_contains_value_casted_to_nil
64 73 field = IssueCustomField.new(:field_format => 'version')
65 74 assert_nothing_raised do
66 75 field.cast_value([1,2, 42])
67 76 end
68 77 end
69 78
70 79 def test_query_filter_options_should_include_versions_with_any_status
71 80 field = IssueCustomField.new(:field_format => 'version', :version_status => ["open"])
72 81 project = Project.find(1)
73 82 version = Version.generate!(:project => project, :status => 'locked')
74 83 query = Query.new(:project => project)
75 84
76 85 assert_not_include version.name, field.possible_values_options(project).map(&:first)
77 86 assert_include version.name, field.query_filter_options(query)[:values].map(&:first)
78 87 end
79 88 end
General Comments 0
You need to be logged in to leave comments. Login now