##// END OF EJS Templates
Merged r15846 (#23841)....
Jean-Philippe Lang -
r15487:ae4bfd533451
parent child
Show More
@@ -1,720 +1,720
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 'uri'
19 19
20 20 module Redmine
21 21 module FieldFormat
22 22 def self.add(name, klass)
23 23 all[name.to_s] = klass.instance
24 24 end
25 25
26 26 def self.delete(name)
27 27 all.delete(name.to_s)
28 28 end
29 29
30 30 def self.all
31 31 @formats ||= Hash.new(Base.instance)
32 32 end
33 33
34 34 def self.available_formats
35 35 all.keys
36 36 end
37 37
38 38 def self.find(name)
39 39 all[name.to_s]
40 40 end
41 41
42 42 # Return an array of custom field formats which can be used in select_tag
43 43 def self.as_select(class_name=nil)
44 44 formats = all.values.select do |format|
45 45 format.class.customized_class_names.nil? || format.class.customized_class_names.include?(class_name)
46 46 end
47 47 formats.map {|format| [::I18n.t(format.label), format.name] }.sort_by(&:first)
48 48 end
49 49
50 50 class Base
51 51 include Singleton
52 52 include Redmine::I18n
53 53 include ERB::Util
54 54
55 55 class_attribute :format_name
56 56 self.format_name = nil
57 57
58 58 # Set this to true if the format supports multiple values
59 59 class_attribute :multiple_supported
60 60 self.multiple_supported = false
61 61
62 62 # Set this to true if the format supports textual search on custom values
63 63 class_attribute :searchable_supported
64 64 self.searchable_supported = false
65 65
66 66 # Restricts the classes that the custom field can be added to
67 67 # Set to nil for no restrictions
68 68 class_attribute :customized_class_names
69 69 self.customized_class_names = nil
70 70
71 71 # Name of the partial for editing the custom field
72 72 class_attribute :form_partial
73 73 self.form_partial = nil
74 74
75 75 class_attribute :change_as_diff
76 76 self.change_as_diff = false
77 77
78 78 def self.add(name)
79 79 self.format_name = name
80 80 Redmine::FieldFormat.add(name, self)
81 81 end
82 82 private_class_method :add
83 83
84 84 def self.field_attributes(*args)
85 85 CustomField.store_accessor :format_store, *args
86 86 end
87 87
88 88 field_attributes :url_pattern
89 89
90 90 def name
91 91 self.class.format_name
92 92 end
93 93
94 94 def label
95 95 "label_#{name}"
96 96 end
97 97
98 98 def cast_custom_value(custom_value)
99 99 cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
100 100 end
101 101
102 102 def cast_value(custom_field, value, customized=nil)
103 103 if value.blank?
104 104 nil
105 105 elsif value.is_a?(Array)
106 106 casted = value.map do |v|
107 107 cast_single_value(custom_field, v, customized)
108 108 end
109 109 casted.compact.sort
110 110 else
111 111 cast_single_value(custom_field, value, customized)
112 112 end
113 113 end
114 114
115 115 def cast_single_value(custom_field, value, customized=nil)
116 116 value.to_s
117 117 end
118 118
119 119 def target_class
120 120 nil
121 121 end
122 122
123 123 def possible_custom_value_options(custom_value)
124 124 possible_values_options(custom_value.custom_field, custom_value.customized)
125 125 end
126 126
127 127 def possible_values_options(custom_field, object=nil)
128 128 []
129 129 end
130 130
131 131 # Returns the validation errors for custom_field
132 132 # Should return an empty array if custom_field is valid
133 133 def validate_custom_field(custom_field)
134 134 []
135 135 end
136 136
137 137 # Returns the validation error messages for custom_value
138 138 # Should return an empty array if custom_value is valid
139 139 def validate_custom_value(custom_value)
140 140 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
141 141 errors = values.map do |value|
142 142 validate_single_value(custom_value.custom_field, value, custom_value.customized)
143 143 end
144 144 errors.flatten.uniq
145 145 end
146 146
147 147 def validate_single_value(custom_field, value, customized=nil)
148 148 []
149 149 end
150 150
151 151 def formatted_custom_value(view, custom_value, html=false)
152 152 formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
153 153 end
154 154
155 155 def formatted_value(view, custom_field, value, customized=nil, html=false)
156 156 casted = cast_value(custom_field, value, customized)
157 157 if html && custom_field.url_pattern.present?
158 158 texts_and_urls = Array.wrap(casted).map do |single_value|
159 159 text = view.format_object(single_value, false).to_s
160 160 url = url_from_pattern(custom_field, single_value, customized)
161 161 [text, url]
162 162 end
163 163 links = texts_and_urls.sort_by(&:first).map {|text, url| view.link_to text, url}
164 164 links.join(', ').html_safe
165 165 else
166 166 casted
167 167 end
168 168 end
169 169
170 170 # Returns an URL generated with the custom field URL pattern
171 171 # and variables substitution:
172 172 # %value% => the custom field value
173 173 # %id% => id of the customized object
174 174 # %project_id% => id of the project of the customized object if defined
175 175 # %project_identifier% => identifier of the project of the customized object if defined
176 176 # %m1%, %m2%... => capture groups matches of the custom field regexp if defined
177 177 def url_from_pattern(custom_field, value, customized)
178 178 url = custom_field.url_pattern.to_s.dup
179 url.gsub!('%value%') {value.to_s}
180 url.gsub!('%id%') {customized.id.to_s}
181 url.gsub!('%project_id%') {(customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
182 url.gsub!('%project_identifier%') {(customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
179 url.gsub!('%value%') {URI.encode value.to_s}
180 url.gsub!('%id%') {URI.encode customized.id.to_s}
181 url.gsub!('%project_id%') {URI.encode (customized.respond_to?(:project) ? customized.project.try(:id) : nil).to_s}
182 url.gsub!('%project_identifier%') {URI.encode (customized.respond_to?(:project) ? customized.project.try(:identifier) : nil).to_s}
183 183 if custom_field.regexp.present?
184 184 url.gsub!(%r{%m(\d+)%}) do
185 185 m = $1.to_i
186 186 if matches ||= value.to_s.match(Regexp.new(custom_field.regexp))
187 matches[m].to_s
187 URI.encode matches[m].to_s
188 188 end
189 189 end
190 190 end
191 URI.encode(url)
191 url
192 192 end
193 193 protected :url_from_pattern
194 194
195 195 def edit_tag(view, tag_id, tag_name, custom_value, options={})
196 196 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
197 197 end
198 198
199 199 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
200 200 view.text_field_tag(tag_name, value, options.merge(:id => tag_id)) +
201 201 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
202 202 end
203 203
204 204 def bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
205 205 if custom_field.is_required?
206 206 ''.html_safe
207 207 else
208 208 view.content_tag('label',
209 209 view.check_box_tag(tag_name, '__none__', (value == '__none__'), :id => nil, :data => {:disables => "##{tag_id}"}) + l(:button_clear),
210 210 :class => 'inline'
211 211 )
212 212 end
213 213 end
214 214 protected :bulk_clear_tag
215 215
216 216 def query_filter_options(custom_field, query)
217 217 {:type => :string}
218 218 end
219 219
220 220 def before_custom_field_save(custom_field)
221 221 end
222 222
223 223 # Returns a ORDER BY clause that can used to sort customized
224 224 # objects by their value of the custom field.
225 225 # Returns nil if the custom field can not be used for sorting.
226 226 def order_statement(custom_field)
227 227 # COALESCE is here to make sure that blank and NULL values are sorted equally
228 228 "COALESCE(#{join_alias custom_field}.value, '')"
229 229 end
230 230
231 231 # Returns a GROUP BY clause that can used to group by custom value
232 232 # Returns nil if the custom field can not be used for grouping.
233 233 def group_statement(custom_field)
234 234 nil
235 235 end
236 236
237 237 # Returns a JOIN clause that is added to the query when sorting by custom values
238 238 def join_for_order_statement(custom_field)
239 239 alias_name = join_alias(custom_field)
240 240
241 241 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
242 242 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
243 243 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
244 244 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
245 245 " AND (#{custom_field.visibility_by_project_condition})" +
246 246 " AND #{alias_name}.value <> ''" +
247 247 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
248 248 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
249 249 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
250 250 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
251 251 end
252 252
253 253 def join_alias(custom_field)
254 254 "cf_#{custom_field.id}"
255 255 end
256 256 protected :join_alias
257 257 end
258 258
259 259 class Unbounded < Base
260 260 def validate_single_value(custom_field, value, customized=nil)
261 261 errs = super
262 262 value = value.to_s
263 263 unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
264 264 errs << ::I18n.t('activerecord.errors.messages.invalid')
265 265 end
266 266 if custom_field.min_length && value.length < custom_field.min_length
267 267 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => custom_field.min_length)
268 268 end
269 269 if custom_field.max_length && custom_field.max_length > 0 && value.length > custom_field.max_length
270 270 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => custom_field.max_length)
271 271 end
272 272 errs
273 273 end
274 274 end
275 275
276 276 class StringFormat < Unbounded
277 277 add 'string'
278 278 self.searchable_supported = true
279 279 self.form_partial = 'custom_fields/formats/string'
280 280 field_attributes :text_formatting
281 281
282 282 def formatted_value(view, custom_field, value, customized=nil, html=false)
283 283 if html
284 284 if custom_field.url_pattern.present?
285 285 super
286 286 elsif custom_field.text_formatting == 'full'
287 287 view.textilizable(value, :object => customized)
288 288 else
289 289 value.to_s
290 290 end
291 291 else
292 292 value.to_s
293 293 end
294 294 end
295 295 end
296 296
297 297 class TextFormat < Unbounded
298 298 add 'text'
299 299 self.searchable_supported = true
300 300 self.form_partial = 'custom_fields/formats/text'
301 301 self.change_as_diff = true
302 302
303 303 def formatted_value(view, custom_field, value, customized=nil, html=false)
304 304 if html
305 305 if value.present?
306 306 if custom_field.text_formatting == 'full'
307 307 view.textilizable(value, :object => customized)
308 308 else
309 309 view.simple_format(html_escape(value))
310 310 end
311 311 else
312 312 ''
313 313 end
314 314 else
315 315 value.to_s
316 316 end
317 317 end
318 318
319 319 def edit_tag(view, tag_id, tag_name, custom_value, options={})
320 320 view.text_area_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :rows => 3))
321 321 end
322 322
323 323 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
324 324 view.text_area_tag(tag_name, value, options.merge(:id => tag_id, :rows => 3)) +
325 325 '<br />'.html_safe +
326 326 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
327 327 end
328 328
329 329 def query_filter_options(custom_field, query)
330 330 {:type => :text}
331 331 end
332 332 end
333 333
334 334 class LinkFormat < StringFormat
335 335 add 'link'
336 336 self.searchable_supported = false
337 337 self.form_partial = 'custom_fields/formats/link'
338 338
339 339 def formatted_value(view, custom_field, value, customized=nil, html=false)
340 340 if html
341 341 if custom_field.url_pattern.present?
342 342 url = url_from_pattern(custom_field, value, customized)
343 343 else
344 344 url = value.to_s
345 345 unless url =~ %r{\A[a-z]+://}i
346 346 # no protocol found, use http by default
347 347 url = "http://" + url
348 348 end
349 349 end
350 350 view.link_to value.to_s, url
351 351 else
352 352 value.to_s
353 353 end
354 354 end
355 355 end
356 356
357 357 class Numeric < Unbounded
358 358 self.form_partial = 'custom_fields/formats/numeric'
359 359
360 360 def order_statement(custom_field)
361 361 # Make the database cast values into numeric
362 362 # Postgresql will raise an error if a value can not be casted!
363 363 # CustomValue validations should ensure that it doesn't occur
364 364 "CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))"
365 365 end
366 366 end
367 367
368 368 class IntFormat < Numeric
369 369 add 'int'
370 370
371 371 def label
372 372 "label_integer"
373 373 end
374 374
375 375 def cast_single_value(custom_field, value, customized=nil)
376 376 value.to_i
377 377 end
378 378
379 379 def validate_single_value(custom_field, value, customized=nil)
380 380 errs = super
381 381 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value.to_s =~ /^[+-]?\d+$/
382 382 errs
383 383 end
384 384
385 385 def query_filter_options(custom_field, query)
386 386 {:type => :integer}
387 387 end
388 388
389 389 def group_statement(custom_field)
390 390 order_statement(custom_field)
391 391 end
392 392 end
393 393
394 394 class FloatFormat < Numeric
395 395 add 'float'
396 396
397 397 def cast_single_value(custom_field, value, customized=nil)
398 398 value.to_f
399 399 end
400 400
401 401 def validate_single_value(custom_field, value, customized=nil)
402 402 errs = super
403 403 errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)
404 404 errs
405 405 end
406 406
407 407 def query_filter_options(custom_field, query)
408 408 {:type => :float}
409 409 end
410 410 end
411 411
412 412 class DateFormat < Unbounded
413 413 add 'date'
414 414 self.form_partial = 'custom_fields/formats/date'
415 415
416 416 def cast_single_value(custom_field, value, customized=nil)
417 417 value.to_date rescue nil
418 418 end
419 419
420 420 def validate_single_value(custom_field, value, customized=nil)
421 421 if value =~ /^\d{4}-\d{2}-\d{2}$/ && (value.to_date rescue false)
422 422 []
423 423 else
424 424 [::I18n.t('activerecord.errors.messages.not_a_date')]
425 425 end
426 426 end
427 427
428 428 def edit_tag(view, tag_id, tag_name, custom_value, options={})
429 429 view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id, :size => 10)) +
430 430 view.calendar_for(tag_id)
431 431 end
432 432
433 433 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
434 434 view.text_field_tag(tag_name, value, options.merge(:id => tag_id, :size => 10)) +
435 435 view.calendar_for(tag_id) +
436 436 bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
437 437 end
438 438
439 439 def query_filter_options(custom_field, query)
440 440 {:type => :date}
441 441 end
442 442
443 443 def group_statement(custom_field)
444 444 order_statement(custom_field)
445 445 end
446 446 end
447 447
448 448 class List < Base
449 449 self.multiple_supported = true
450 450 field_attributes :edit_tag_style
451 451
452 452 def edit_tag(view, tag_id, tag_name, custom_value, options={})
453 453 if custom_value.custom_field.edit_tag_style == 'check_box'
454 454 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
455 455 else
456 456 select_edit_tag(view, tag_id, tag_name, custom_value, options)
457 457 end
458 458 end
459 459
460 460 def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
461 461 opts = []
462 462 opts << [l(:label_no_change_option), ''] unless custom_field.multiple?
463 463 opts << [l(:label_none), '__none__'] unless custom_field.is_required?
464 464 opts += possible_values_options(custom_field, objects)
465 465 view.select_tag(tag_name, view.options_for_select(opts, value), options.merge(:multiple => custom_field.multiple?))
466 466 end
467 467
468 468 def query_filter_options(custom_field, query)
469 469 {:type => :list_optional, :values => possible_values_options(custom_field, query.project)}
470 470 end
471 471
472 472 protected
473 473
474 474 # Renders the edit tag as a select tag
475 475 def select_edit_tag(view, tag_id, tag_name, custom_value, options={})
476 476 blank_option = ''.html_safe
477 477 unless custom_value.custom_field.multiple?
478 478 if custom_value.custom_field.is_required?
479 479 unless custom_value.custom_field.default_value.present?
480 480 blank_option = view.content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---", :value => '')
481 481 end
482 482 else
483 483 blank_option = view.content_tag('option', '&nbsp;'.html_safe, :value => '')
484 484 end
485 485 end
486 486 options_tags = blank_option + view.options_for_select(possible_custom_value_options(custom_value), custom_value.value)
487 487 s = view.select_tag(tag_name, options_tags, options.merge(:id => tag_id, :multiple => custom_value.custom_field.multiple?))
488 488 if custom_value.custom_field.multiple?
489 489 s << view.hidden_field_tag(tag_name, '')
490 490 end
491 491 s
492 492 end
493 493
494 494 # Renders the edit tag as check box or radio tags
495 495 def check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
496 496 opts = []
497 497 unless custom_value.custom_field.multiple? || custom_value.custom_field.is_required?
498 498 opts << ["(#{l(:label_none)})", '']
499 499 end
500 500 opts += possible_custom_value_options(custom_value)
501 501 s = ''.html_safe
502 502 tag_method = custom_value.custom_field.multiple? ? :check_box_tag : :radio_button_tag
503 503 opts.each do |label, value|
504 504 value ||= label
505 505 checked = (custom_value.value.is_a?(Array) && custom_value.value.include?(value)) || custom_value.value.to_s == value
506 506 tag = view.send(tag_method, tag_name, value, checked, :id => tag_id)
507 507 # set the id on the first tag only
508 508 tag_id = nil
509 509 s << view.content_tag('label', tag + ' ' + label)
510 510 end
511 511 if custom_value.custom_field.multiple?
512 512 s << view.hidden_field_tag(tag_name, '')
513 513 end
514 514 css = "#{options[:class]} check_box_group"
515 515 view.content_tag('span', s, options.merge(:class => css))
516 516 end
517 517 end
518 518
519 519 class ListFormat < List
520 520 add 'list'
521 521 self.searchable_supported = true
522 522 self.form_partial = 'custom_fields/formats/list'
523 523
524 524 def possible_custom_value_options(custom_value)
525 525 options = possible_values_options(custom_value.custom_field)
526 526 missing = [custom_value.value].flatten.reject(&:blank?) - options
527 527 if missing.any?
528 528 options += missing
529 529 end
530 530 options
531 531 end
532 532
533 533 def possible_values_options(custom_field, object=nil)
534 534 custom_field.possible_values
535 535 end
536 536
537 537 def validate_custom_field(custom_field)
538 538 errors = []
539 539 errors << [:possible_values, :blank] if custom_field.possible_values.blank?
540 540 errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
541 541 errors
542 542 end
543 543
544 544 def validate_custom_value(custom_value)
545 545 values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
546 546 invalid_values = values - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values
547 547 if invalid_values.any?
548 548 [::I18n.t('activerecord.errors.messages.inclusion')]
549 549 else
550 550 []
551 551 end
552 552 end
553 553
554 554 def group_statement(custom_field)
555 555 order_statement(custom_field)
556 556 end
557 557 end
558 558
559 559 class BoolFormat < List
560 560 add 'bool'
561 561 self.multiple_supported = false
562 562 self.form_partial = 'custom_fields/formats/bool'
563 563
564 564 def label
565 565 "label_boolean"
566 566 end
567 567
568 568 def cast_single_value(custom_field, value, customized=nil)
569 569 value == '1' ? true : false
570 570 end
571 571
572 572 def possible_values_options(custom_field, object=nil)
573 573 [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
574 574 end
575 575
576 576 def group_statement(custom_field)
577 577 order_statement(custom_field)
578 578 end
579 579
580 580 def edit_tag(view, tag_id, tag_name, custom_value, options={})
581 581 case custom_value.custom_field.edit_tag_style
582 582 when 'check_box'
583 583 single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
584 584 when 'radio'
585 585 check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
586 586 else
587 587 select_edit_tag(view, tag_id, tag_name, custom_value, options)
588 588 end
589 589 end
590 590
591 591 # Renders the edit tag as a simple check box
592 592 def single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options={})
593 593 s = ''.html_safe
594 594 s << view.hidden_field_tag(tag_name, '0', :id => nil)
595 595 s << view.check_box_tag(tag_name, '1', custom_value.value.to_s == '1', :id => tag_id)
596 596 view.content_tag('span', s, options)
597 597 end
598 598 end
599 599
600 600 class RecordList < List
601 601 self.customized_class_names = %w(Issue TimeEntry Version Document Project)
602 602
603 603 def cast_single_value(custom_field, value, customized=nil)
604 604 target_class.find_by_id(value.to_i) if value.present?
605 605 end
606 606
607 607 def target_class
608 608 @target_class ||= self.class.name[/^(.*::)?(.+)Format$/, 2].constantize rescue nil
609 609 end
610 610
611 611 def reset_target_class
612 612 @target_class = nil
613 613 end
614 614
615 615 def possible_custom_value_options(custom_value)
616 616 options = possible_values_options(custom_value.custom_field, custom_value.customized)
617 617 missing = [custom_value.value_was].flatten.reject(&:blank?) - options.map(&:last)
618 618 if missing.any?
619 619 options += target_class.where(:id => missing.map(&:to_i)).map {|o| [o.to_s, o.id.to_s]}
620 620 options.sort_by!(&:first)
621 621 end
622 622 options
623 623 end
624 624
625 625 def order_statement(custom_field)
626 626 if target_class.respond_to?(:fields_for_order_statement)
627 627 target_class.fields_for_order_statement(value_join_alias(custom_field))
628 628 end
629 629 end
630 630
631 631 def group_statement(custom_field)
632 632 "COALESCE(#{join_alias custom_field}.value, '')"
633 633 end
634 634
635 635 def join_for_order_statement(custom_field)
636 636 alias_name = join_alias(custom_field)
637 637
638 638 "LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
639 639 " ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
640 640 " AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
641 641 " AND #{alias_name}.custom_field_id = #{custom_field.id}" +
642 642 " AND (#{custom_field.visibility_by_project_condition})" +
643 643 " AND #{alias_name}.value <> ''" +
644 644 " AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
645 645 " WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
646 646 " AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
647 647 " AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)" +
648 648 " LEFT OUTER JOIN #{target_class.table_name} #{value_join_alias custom_field}" +
649 649 " ON CAST(CASE #{alias_name}.value WHEN '' THEN '0' ELSE #{alias_name}.value END AS decimal(30,0)) = #{value_join_alias custom_field}.id"
650 650 end
651 651
652 652 def value_join_alias(custom_field)
653 653 join_alias(custom_field) + "_" + custom_field.field_format
654 654 end
655 655 protected :value_join_alias
656 656 end
657 657
658 658 class UserFormat < RecordList
659 659 add 'user'
660 660 self.form_partial = 'custom_fields/formats/user'
661 661 field_attributes :user_role
662 662
663 663 def possible_values_options(custom_field, object=nil)
664 664 if object.is_a?(Array)
665 665 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
666 666 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
667 667 elsif object.respond_to?(:project) && object.project
668 668 scope = object.project.users
669 669 if custom_field.user_role.is_a?(Array)
670 670 role_ids = custom_field.user_role.map(&:to_s).reject(&:blank?).map(&:to_i)
671 671 if role_ids.any?
672 672 scope = scope.where("#{Member.table_name}.id IN (SELECT DISTINCT member_id FROM #{MemberRole.table_name} WHERE role_id IN (?))", role_ids)
673 673 end
674 674 end
675 675 scope.sorted.collect {|u| [u.to_s, u.id.to_s]}
676 676 else
677 677 []
678 678 end
679 679 end
680 680
681 681 def before_custom_field_save(custom_field)
682 682 super
683 683 if custom_field.user_role.is_a?(Array)
684 684 custom_field.user_role.map!(&:to_s).reject!(&:blank?)
685 685 end
686 686 end
687 687 end
688 688
689 689 class VersionFormat < RecordList
690 690 add 'version'
691 691 self.form_partial = 'custom_fields/formats/version'
692 692 field_attributes :version_status
693 693
694 694 def possible_values_options(custom_field, object=nil)
695 695 if object.is_a?(Array)
696 696 projects = object.map {|o| o.respond_to?(:project) ? o.project : nil}.compact.uniq
697 697 projects.map {|project| possible_values_options(custom_field, project)}.reduce(:&) || []
698 698 elsif object.respond_to?(:project) && object.project
699 699 scope = object.project.shared_versions
700 700 if custom_field.version_status.is_a?(Array)
701 701 statuses = custom_field.version_status.map(&:to_s).reject(&:blank?)
702 702 if statuses.any?
703 703 scope = scope.where(:status => statuses.map(&:to_s))
704 704 end
705 705 end
706 706 scope.sort.collect {|u| [u.to_s, u.id.to_s]}
707 707 else
708 708 []
709 709 end
710 710 end
711 711
712 712 def before_custom_field_save(custom_field)
713 713 super
714 714 if custom_field.version_status.is_a?(Array)
715 715 custom_field.version_status.map!(&:to_s).reject!(&:blank?)
716 716 end
717 717 end
718 718 end
719 719 end
720 720 end
@@ -1,70 +1,86
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
20 20 class Redmine::FieldFormatTest < ActionView::TestCase
21 21 include ApplicationHelper
22 22
23 23 def test_string_field_with_text_formatting_disabled_should_not_format_text
24 24 field = IssueCustomField.new(:field_format => 'string')
25 25 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")
26 26
27 27 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false)
28 28 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, true)
29 29 end
30 30
31 31 def test_string_field_with_text_formatting_enabled_should_format_text
32 32 field = IssueCustomField.new(:field_format => 'string', :text_formatting => 'full')
33 33 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")
34 34
35 35 assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false)
36 36 assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true)
37 37 end
38 38
39 39 def test_text_field_with_text_formatting_disabled_should_not_format_text
40 40 field = IssueCustomField.new(:field_format => 'text')
41 41 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")
42 42
43 43 assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
44 44 assert_include "*foo*\n<br />bar", field.format.formatted_custom_value(self, custom_value, true)
45 45 end
46 46
47 47 def test_text_field_with_text_formatting_enabled_should_format_text
48 48 field = IssueCustomField.new(:field_format => 'text', :text_formatting => 'full')
49 49 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")
50 50
51 51 assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
52 52 assert_include "<strong>foo</strong>", field.format.formatted_custom_value(self, custom_value, true)
53 53 end
54 54
55 55 def test_text_field_with_url_pattern_should_format_as_link
56 56 field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
57 57 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "bar")
58 58
59 59 assert_equal "bar", field.format.formatted_custom_value(self, custom_value, false)
60 60 assert_equal '<a href="http://foo/bar">bar</a>', field.format.formatted_custom_value(self, custom_value, true)
61 61 end
62 62
63 63 def test_text_field_with_url_pattern_and_value_containing_a_space_should_format_as_link
64 64 field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
65 65 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "foo bar")
66 66
67 67 assert_equal "foo bar", field.format.formatted_custom_value(self, custom_value, false)
68 68 assert_equal '<a href="http://foo/foo%20bar">foo bar</a>', field.format.formatted_custom_value(self, custom_value, true)
69 69 end
70
71 def test_text_field_with_url_pattern_should_not_encode_url_pattern
72 field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/bar#anchor')
73 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "1")
74
75 assert_equal "1", field.format.formatted_custom_value(self, custom_value, false)
76 assert_equal '<a href="http://foo/bar#anchor">1</a>', field.format.formatted_custom_value(self, custom_value, true)
77 end
78
79 def test_text_field_with_url_pattern_should_encode_values
80 field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%#anchor')
81 custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "foo bar")
82
83 assert_equal "foo bar", field.format.formatted_custom_value(self, custom_value, false)
84 assert_equal '<a href="http://foo/foo%20bar#anchor">foo bar</a>', field.format.formatted_custom_value(self, custom_value, true)
85 end
70 86 end
General Comments 0
You need to be logged in to leave comments. Login now