##// END OF EJS Templates
Use base class name as customized type to fix @UserCustomField#order_statement@....
Etienne Massip -
r9697:0cc817fba324
parent child
Show More
@@ -1,227 +1,228
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class CustomField < ActiveRecord::Base
19 19 include Redmine::SubclassFactory
20 20
21 21 has_many :custom_values, :dependent => :delete_all
22 22 acts_as_list :scope => 'type = \'#{self.class}\''
23 23 serialize :possible_values
24 24
25 25 validates_presence_of :name, :field_format
26 26 validates_uniqueness_of :name, :scope => :type
27 27 validates_length_of :name, :maximum => 30
28 28 validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
29 29
30 30 validate :validate_custom_field
31 31 before_validation :set_searchable
32 32
33 33 def initialize(attributes=nil, *args)
34 34 super
35 35 self.possible_values ||= []
36 36 end
37 37
38 38 def set_searchable
39 39 # make sure these fields are not searchable
40 40 self.searchable = false if %w(int float date bool).include?(field_format)
41 41 # make sure only these fields can have multiple values
42 42 self.multiple = false unless %w(list user version).include?(field_format)
43 43 true
44 44 end
45 45
46 46 def validate_custom_field
47 47 if self.field_format == "list"
48 48 errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
49 49 errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
50 50 end
51 51
52 52 if regexp.present?
53 53 begin
54 54 Regexp.new(regexp)
55 55 rescue
56 56 errors.add(:regexp, :invalid)
57 57 end
58 58 end
59 59
60 60 if default_value.present? && !valid_field_value?(default_value)
61 61 errors.add(:default_value, :invalid)
62 62 end
63 63 end
64 64
65 65 def possible_values_options(obj=nil)
66 66 case field_format
67 67 when 'user', 'version'
68 68 if obj.respond_to?(:project) && obj.project
69 69 case field_format
70 70 when 'user'
71 71 obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
72 72 when 'version'
73 73 obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
74 74 end
75 75 elsif obj.is_a?(Array)
76 76 obj.collect {|o| possible_values_options(o)}.reduce(:&)
77 77 else
78 78 []
79 79 end
80 80 when 'bool'
81 81 [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']]
82 82 else
83 83 possible_values || []
84 84 end
85 85 end
86 86
87 87 def possible_values(obj=nil)
88 88 case field_format
89 89 when 'user', 'version'
90 90 possible_values_options(obj).collect(&:last)
91 91 when 'bool'
92 92 ['1', '0']
93 93 else
94 94 values = super()
95 95 if values.is_a?(Array)
96 96 values.each do |value|
97 97 value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
98 98 end
99 99 end
100 100 values
101 101 end
102 102 end
103 103
104 104 # Makes possible_values accept a multiline string
105 105 def possible_values=(arg)
106 106 if arg.is_a?(Array)
107 107 super(arg.compact.collect(&:strip).select {|v| !v.blank?})
108 108 else
109 109 self.possible_values = arg.to_s.split(/[\n\r]+/)
110 110 end
111 111 end
112 112
113 113 def cast_value(value)
114 114 casted = nil
115 115 unless value.blank?
116 116 case field_format
117 117 when 'string', 'text', 'list'
118 118 casted = value
119 119 when 'date'
120 120 casted = begin; value.to_date; rescue; nil end
121 121 when 'bool'
122 122 casted = (value == '1' ? true : false)
123 123 when 'int'
124 124 casted = value.to_i
125 125 when 'float'
126 126 casted = value.to_f
127 127 when 'user', 'version'
128 128 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
129 129 end
130 130 end
131 131 casted
132 132 end
133 133
134 134 # Returns a ORDER BY clause that can used to sort customized
135 135 # objects by their value of the custom field.
136 136 # Returns false, if the custom field can not be used for sorting.
137 137 def order_statement
138 138 return nil if multiple?
139 139 case field_format
140 140 when 'string', 'text', 'list', 'date', 'bool'
141 141 # COALESCE is here to make sure that blank and NULL values are sorted equally
142 self.custom_values.first.to_sql
142 143 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
143 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
144 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
144 145 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
145 146 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
146 147 when 'int', 'float'
147 148 # Make the database cast values into numeric
148 149 # Postgresql will raise an error if a value can not be casted!
149 150 # CustomValue validations should ensure that it doesn't occur
150 151 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
151 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
152 " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
152 153 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
153 154 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
154 155 else
155 156 nil
156 157 end
157 158 end
158 159
159 160 def <=>(field)
160 161 position <=> field.position
161 162 end
162 163
163 164 def self.customized_class
164 165 self.name =~ /^(.+)CustomField$/
165 166 begin; $1.constantize; rescue nil; end
166 167 end
167 168
168 169 # to move in project_custom_field
169 170 def self.for_all
170 171 find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
171 172 end
172 173
173 174 def type_name
174 175 nil
175 176 end
176 177
177 178 # Returns the error messages for the given value
178 179 # or an empty array if value is a valid value for the custom field
179 180 def validate_field_value(value)
180 181 errs = []
181 182 if value.is_a?(Array)
182 183 if !multiple?
183 184 errs << ::I18n.t('activerecord.errors.messages.invalid')
184 185 end
185 186 if is_required? && value.detect(&:present?).nil?
186 187 errs << ::I18n.t('activerecord.errors.messages.blank')
187 188 end
188 189 value.each {|v| errs += validate_field_value_format(v)}
189 190 else
190 191 if is_required? && value.blank?
191 192 errs << ::I18n.t('activerecord.errors.messages.blank')
192 193 end
193 194 errs += validate_field_value_format(value)
194 195 end
195 196 errs
196 197 end
197 198
198 199 # Returns true if value is a valid value for the custom field
199 200 def valid_field_value?(value)
200 201 validate_field_value(value).empty?
201 202 end
202 203
203 204 protected
204 205
205 206 # Returns the error message for the given value regarding its format
206 207 def validate_field_value_format(value)
207 208 errs = []
208 209 if value.present?
209 210 errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
210 211 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
211 212 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
212 213
213 214 # Format specific validations
214 215 case field_format
215 216 when 'int'
216 217 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
217 218 when 'float'
218 219 begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
219 220 when 'date'
220 221 errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
221 222 when 'list'
222 223 errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
223 224 end
224 225 end
225 226 errs
226 227 end
227 228 end
General Comments 0
You need to be logged in to leave comments. Login now