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