comparison .svn/pristine/14/14309d64e537e5e954feb549559e6200d128036a.svn-base @ 1296:038ba2d95de8 redmine-2.2

Fix redmine-2.2 branch update (add missing svn files)
author Chris Cannam
date Fri, 14 Jun 2013 09:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
1294:3e4c3460b6ca 1296:038ba2d95de8
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 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
33 CUSTOM_FIELDS_TABS = [
34 {:name => 'IssueCustomField', :partial => 'custom_fields/index',
35 :label => :label_issue_plural},
36 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
37 :label => :label_spent_time},
38 {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
39 :label => :label_project_plural},
40 {:name => 'VersionCustomField', :partial => 'custom_fields/index',
41 :label => :label_version_plural},
42 {:name => 'UserCustomField', :partial => 'custom_fields/index',
43 :label => :label_user_plural},
44 {:name => 'GroupCustomField', :partial => 'custom_fields/index',
45 :label => :label_group_plural},
46 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
47 :label => TimeEntryActivity::OptionName},
48 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
49 :label => IssuePriority::OptionName},
50 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
51 :label => DocumentCategory::OptionName}
52 ]
53
54 CUSTOM_FIELDS_NAMES = CUSTOM_FIELDS_TABS.collect{|v| v[:name]}
55
56 def field_format=(arg)
57 # cannot change format of a saved custom field
58 super if new_record?
59 end
60
61 def set_searchable
62 # make sure these fields are not searchable
63 self.searchable = false if %w(int float date bool).include?(field_format)
64 # make sure only these fields can have multiple values
65 self.multiple = false unless %w(list user version).include?(field_format)
66 true
67 end
68
69 def validate_custom_field
70 if self.field_format == "list"
71 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
72 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
73 end
74
75 if regexp.present?
76 begin
77 Regexp.new(regexp)
78 rescue
79 errors.add(:regexp, :invalid)
80 end
81 end
82
83 if default_value.present? && !valid_field_value?(default_value)
84 errors.add(:default_value, :invalid)
85 end
86 end
87
88 def possible_values_options(obj=nil)
89 case field_format
90 when 'user', 'version'
91 if obj.respond_to?(:project) && obj.project
92 case field_format
93 when 'user'
94 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
95 when 'version'
96 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
97 end
98 elsif obj.is_a?(Array)
99 obj.collect {|o| possible_values_options(o)}.reduce(:&)
100 else
101 []
102 end
103 when 'bool'
104 [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']]
105 else
106 possible_values || []
107 end
108 end
109
110 def possible_values(obj=nil)
111 case field_format
112 when 'user', 'version'
113 possible_values_options(obj).collect(&:last)
114 when 'bool'
115 ['1', '0']
116 else
117 values = super()
118 if values.is_a?(Array)
119 values.each do |value|
120 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
121 end
122 end
123 values || []
124 end
125 end
126
127 # Makes possible_values accept a multiline string
128 def possible_values=(arg)
129 if arg.is_a?(Array)
130 super(arg.compact.collect(&:strip).select {|v| !v.blank?})
131 else
132 self.possible_values = arg.to_s.split(/[\n\r]+/)
133 end
134 end
135
136 def cast_value(value)
137 casted = nil
138 unless value.blank?
139 case field_format
140 when 'string', 'text', 'list'
141 casted = value
142 when 'date'
143 casted = begin; value.to_date; rescue; nil end
144 when 'bool'
145 casted = (value == '1' ? true : false)
146 when 'int'
147 casted = value.to_i
148 when 'float'
149 casted = value.to_f
150 when 'user', 'version'
151 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
152 end
153 end
154 casted
155 end
156
157 def value_from_keyword(keyword, customized)
158 possible_values_options = possible_values_options(customized)
159 if possible_values_options.present?
160 keyword = keyword.to_s.downcase
161 if v = possible_values_options.detect {|text, id| text.downcase == keyword}
162 if v.is_a?(Array)
163 v.last
164 else
165 v
166 end
167 end
168 else
169 keyword
170 end
171 end
172
173 # Returns a ORDER BY clause that can used to sort customized
174 # objects by their value of the custom field.
175 # Returns nil if the custom field can not be used for sorting.
176 def order_statement
177 return nil if multiple?
178 case field_format
179 when 'string', 'text', 'list', 'date', 'bool'
180 # COALESCE is here to make sure that blank and NULL values are sorted equally
181 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
182 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
183 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
184 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
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 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
190 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
191 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
192 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
193 when 'user', 'version'
194 value_class.fields_for_order_statement(value_join_alias)
195 else
196 nil
197 end
198 end
199
200 # Returns a GROUP BY clause that can used to group by custom value
201 # Returns nil if the custom field can not be used for grouping.
202 def group_statement
203 return nil if multiple?
204 case field_format
205 when 'list', 'date', 'bool', 'int'
206 order_statement
207 when 'user', 'version'
208 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
209 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
210 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
211 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
212 else
213 nil
214 end
215 end
216
217 def join_for_order_statement
218 case field_format
219 when 'user', 'version'
220 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
221 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
222 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
223 " AND #{join_alias}.custom_field_id = #{id}" +
224 " AND #{join_alias}.value <> ''" +
225 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
226 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
227 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
228 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" +
229 " LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" +
230 " ON CAST(#{join_alias}.value as decimal(60,0)) = #{value_join_alias}.id"
231 else
232 nil
233 end
234 end
235
236 def join_alias
237 "cf_#{id}"
238 end
239
240 def value_join_alias
241 join_alias + "_" + field_format
242 end
243
244 def <=>(field)
245 position <=> field.position
246 end
247
248 # Returns the class that values represent
249 def value_class
250 case field_format
251 when 'user', 'version'
252 field_format.classify.constantize
253 else
254 nil
255 end
256 end
257
258 def self.customized_class
259 self.name =~ /^(.+)CustomField$/
260 begin; $1.constantize; rescue nil; end
261 end
262
263 # to move in project_custom_field
264 def self.for_all
265 find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
266 end
267
268 def type_name
269 nil
270 end
271
272 # Returns the error messages for the given value
273 # or an empty array if value is a valid value for the custom field
274 def validate_field_value(value)
275 errs = []
276 if value.is_a?(Array)
277 if !multiple?
278 errs << ::I18n.t('activerecord.errors.messages.invalid')
279 end
280 if is_required? && value.detect(&:present?).nil?
281 errs << ::I18n.t('activerecord.errors.messages.blank')
282 end
283 value.each {|v| errs += validate_field_value_format(v)}
284 else
285 if is_required? && value.blank?
286 errs << ::I18n.t('activerecord.errors.messages.blank')
287 end
288 errs += validate_field_value_format(value)
289 end
290 errs
291 end
292
293 # Returns true if value is a valid value for the custom field
294 def valid_field_value?(value)
295 validate_field_value(value).empty?
296 end
297
298 def format_in?(*args)
299 args.include?(field_format)
300 end
301
302 protected
303
304 # Returns the error message for the given value regarding its format
305 def validate_field_value_format(value)
306 errs = []
307 if value.present?
308 errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
309 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
310 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
311
312 # Format specific validations
313 case field_format
314 when 'int'
315 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
316 when 'float'
317 begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
318 when 'date'
319 errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
320 when 'list'
321 errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
322 end
323 end
324 errs
325 end
326 end