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