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