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