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