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