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