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