##// END OF EJS Templates
Do not validate blank default custom field value....
Jean-Philippe Lang -
r8602:8d5f932660f9
parent child
Show More
@@ -1,217 +1,217
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 unless valid_field_value?(default_value)
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)}.inject {|memo, v| memo & v}
77 77 else
78 78 []
79 79 end
80 80 else
81 81 read_attribute :possible_values
82 82 end
83 83 end
84 84
85 85 def possible_values(obj=nil)
86 86 case field_format
87 87 when 'user', 'version'
88 88 possible_values_options(obj).collect(&:last)
89 89 else
90 90 read_attribute :possible_values
91 91 end
92 92 end
93 93
94 94 # Makes possible_values accept a multiline string
95 95 def possible_values=(arg)
96 96 if arg.is_a?(Array)
97 97 write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?})
98 98 else
99 99 self.possible_values = arg.to_s.split(/[\n\r]+/)
100 100 end
101 101 end
102 102
103 103 def cast_value(value)
104 104 casted = nil
105 105 unless value.blank?
106 106 case field_format
107 107 when 'string', 'text', 'list'
108 108 casted = value
109 109 when 'date'
110 110 casted = begin; value.to_date; rescue; nil end
111 111 when 'bool'
112 112 casted = (value == '1' ? true : false)
113 113 when 'int'
114 114 casted = value.to_i
115 115 when 'float'
116 116 casted = value.to_f
117 117 when 'user', 'version'
118 118 casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
119 119 end
120 120 end
121 121 casted
122 122 end
123 123
124 124 # Returns a ORDER BY clause that can used to sort customized
125 125 # objects by their value of the custom field.
126 126 # Returns false, if the custom field can not be used for sorting.
127 127 def order_statement
128 128 return nil if multiple?
129 129 case field_format
130 130 when 'string', 'text', 'list', 'date', 'bool'
131 131 # COALESCE is here to make sure that blank and NULL values are sorted equally
132 132 "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
133 133 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
134 134 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
135 135 " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
136 136 when 'int', 'float'
137 137 # Make the database cast values into numeric
138 138 # Postgresql will raise an error if a value can not be casted!
139 139 # CustomValue validations should ensure that it doesn't occur
140 140 "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
141 141 " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" +
142 142 " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
143 143 " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
144 144 else
145 145 nil
146 146 end
147 147 end
148 148
149 149 def <=>(field)
150 150 position <=> field.position
151 151 end
152 152
153 153 def self.customized_class
154 154 self.name =~ /^(.+)CustomField$/
155 155 begin; $1.constantize; rescue nil; end
156 156 end
157 157
158 158 # to move in project_custom_field
159 159 def self.for_all
160 160 find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
161 161 end
162 162
163 163 def type_name
164 164 nil
165 165 end
166 166
167 167 # Returns the error messages for the given value
168 168 # or an empty array if value is a valid value for the custom field
169 169 def validate_field_value(value)
170 170 errs = []
171 171 if value.is_a?(Array)
172 172 if !multiple?
173 173 errs << ::I18n.t('activerecord.errors.messages.invalid')
174 174 end
175 175 if is_required? && value.detect(&:present?).nil?
176 176 errs << ::I18n.t('activerecord.errors.messages.blank')
177 177 end
178 178 value.each {|v| errs += validate_field_value_format(v)}
179 179 else
180 180 if is_required? && value.blank?
181 181 errs << ::I18n.t('activerecord.errors.messages.blank')
182 182 end
183 183 errs += validate_field_value_format(value)
184 184 end
185 185 errs
186 186 end
187 187
188 188 # Returns true if value is a valid value for the custom field
189 189 def valid_field_value?(value)
190 190 validate_field_value(value).empty?
191 191 end
192 192
193 193 protected
194 194
195 195 # Returns the error message for the given value regarding its format
196 196 def validate_field_value_format(value)
197 197 errs = []
198 198 if value.present?
199 199 errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
200 200 errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
201 201 errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
202 202
203 203 # Format specific validations
204 204 case field_format
205 205 when 'int'
206 206 errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
207 207 when 'float'
208 208 begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
209 209 when 'date'
210 210 errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
211 211 when 'list'
212 212 errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
213 213 end
214 214 end
215 215 errs
216 216 end
217 217 end
@@ -1,189 +1,194
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 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class CustomFieldTest < ActiveSupport::TestCase
21 21 fixtures :custom_fields
22 22
23 23 def test_create
24 24 field = UserCustomField.new(:name => 'Money money money', :field_format => 'float')
25 25 assert field.save
26 26 end
27 27
28 28 def test_before_validation
29 29 field = CustomField.new(:name => 'test_before_validation', :field_format => 'int')
30 30 field.searchable = true
31 31 assert field.save
32 32 assert_equal false, field.searchable
33 33 field.searchable = true
34 34 assert field.save
35 35 assert_equal false, field.searchable
36 36 end
37 37
38 38 def test_regexp_validation
39 39 field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9')
40 40 assert !field.save
41 41 assert_equal I18n.t('activerecord.errors.messages.invalid'),
42 42 field.errors[:regexp].to_s
43 43 field.regexp = '[a-z0-9]'
44 44 assert field.save
45 45 end
46 46
47 47 def test_default_value_should_be_validated
48 48 field = CustomField.new(:name => 'Test', :field_format => 'int')
49 49 field.default_value = 'abc'
50 50 assert !field.valid?
51 51 field.default_value = '6'
52 52 assert field.valid?
53 53 end
54 54
55 def test_default_value_should_not_be_validated_when_blank
56 field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '')
57 assert field.valid?
58 end
59
55 60 def test_possible_values_should_accept_an_array
56 61 field = CustomField.new
57 62 field.possible_values = ["One value", ""]
58 63 assert_equal ["One value"], field.possible_values
59 64 end
60 65
61 66 def test_possible_values_should_accept_a_string
62 67 field = CustomField.new
63 68 field.possible_values = "One value"
64 69 assert_equal ["One value"], field.possible_values
65 70 end
66 71
67 72 def test_possible_values_should_accept_a_multiline_string
68 73 field = CustomField.new
69 74 field.possible_values = "One value\nAnd another one \r\n \n"
70 75 assert_equal ["One value", "And another one"], field.possible_values
71 76 end
72 77
73 78 def test_destroy
74 79 field = CustomField.find(1)
75 80 assert field.destroy
76 81 end
77 82
78 83 def test_new_subclass_instance_should_return_an_instance
79 84 f = CustomField.new_subclass_instance('IssueCustomField')
80 85 assert_kind_of IssueCustomField, f
81 86 end
82 87
83 88 def test_new_subclass_instance_should_set_attributes
84 89 f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test')
85 90 assert_kind_of IssueCustomField, f
86 91 assert_equal 'Test', f.name
87 92 end
88 93
89 94 def test_new_subclass_instance_with_invalid_class_name_should_return_nil
90 95 assert_nil CustomField.new_subclass_instance('WrongClassName')
91 96 end
92 97
93 98 def test_new_subclass_instance_with_non_subclass_name_should_return_nil
94 99 assert_nil CustomField.new_subclass_instance('Project')
95 100 end
96 101
97 102 def test_string_field_validation_with_blank_value
98 103 f = CustomField.new(:field_format => 'string')
99 104
100 105 assert f.valid_field_value?(nil)
101 106 assert f.valid_field_value?('')
102 107
103 108 f.is_required = true
104 109 assert !f.valid_field_value?(nil)
105 110 assert !f.valid_field_value?('')
106 111 end
107 112
108 113 def test_string_field_validation_with_min_and_max_lengths
109 114 f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5)
110 115
111 116 assert f.valid_field_value?(nil)
112 117 assert f.valid_field_value?('')
113 118 assert f.valid_field_value?('a' * 2)
114 119 assert !f.valid_field_value?('a')
115 120 assert !f.valid_field_value?('a' * 6)
116 121 end
117 122
118 123 def test_string_field_validation_with_regexp
119 124 f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$')
120 125
121 126 assert f.valid_field_value?(nil)
122 127 assert f.valid_field_value?('')
123 128 assert f.valid_field_value?('ABC')
124 129 assert !f.valid_field_value?('abc')
125 130 end
126 131
127 132 def test_date_field_validation
128 133 f = CustomField.new(:field_format => 'date')
129 134
130 135 assert f.valid_field_value?(nil)
131 136 assert f.valid_field_value?('')
132 137 assert f.valid_field_value?('1975-07-14')
133 138 assert !f.valid_field_value?('1975-07-33')
134 139 assert !f.valid_field_value?('abc')
135 140 end
136 141
137 142 def test_list_field_validation
138 143 f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2'])
139 144
140 145 assert f.valid_field_value?(nil)
141 146 assert f.valid_field_value?('')
142 147 assert f.valid_field_value?('value2')
143 148 assert !f.valid_field_value?('abc')
144 149 end
145 150
146 151 def test_int_field_validation
147 152 f = CustomField.new(:field_format => 'int')
148 153
149 154 assert f.valid_field_value?(nil)
150 155 assert f.valid_field_value?('')
151 156 assert f.valid_field_value?('123')
152 157 assert f.valid_field_value?('+123')
153 158 assert f.valid_field_value?('-123')
154 159 assert !f.valid_field_value?('6abc')
155 160 end
156 161
157 162 def test_float_field_validation
158 163 f = CustomField.new(:field_format => 'float')
159 164
160 165 assert f.valid_field_value?(nil)
161 166 assert f.valid_field_value?('')
162 167 assert f.valid_field_value?('11.2')
163 168 assert f.valid_field_value?('-6.250')
164 169 assert f.valid_field_value?('5')
165 170 assert !f.valid_field_value?('6abc')
166 171 end
167 172
168 173 def test_multi_field_validation
169 174 f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2'])
170 175
171 176 assert f.valid_field_value?(nil)
172 177 assert f.valid_field_value?('')
173 178 assert f.valid_field_value?([])
174 179 assert f.valid_field_value?([nil])
175 180 assert f.valid_field_value?([''])
176 181
177 182 assert f.valid_field_value?('value2')
178 183 assert !f.valid_field_value?('abc')
179 184
180 185 assert f.valid_field_value?(['value2'])
181 186 assert !f.valid_field_value?(['abc'])
182 187
183 188 assert f.valid_field_value?(['', 'value2'])
184 189 assert !f.valid_field_value?(['', 'abc'])
185 190
186 191 assert f.valid_field_value?(['value1', 'value2'])
187 192 assert !f.valid_field_value?(['value1', 'abc'])
188 193 end
189 194 end
General Comments 0
You need to be logged in to leave comments. Login now