Mercurial > hg > soundsoftware-site
comparison app/models/custom_field.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 261b3d9a4903 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 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. |
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 class CustomField < ActiveRecord::Base | 18 class CustomField < ActiveRecord::Base |
19 include Redmine::SubclassFactory | |
20 | |
19 has_many :custom_values, :dependent => :delete_all | 21 has_many :custom_values, :dependent => :delete_all |
20 acts_as_list :scope => 'type = \'#{self.class}\'' | 22 acts_as_list :scope => 'type = \'#{self.class}\'' |
21 serialize :possible_values | 23 serialize :possible_values |
22 | 24 |
23 validates_presence_of :name, :field_format | 25 validates_presence_of :name, :field_format |
24 validates_uniqueness_of :name, :scope => :type | 26 validates_uniqueness_of :name, :scope => :type |
25 validates_length_of :name, :maximum => 30 | 27 validates_length_of :name, :maximum => 30 |
26 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats | 28 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats |
27 | 29 |
28 validate :validate_values | 30 validate :validate_custom_field |
29 | 31 before_validation :set_searchable |
30 def initialize(attributes = nil) | 32 |
31 super | 33 CUSTOM_FIELDS_TABS = [ |
32 self.possible_values ||= [] | 34 {:name => 'IssueCustomField', :partial => 'custom_fields/index', |
33 end | 35 :label => :label_issue_plural}, |
34 | 36 {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index', |
35 def before_validation | 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 | |
36 # make sure these fields are not searchable | 62 # make sure these fields are not searchable |
37 self.searchable = false if %w(int float date bool).include?(field_format) | 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) | |
38 true | 66 true |
39 end | 67 end |
40 | 68 |
41 def validate_values | 69 def validate_custom_field |
42 if self.field_format == "list" | 70 if self.field_format == "list" |
43 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? | 71 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? |
44 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array | 72 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array |
45 end | 73 end |
46 | 74 |
50 rescue | 78 rescue |
51 errors.add(:regexp, :invalid) | 79 errors.add(:regexp, :invalid) |
52 end | 80 end |
53 end | 81 end |
54 | 82 |
55 # validate default value | 83 if default_value.present? && !valid_field_value?(default_value) |
56 v = CustomValue.new(:custom_field => self.clone, :value => default_value, :customized => nil) | 84 errors.add(:default_value, :invalid) |
57 v.custom_field.is_required = false | 85 end |
58 errors.add(:default_value, :invalid) unless v.valid? | |
59 end | 86 end |
60 | 87 |
61 def possible_values_options(obj=nil) | 88 def possible_values_options(obj=nil) |
62 case field_format | 89 case field_format |
63 when 'user', 'version' | 90 when 'user', 'version' |
67 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} | 94 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} |
68 when 'version' | 95 when 'version' |
69 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} | 96 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} |
70 end | 97 end |
71 elsif obj.is_a?(Array) | 98 elsif obj.is_a?(Array) |
72 obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v} | 99 obj.collect {|o| possible_values_options(o)}.reduce(:&) |
73 else | 100 else |
74 [] | 101 [] |
75 end | 102 end |
76 else | 103 when 'bool' |
77 read_attribute :possible_values | 104 [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']] |
105 else | |
106 possible_values || [] | |
78 end | 107 end |
79 end | 108 end |
80 | 109 |
81 def possible_values(obj=nil) | 110 def possible_values(obj=nil) |
82 case field_format | 111 case field_format |
83 when 'user', 'version' | 112 when 'user', 'version' |
84 possible_values_options(obj).collect(&:last) | 113 possible_values_options(obj).collect(&:last) |
85 else | 114 when 'bool' |
86 read_attribute :possible_values | 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 || [] | |
87 end | 124 end |
88 end | 125 end |
89 | 126 |
90 # Makes possible_values accept a multiline string | 127 # Makes possible_values accept a multiline string |
91 def possible_values=(arg) | 128 def possible_values=(arg) |
92 if arg.is_a?(Array) | 129 if arg.is_a?(Array) |
93 write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?}) | 130 super(arg.compact.collect(&:strip).select {|v| !v.blank?}) |
94 else | 131 else |
95 self.possible_values = arg.to_s.split(/[\n\r]+/) | 132 self.possible_values = arg.to_s.split(/[\n\r]+/) |
96 end | 133 end |
97 end | 134 end |
98 | 135 |
115 end | 152 end |
116 end | 153 end |
117 casted | 154 casted |
118 end | 155 end |
119 | 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 | |
120 # Returns a ORDER BY clause that can used to sort customized | 173 # Returns a ORDER BY clause that can used to sort customized |
121 # objects by their value of the custom field. | 174 # objects by their value of the custom field. |
122 # Returns false, if the custom field can not be used for sorting. | 175 # Returns nil if the custom field can not be used for sorting. |
123 def order_statement | 176 def order_statement |
177 return nil if multiple? | |
124 case field_format | 178 case field_format |
125 when 'string', 'text', 'list', 'date', 'bool' | 179 when 'string', 'text', 'list', 'date', 'bool' |
126 # COALESCE is here to make sure that blank and NULL values are sorted equally | 180 # COALESCE is here to make sure that blank and NULL values are sorted equally |
127 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" + | 181 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" + |
128 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | 182 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" + |
129 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | 183 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
130 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" | 184 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" |
131 when 'int', 'float' | 185 when 'int', 'float' |
132 # Make the database cast values into numeric | 186 # Make the database cast values into numeric |
133 # Postgresql will raise an error if a value can not be casted! | 187 # Postgresql will raise an error if a value can not be casted! |
134 # CustomValue validations should ensure that it doesn't occur | 188 # CustomValue validations should ensure that it doesn't occur |
135 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" + | 189 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" + |
136 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | 190 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" + |
137 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | 191 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
138 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" | 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) | |
139 else | 195 else |
140 nil | 196 nil |
141 end | 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 | |
142 end | 242 end |
143 | 243 |
144 def <=>(field) | 244 def <=>(field) |
145 position <=> field.position | 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 | |
146 end | 256 end |
147 | 257 |
148 def self.customized_class | 258 def self.customized_class |
149 self.name =~ /^(.+)CustomField$/ | 259 self.name =~ /^(.+)CustomField$/ |
150 begin; $1.constantize; rescue nil; end | 260 begin; $1.constantize; rescue nil; end |
156 end | 266 end |
157 | 267 |
158 def type_name | 268 def type_name |
159 nil | 269 nil |
160 end | 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 | |
161 end | 326 end |