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