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