##// END OF EJS Templates
Prevent unexpected nil in custom value validation....
Jean-Philippe Lang -
r1087:e13f49d91951
parent child
Show More
@@ -1,45 +1,50
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 CustomValue < ActiveRecord::Base
19 19 belongs_to :custom_field
20 20 belongs_to :customized, :polymorphic => true
21 21
22 22 def after_initialize
23 23 if custom_field && new_record? && (customized_type.blank? || (customized && customized.new_record?))
24 24 self.value ||= custom_field.default_value
25 25 end
26 26 end
27 27
28 28 protected
29 29 def validate
30 errors.add(:value, :activerecord_error_blank) and return if custom_field.is_required? and value.blank?
31 errors.add(:value, :activerecord_error_invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
32 errors.add(:value, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length and value.length > 0
33 errors.add(:value, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length
34 case custom_field.field_format
35 when 'int'
36 errors.add(:value, :activerecord_error_not_a_number) unless value.blank? || value =~ /^[+-]?\d+$/
37 when 'float'
38 begin; !value.blank? && Kernel.Float(value); rescue; errors.add(:value, :activerecord_error_invalid) end
39 when 'date'
40 errors.add(:value, :activerecord_error_not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ or value.blank?
41 when 'list'
42 errors.add(:value, :activerecord_error_inclusion) unless custom_field.possible_values.include?(value) or value.blank?
30 if value.blank?
31 errors.add(:value, :activerecord_error_blank) if custom_field.is_required? and value.blank?
32 else
33 errors.add(:value, :activerecord_error_invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp)
34 errors.add(:value, :activerecord_error_too_short) if custom_field.min_length > 0 and value.length < custom_field.min_length
35 errors.add(:value, :activerecord_error_too_long) if custom_field.max_length > 0 and value.length > custom_field.max_length
36
37 # Format specific validations
38 case custom_field.field_format
39 when 'int'
40 errors.add(:value, :activerecord_error_not_a_number) unless value =~ /^[+-]?\d+$/
41 when 'float'
42 begin; Kernel.Float(value); rescue; errors.add(:value, :activerecord_error_invalid) end
43 when 'date'
44 errors.add(:value, :activerecord_error_not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/
45 when 'list'
46 errors.add(:value, :activerecord_error_inclusion) unless custom_field.possible_values.include?(value)
47 end
43 48 end
44 49 end
45 50 end
@@ -1,45 +1,117
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class CustomValueTest < Test::Unit::TestCase
21 21 fixtures :custom_fields
22 22
23 def test_float_field
23 def test_string_field_validation_with_blank_value
24 f = CustomField.new(:field_format => 'string')
25 v = CustomValue.new(:custom_field => f)
26
27 v.value = nil
28 assert v.valid?
29 v.value = ''
30 assert v.valid?
31
32 f.is_required = true
33 v.value = nil
34 assert !v.valid?
35 v.value = ''
36 assert !v.valid?
37 end
38
39 def test_string_field_validation_with_min_and_max_lengths
40 f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5)
41 v = CustomValue.new(:custom_field => f, :value => '')
42 assert v.valid?
43 v.value = 'a'
44 assert !v.valid?
45 v.value = 'a' * 2
46 assert v.valid?
47 v.value = 'a' * 6
48 assert !v.valid?
49 end
50
51 def test_string_field_validation_with_regexp
52 f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$')
53 v = CustomValue.new(:custom_field => f, :value => '')
54 assert v.valid?
55 v.value = 'abc'
56 assert !v.valid?
57 v.value = 'ABC'
58 assert v.valid?
59 end
60
61 def test_date_field_validation
62 f = CustomField.new(:field_format => 'date')
63 v = CustomValue.new(:custom_field => f, :value => '')
64 assert v.valid?
65 v.value = 'abc'
66 assert !v.valid?
67 v.value = '1975-07-14'
68 assert v.valid?
69 end
70
71 def test_list_field_validation
72 f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2'])
73 v = CustomValue.new(:custom_field => f, :value => '')
74 assert v.valid?
75 v.value = 'abc'
76 assert !v.valid?
77 v.value = 'value2'
78 assert v.valid?
79 end
80
81 def test_int_field_validation
82 f = CustomField.new(:field_format => 'int')
83 v = CustomValue.new(:custom_field => f, :value => '')
84 assert v.valid?
85 v.value = 'abc'
86 assert !v.valid?
87 v.value = '123'
88 assert v.valid?
89 v.value = '+123'
90 assert v.valid?
91 v.value = '-123'
92 assert v.valid?
93 end
94
95 def test_float_field_validation
24 96 v = CustomValue.new(:customized => User.find(:first), :custom_field => UserCustomField.find_by_name('Money'))
25 97 v.value = '11.2'
26 98 assert v.save
27 99 v.value = ''
28 100 assert v.save
29 101 v.value = '-6.250'
30 102 assert v.save
31 103 v.value = '6a'
32 104 assert !v.save
33 105 end
34 106
35 107 def test_default_value
36 108 field = CustomField.find_by_default_value('Default string')
37 109 assert_not_nil field
38 110
39 111 v = CustomValue.new(:custom_field => field)
40 112 assert_equal 'Default string', v.value
41 113
42 114 v = CustomValue.new(:custom_field => field, :value => 'Not empty')
43 115 assert_equal 'Not empty', v.value
44 116 end
45 117 end
General Comments 0
You need to be logged in to leave comments. Login now