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