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