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