Mercurial > hg > soundsoftware-site
comparison .svn/pristine/6e/6e3338f454d707517019dd9e77ecbfdcf1570d99.svn-base @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2013 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 class CustomField < ActiveRecord::Base | |
19 include Redmine::SubclassFactory | |
20 | |
21 has_many :custom_values, :dependent => :delete_all | |
22 acts_as_list :scope => 'type = \'#{self.class}\'' | |
23 serialize :possible_values | |
24 | |
25 validates_presence_of :name, :field_format | |
26 validates_uniqueness_of :name, :scope => :type | |
27 validates_length_of :name, :maximum => 30 | |
28 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats | |
29 | |
30 validate :validate_custom_field | |
31 before_validation :set_searchable | |
32 after_save :handle_multiplicity_change | |
33 | |
34 scope :sorted, lambda { order("#{table_name}.position ASC") } | |
35 | |
36 CUSTOM_FIELDS_TABS = [ | |
37 {:name => 'IssueCustomField', :partial => 'custom_fields/index', | |
38 :label => :label_issue_plural}, | |
39 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index', | |
40 :label => :label_spent_time}, | |
41 {:name => 'ProjectCustomField', :partial => 'custom_fields/index', | |
42 :label => :label_project_plural}, | |
43 {:name => 'VersionCustomField', :partial => 'custom_fields/index', | |
44 :label => :label_version_plural}, | |
45 {:name => 'UserCustomField', :partial => 'custom_fields/index', | |
46 :label => :label_user_plural}, | |
47 {:name => 'GroupCustomField', :partial => 'custom_fields/index', | |
48 :label => :label_group_plural}, | |
49 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index', | |
50 :label => TimeEntryActivity::OptionName}, | |
51 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index', | |
52 :label => IssuePriority::OptionName}, | |
53 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index', | |
54 :label => DocumentCategory::OptionName} | |
55 ] | |
56 | |
57 CUSTOM_FIELDS_NAMES = CUSTOM_FIELDS_TABS.collect{|v| v[:name]} | |
58 | |
59 def field_format=(arg) | |
60 # cannot change format of a saved custom field | |
61 super if new_record? | |
62 end | |
63 | |
64 def set_searchable | |
65 # make sure these fields are not searchable | |
66 self.searchable = false if %w(int float date bool).include?(field_format) | |
67 # make sure only these fields can have multiple values | |
68 self.multiple = false unless %w(list user version).include?(field_format) | |
69 true | |
70 end | |
71 | |
72 def validate_custom_field | |
73 if self.field_format == "list" | |
74 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? | |
75 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array | |
76 end | |
77 | |
78 if regexp.present? | |
79 begin | |
80 Regexp.new(regexp) | |
81 rescue | |
82 errors.add(:regexp, :invalid) | |
83 end | |
84 end | |
85 | |
86 if default_value.present? && !valid_field_value?(default_value) | |
87 errors.add(:default_value, :invalid) | |
88 end | |
89 end | |
90 | |
91 def possible_values_options(obj=nil) | |
92 case field_format | |
93 when 'user', 'version' | |
94 if obj.respond_to?(:project) && obj.project | |
95 case field_format | |
96 when 'user' | |
97 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} | |
98 when 'version' | |
99 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} | |
100 end | |
101 elsif obj.is_a?(Array) | |
102 obj.collect {|o| possible_values_options(o)}.reduce(:&) | |
103 else | |
104 [] | |
105 end | |
106 when 'bool' | |
107 [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']] | |
108 else | |
109 possible_values || [] | |
110 end | |
111 end | |
112 | |
113 def possible_values(obj=nil) | |
114 case field_format | |
115 when 'user', 'version' | |
116 possible_values_options(obj).collect(&:last) | |
117 when 'bool' | |
118 ['1', '0'] | |
119 else | |
120 values = super() | |
121 if values.is_a?(Array) | |
122 values.each do |value| | |
123 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) | |
124 end | |
125 end | |
126 values || [] | |
127 end | |
128 end | |
129 | |
130 # Makes possible_values accept a multiline string | |
131 def possible_values=(arg) | |
132 if arg.is_a?(Array) | |
133 super(arg.compact.collect(&:strip).select {|v| !v.blank?}) | |
134 else | |
135 self.possible_values = arg.to_s.split(/[\n\r]+/) | |
136 end | |
137 end | |
138 | |
139 def cast_value(value) | |
140 casted = nil | |
141 unless value.blank? | |
142 case field_format | |
143 when 'string', 'text', 'list' | |
144 casted = value | |
145 when 'date' | |
146 casted = begin; value.to_date; rescue; nil end | |
147 when 'bool' | |
148 casted = (value == '1' ? true : false) | |
149 when 'int' | |
150 casted = value.to_i | |
151 when 'float' | |
152 casted = value.to_f | |
153 when 'user', 'version' | |
154 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) | |
155 end | |
156 end | |
157 casted | |
158 end | |
159 | |
160 def value_from_keyword(keyword, customized) | |
161 possible_values_options = possible_values_options(customized) | |
162 if possible_values_options.present? | |
163 keyword = keyword.to_s.downcase | |
164 if v = possible_values_options.detect {|text, id| text.downcase == keyword} | |
165 if v.is_a?(Array) | |
166 v.last | |
167 else | |
168 v | |
169 end | |
170 end | |
171 else | |
172 keyword | |
173 end | |
174 end | |
175 | |
176 # Returns a ORDER BY clause that can used to sort customized | |
177 # objects by their value of the custom field. | |
178 # Returns nil if the custom field can not be used for sorting. | |
179 def order_statement | |
180 return nil if multiple? | |
181 case field_format | |
182 when 'string', 'text', 'list', 'date', 'bool' | |
183 # COALESCE is here to make sure that blank and NULL values are sorted equally | |
184 "COALESCE(#{join_alias}.value, '')" | |
185 when 'int', 'float' | |
186 # Make the database cast values into numeric | |
187 # Postgresql will raise an error if a value can not be casted! | |
188 # CustomValue validations should ensure that it doesn't occur | |
189 "CAST(CASE #{join_alias}.value WHEN '' THEN '0' ELSE #{join_alias}.value END AS decimal(30,3))" | |
190 when 'user', 'version' | |
191 value_class.fields_for_order_statement(value_join_alias) | |
192 else | |
193 nil | |
194 end | |
195 end | |
196 | |
197 # Returns a GROUP BY clause that can used to group by custom value | |
198 # Returns nil if the custom field can not be used for grouping. | |
199 def group_statement | |
200 return nil if multiple? | |
201 case field_format | |
202 when 'list', 'date', 'bool', 'int' | |
203 order_statement | |
204 when 'user', 'version' | |
205 "COALESCE(#{join_alias}.value, '')" | |
206 else | |
207 nil | |
208 end | |
209 end | |
210 | |
211 def join_for_order_statement | |
212 case field_format | |
213 when 'user', 'version' | |
214 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + | |
215 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + | |
216 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + | |
217 " AND #{join_alias}.custom_field_id = #{id}" + | |
218 " AND #{join_alias}.value <> ''" + | |
219 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + | |
220 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + | |
221 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + | |
222 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" + | |
223 " LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" + | |
224 " ON CAST(CASE #{join_alias}.value WHEN '' THEN '0' ELSE #{join_alias}.value END AS decimal(30,0)) = #{value_join_alias}.id" | |
225 when 'int', 'float' | |
226 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + | |
227 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + | |
228 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + | |
229 " AND #{join_alias}.custom_field_id = #{id}" + | |
230 " AND #{join_alias}.value <> ''" + | |
231 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + | |
232 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + | |
233 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + | |
234 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" | |
235 when 'string', 'text', 'list', 'date', 'bool' | |
236 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" + | |
237 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" + | |
238 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" + | |
239 " AND #{join_alias}.custom_field_id = #{id}" + | |
240 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" + | |
241 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" + | |
242 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" + | |
243 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" | |
244 else | |
245 nil | |
246 end | |
247 end | |
248 | |
249 def join_alias | |
250 "cf_#{id}" | |
251 end | |
252 | |
253 def value_join_alias | |
254 join_alias + "_" + field_format | |
255 end | |
256 | |
257 def <=>(field) | |
258 position <=> field.position | |
259 end | |
260 | |
261 # Returns the class that values represent | |
262 def value_class | |
263 case field_format | |
264 when 'user', 'version' | |
265 field_format.classify.constantize | |
266 else | |
267 nil | |
268 end | |
269 end | |
270 | |
271 def self.customized_class | |
272 self.name =~ /^(.+)CustomField$/ | |
273 begin; $1.constantize; rescue nil; end | |
274 end | |
275 | |
276 # to move in project_custom_field | |
277 def self.for_all | |
278 where(:is_for_all => true).order('position').all | |
279 end | |
280 | |
281 def type_name | |
282 nil | |
283 end | |
284 | |
285 # Returns the error messages for the given value | |
286 # or an empty array if value is a valid value for the custom field | |
287 def validate_field_value(value) | |
288 errs = [] | |
289 if value.is_a?(Array) | |
290 if !multiple? | |
291 errs << ::I18n.t('activerecord.errors.messages.invalid') | |
292 end | |
293 if is_required? && value.detect(&:present?).nil? | |
294 errs << ::I18n.t('activerecord.errors.messages.blank') | |
295 end | |
296 value.each {|v| errs += validate_field_value_format(v)} | |
297 else | |
298 if is_required? && value.blank? | |
299 errs << ::I18n.t('activerecord.errors.messages.blank') | |
300 end | |
301 errs += validate_field_value_format(value) | |
302 end | |
303 errs | |
304 end | |
305 | |
306 # Returns true if value is a valid value for the custom field | |
307 def valid_field_value?(value) | |
308 validate_field_value(value).empty? | |
309 end | |
310 | |
311 def format_in?(*args) | |
312 args.include?(field_format) | |
313 end | |
314 | |
315 protected | |
316 | |
317 # Returns the error message for the given value regarding its format | |
318 def validate_field_value_format(value) | |
319 errs = [] | |
320 if value.present? | |
321 errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp) | |
322 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length | |
323 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length | |
324 | |
325 # Format specific validations | |
326 case field_format | |
327 when 'int' | |
328 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ | |
329 when 'float' | |
330 begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end | |
331 when 'date' | |
332 errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end | |
333 when 'list' | |
334 errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value) | |
335 end | |
336 end | |
337 errs | |
338 end | |
339 | |
340 # Removes multiple values for the custom field after setting the multiple attribute to false | |
341 # We kepp the value with the highest id for each customized object | |
342 def handle_multiplicity_change | |
343 if !new_record? && multiple_was && !multiple | |
344 ids = custom_values. | |
345 where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" + | |
346 " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" + | |
347 " AND cve.id > #{CustomValue.table_name}.id)"). | |
348 pluck(:id) | |
349 | |
350 if ids.any? | |
351 custom_values.where(:id => ids).delete_all | |
352 end | |
353 end | |
354 end | |
355 end |