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