Chris@441
|
1 # Redmine - project management software
|
Chris@1115
|
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class CustomField < ActiveRecord::Base
|
Chris@1115
|
19 include Redmine::SubclassFactory
|
Chris@1115
|
20
|
Chris@0
|
21 has_many :custom_values, :dependent => :delete_all
|
Chris@0
|
22 acts_as_list :scope => 'type = \'#{self.class}\''
|
Chris@0
|
23 serialize :possible_values
|
Chris@909
|
24
|
Chris@0
|
25 validates_presence_of :name, :field_format
|
Chris@0
|
26 validates_uniqueness_of :name, :scope => :type
|
Chris@0
|
27 validates_length_of :name, :maximum => 30
|
Chris@0
|
28 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
|
Chris@0
|
29
|
Chris@1115
|
30 validate :validate_custom_field
|
Chris@1115
|
31 before_validation :set_searchable
|
Chris@909
|
32
|
Chris@1115
|
33 CUSTOM_FIELDS_TABS = [
|
Chris@1115
|
34 {:name => 'IssueCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
35 :label => :label_issue_plural},
|
Chris@1115
|
36 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
37 :label => :label_spent_time},
|
Chris@1115
|
38 {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
39 :label => :label_project_plural},
|
Chris@1115
|
40 {:name => 'VersionCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
41 :label => :label_version_plural},
|
Chris@1115
|
42 {:name => 'UserCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
43 :label => :label_user_plural},
|
Chris@1115
|
44 {:name => 'GroupCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
45 :label => :label_group_plural},
|
Chris@1115
|
46 {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
47 :label => TimeEntryActivity::OptionName},
|
Chris@1115
|
48 {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
49 :label => IssuePriority::OptionName},
|
Chris@1115
|
50 {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
|
Chris@1115
|
51 :label => DocumentCategory::OptionName}
|
Chris@1115
|
52 ]
|
Chris@1115
|
53
|
Chris@1115
|
54 CUSTOM_FIELDS_NAMES = CUSTOM_FIELDS_TABS.collect{|v| v[:name]}
|
Chris@1115
|
55
|
Chris@1115
|
56 def field_format=(arg)
|
Chris@1115
|
57 # cannot change format of a saved custom field
|
Chris@1115
|
58 super if new_record?
|
Chris@0
|
59 end
|
Chris@909
|
60
|
Chris@1115
|
61 def set_searchable
|
Chris@0
|
62 # make sure these fields are not searchable
|
Chris@0
|
63 self.searchable = false if %w(int float date bool).include?(field_format)
|
Chris@1115
|
64 # make sure only these fields can have multiple values
|
Chris@1115
|
65 self.multiple = false unless %w(list user version).include?(field_format)
|
Chris@0
|
66 true
|
Chris@0
|
67 end
|
Chris@909
|
68
|
Chris@1115
|
69 def validate_custom_field
|
Chris@0
|
70 if self.field_format == "list"
|
Chris@0
|
71 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
|
Chris@0
|
72 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
|
Chris@0
|
73 end
|
Chris@909
|
74
|
Chris@909
|
75 if regexp.present?
|
Chris@909
|
76 begin
|
Chris@909
|
77 Regexp.new(regexp)
|
Chris@909
|
78 rescue
|
Chris@909
|
79 errors.add(:regexp, :invalid)
|
Chris@909
|
80 end
|
Chris@909
|
81 end
|
Chris@909
|
82
|
Chris@1115
|
83 if default_value.present? && !valid_field_value?(default_value)
|
Chris@1115
|
84 errors.add(:default_value, :invalid)
|
Chris@1115
|
85 end
|
Chris@0
|
86 end
|
Chris@909
|
87
|
Chris@441
|
88 def possible_values_options(obj=nil)
|
Chris@441
|
89 case field_format
|
Chris@441
|
90 when 'user', 'version'
|
Chris@441
|
91 if obj.respond_to?(:project) && obj.project
|
Chris@441
|
92 case field_format
|
Chris@441
|
93 when 'user'
|
Chris@441
|
94 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
|
Chris@441
|
95 when 'version'
|
Chris@909
|
96 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
|
Chris@441
|
97 end
|
Chris@441
|
98 elsif obj.is_a?(Array)
|
Chris@1115
|
99 obj.collect {|o| possible_values_options(o)}.reduce(:&)
|
Chris@441
|
100 else
|
Chris@441
|
101 []
|
Chris@441
|
102 end
|
Chris@1115
|
103 when 'bool'
|
Chris@1115
|
104 [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']]
|
Chris@441
|
105 else
|
Chris@1115
|
106 possible_values || []
|
Chris@441
|
107 end
|
Chris@441
|
108 end
|
Chris@909
|
109
|
Chris@441
|
110 def possible_values(obj=nil)
|
Chris@441
|
111 case field_format
|
Chris@441
|
112 when 'user', 'version'
|
Chris@441
|
113 possible_values_options(obj).collect(&:last)
|
Chris@1115
|
114 when 'bool'
|
Chris@1115
|
115 ['1', '0']
|
Chris@441
|
116 else
|
Chris@1115
|
117 values = super()
|
Chris@1115
|
118 if values.is_a?(Array)
|
Chris@1115
|
119 values.each do |value|
|
Chris@1115
|
120 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
|
Chris@1115
|
121 end
|
Chris@1115
|
122 end
|
Chris@1115
|
123 values || []
|
Chris@441
|
124 end
|
Chris@441
|
125 end
|
Chris@909
|
126
|
Chris@0
|
127 # Makes possible_values accept a multiline string
|
Chris@0
|
128 def possible_values=(arg)
|
Chris@0
|
129 if arg.is_a?(Array)
|
Chris@1115
|
130 super(arg.compact.collect(&:strip).select {|v| !v.blank?})
|
Chris@0
|
131 else
|
Chris@0
|
132 self.possible_values = arg.to_s.split(/[\n\r]+/)
|
Chris@0
|
133 end
|
Chris@0
|
134 end
|
Chris@909
|
135
|
Chris@0
|
136 def cast_value(value)
|
Chris@0
|
137 casted = nil
|
Chris@0
|
138 unless value.blank?
|
Chris@0
|
139 case field_format
|
Chris@0
|
140 when 'string', 'text', 'list'
|
Chris@0
|
141 casted = value
|
Chris@0
|
142 when 'date'
|
Chris@0
|
143 casted = begin; value.to_date; rescue; nil end
|
Chris@0
|
144 when 'bool'
|
Chris@0
|
145 casted = (value == '1' ? true : false)
|
Chris@0
|
146 when 'int'
|
Chris@0
|
147 casted = value.to_i
|
Chris@0
|
148 when 'float'
|
Chris@0
|
149 casted = value.to_f
|
Chris@441
|
150 when 'user', 'version'
|
Chris@441
|
151 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
|
Chris@0
|
152 end
|
Chris@0
|
153 end
|
Chris@0
|
154 casted
|
Chris@0
|
155 end
|
Chris@909
|
156
|
Chris@1115
|
157 def value_from_keyword(keyword, customized)
|
Chris@1115
|
158 possible_values_options = possible_values_options(customized)
|
Chris@1115
|
159 if possible_values_options.present?
|
Chris@1115
|
160 keyword = keyword.to_s.downcase
|
Chris@1115
|
161 if v = possible_values_options.detect {|text, id| text.downcase == keyword}
|
Chris@1115
|
162 if v.is_a?(Array)
|
Chris@1115
|
163 v.last
|
Chris@1115
|
164 else
|
Chris@1115
|
165 v
|
Chris@1115
|
166 end
|
Chris@1115
|
167 end
|
Chris@1115
|
168 else
|
Chris@1115
|
169 keyword
|
Chris@1115
|
170 end
|
Chris@1115
|
171 end
|
Chris@1115
|
172
|
Chris@0
|
173 # Returns a ORDER BY clause that can used to sort customized
|
Chris@0
|
174 # objects by their value of the custom field.
|
Chris@1115
|
175 # Returns nil if the custom field can not be used for sorting.
|
Chris@0
|
176 def order_statement
|
Chris@1115
|
177 return nil if multiple?
|
Chris@0
|
178 case field_format
|
Chris@0
|
179 when 'string', 'text', 'list', 'date', 'bool'
|
Chris@0
|
180 # COALESCE is here to make sure that blank and NULL values are sorted equally
|
Chris@909
|
181 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
|
Chris@1115
|
182 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
|
Chris@0
|
183 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
|
Chris@0
|
184 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
|
Chris@0
|
185 when 'int', 'float'
|
Chris@0
|
186 # Make the database cast values into numeric
|
Chris@0
|
187 # Postgresql will raise an error if a value can not be casted!
|
Chris@0
|
188 # CustomValue validations should ensure that it doesn't occur
|
Chris@909
|
189 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
|
Chris@1115
|
190 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
|
Chris@0
|
191 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
|
Chris@0
|
192 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
|
Chris@1115
|
193 when 'user', 'version'
|
Chris@1115
|
194 value_class.fields_for_order_statement(value_join_alias)
|
Chris@0
|
195 else
|
Chris@0
|
196 nil
|
Chris@0
|
197 end
|
Chris@0
|
198 end
|
Chris@0
|
199
|
Chris@1115
|
200 # Returns a GROUP BY clause that can used to group by custom value
|
Chris@1115
|
201 # Returns nil if the custom field can not be used for grouping.
|
Chris@1115
|
202 def group_statement
|
Chris@1115
|
203 return nil if multiple?
|
Chris@1115
|
204 case field_format
|
Chris@1115
|
205 when 'list', 'date', 'bool', 'int'
|
Chris@1115
|
206 order_statement
|
Chris@1115
|
207 when 'user', 'version'
|
Chris@1115
|
208 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
|
Chris@1115
|
209 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
|
Chris@1115
|
210 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
|
Chris@1115
|
211 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
|
Chris@1115
|
212 else
|
Chris@1115
|
213 nil
|
Chris@1115
|
214 end
|
Chris@1115
|
215 end
|
Chris@1115
|
216
|
Chris@1115
|
217 def join_for_order_statement
|
Chris@1115
|
218 case field_format
|
Chris@1115
|
219 when 'user', 'version'
|
Chris@1115
|
220 "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
|
Chris@1115
|
221 " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
|
Chris@1115
|
222 " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
|
Chris@1115
|
223 " AND #{join_alias}.custom_field_id = #{id}" +
|
Chris@1115
|
224 " AND #{join_alias}.value <> ''" +
|
Chris@1115
|
225 " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
|
Chris@1115
|
226 " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
|
Chris@1115
|
227 " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
|
Chris@1115
|
228 " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" +
|
Chris@1115
|
229 " LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" +
|
Chris@1115
|
230 " ON CAST(#{join_alias}.value as decimal(60,0)) = #{value_join_alias}.id"
|
Chris@1115
|
231 else
|
Chris@1115
|
232 nil
|
Chris@1115
|
233 end
|
Chris@1115
|
234 end
|
Chris@1115
|
235
|
Chris@1115
|
236 def join_alias
|
Chris@1115
|
237 "cf_#{id}"
|
Chris@1115
|
238 end
|
Chris@1115
|
239
|
Chris@1115
|
240 def value_join_alias
|
Chris@1115
|
241 join_alias + "_" + field_format
|
Chris@1115
|
242 end
|
Chris@1115
|
243
|
Chris@0
|
244 def <=>(field)
|
Chris@0
|
245 position <=> field.position
|
Chris@0
|
246 end
|
Chris@909
|
247
|
Chris@1115
|
248 # Returns the class that values represent
|
Chris@1115
|
249 def value_class
|
Chris@1115
|
250 case field_format
|
Chris@1115
|
251 when 'user', 'version'
|
Chris@1115
|
252 field_format.classify.constantize
|
Chris@1115
|
253 else
|
Chris@1115
|
254 nil
|
Chris@1115
|
255 end
|
Chris@1115
|
256 end
|
Chris@1115
|
257
|
Chris@0
|
258 def self.customized_class
|
Chris@0
|
259 self.name =~ /^(.+)CustomField$/
|
Chris@0
|
260 begin; $1.constantize; rescue nil; end
|
Chris@0
|
261 end
|
Chris@909
|
262
|
Chris@0
|
263 # to move in project_custom_field
|
Chris@0
|
264 def self.for_all
|
Chris@0
|
265 find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
|
Chris@0
|
266 end
|
Chris@909
|
267
|
Chris@0
|
268 def type_name
|
Chris@0
|
269 nil
|
Chris@0
|
270 end
|
Chris@1115
|
271
|
Chris@1115
|
272 # Returns the error messages for the given value
|
Chris@1115
|
273 # or an empty array if value is a valid value for the custom field
|
Chris@1115
|
274 def validate_field_value(value)
|
Chris@1115
|
275 errs = []
|
Chris@1115
|
276 if value.is_a?(Array)
|
Chris@1115
|
277 if !multiple?
|
Chris@1115
|
278 errs << ::I18n.t('activerecord.errors.messages.invalid')
|
Chris@1115
|
279 end
|
Chris@1115
|
280 if is_required? && value.detect(&:present?).nil?
|
Chris@1115
|
281 errs << ::I18n.t('activerecord.errors.messages.blank')
|
Chris@1115
|
282 end
|
Chris@1115
|
283 value.each {|v| errs += validate_field_value_format(v)}
|
Chris@1115
|
284 else
|
Chris@1115
|
285 if is_required? && value.blank?
|
Chris@1115
|
286 errs << ::I18n.t('activerecord.errors.messages.blank')
|
Chris@1115
|
287 end
|
Chris@1115
|
288 errs += validate_field_value_format(value)
|
Chris@1115
|
289 end
|
Chris@1115
|
290 errs
|
Chris@1115
|
291 end
|
Chris@1115
|
292
|
Chris@1115
|
293 # Returns true if value is a valid value for the custom field
|
Chris@1115
|
294 def valid_field_value?(value)
|
Chris@1115
|
295 validate_field_value(value).empty?
|
Chris@1115
|
296 end
|
Chris@1115
|
297
|
Chris@1115
|
298 def format_in?(*args)
|
Chris@1115
|
299 args.include?(field_format)
|
Chris@1115
|
300 end
|
Chris@1115
|
301
|
Chris@1115
|
302 protected
|
Chris@1115
|
303
|
Chris@1115
|
304 # Returns the error message for the given value regarding its format
|
Chris@1115
|
305 def validate_field_value_format(value)
|
Chris@1115
|
306 errs = []
|
Chris@1115
|
307 if value.present?
|
Chris@1115
|
308 errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
|
Chris@1115
|
309 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
|
Chris@1115
|
310 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
|
Chris@1115
|
311
|
Chris@1115
|
312 # Format specific validations
|
Chris@1115
|
313 case field_format
|
Chris@1115
|
314 when 'int'
|
Chris@1115
|
315 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
|
Chris@1115
|
316 when 'float'
|
Chris@1115
|
317 begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
|
Chris@1115
|
318 when 'date'
|
Chris@1115
|
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
|
Chris@1115
|
320 when 'list'
|
Chris@1115
|
321 errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
|
Chris@1115
|
322 end
|
Chris@1115
|
323 end
|
Chris@1115
|
324 errs
|
Chris@1115
|
325 end
|
Chris@0
|
326 end
|