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