@@ -1,73 +1,73 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | validate :validate_custom_value |
|
23 | 23 | |
|
24 | 24 | def after_initialize |
|
25 | 25 | if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?)) |
|
26 | 26 | self.value ||= custom_field.default_value |
|
27 | 27 | end |
|
28 | 28 | end |
|
29 | 29 | |
|
30 | 30 | # Returns true if the boolean custom value is true |
|
31 | 31 | def true? |
|
32 | 32 | self.value == '1' |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def editable? |
|
36 | 36 | custom_field.editable? |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def visible? |
|
40 | 40 | custom_field.visible? |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def required? |
|
44 | 44 | custom_field.is_required? |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def to_s |
|
48 | 48 | value.to_s |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | protected |
|
52 | 52 | def validate_custom_value |
|
53 | 53 | if value.blank? |
|
54 | 54 | errors.add(:value, :blank) if custom_field.is_required? and value.blank? |
|
55 | 55 | else |
|
56 | 56 | errors.add(:value, :invalid) unless custom_field.regexp.blank? or value =~ Regexp.new(custom_field.regexp) |
|
57 | 57 | errors.add(:value, :too_short, :count => custom_field.min_length) if custom_field.min_length > 0 and value.length < custom_field.min_length |
|
58 | 58 | errors.add(:value, :too_long, :count => custom_field.max_length) if custom_field.max_length > 0 and value.length > custom_field.max_length |
|
59 | 59 | |
|
60 | 60 | # Format specific validations |
|
61 | 61 | case custom_field.field_format |
|
62 | 62 | when 'int' |
|
63 | 63 | errors.add(:value, :not_a_number) unless value =~ /^[+-]?\d+$/ |
|
64 | 64 | when 'float' |
|
65 | 65 | begin; Kernel.Float(value); rescue; errors.add(:value, :invalid) end |
|
66 | 66 | when 'date' |
|
67 | errors.add(:value, :not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ | |
|
67 | errors.add(:value, :not_a_date) unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end | |
|
68 | 68 | when 'list' |
|
69 | 69 | errors.add(:value, :inclusion) unless custom_field.possible_values.include?(value) |
|
70 | 70 | end |
|
71 | 71 | end |
|
72 | 72 | end |
|
73 | 73 | end |
@@ -1,123 +1,125 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 CustomValueTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :custom_fields, :custom_values, :users |
|
22 | 22 | |
|
23 | 23 | def test_string_field_validation_with_blank_value |
|
24 | 24 | f = CustomField.new(:field_format => 'string') |
|
25 | 25 | v = CustomValue.new(:custom_field => f) |
|
26 | 26 | |
|
27 | 27 | v.value = nil |
|
28 | 28 | assert v.valid? |
|
29 | 29 | v.value = '' |
|
30 | 30 | assert v.valid? |
|
31 | 31 | |
|
32 | 32 | f.is_required = true |
|
33 | 33 | v.value = nil |
|
34 | 34 | assert !v.valid? |
|
35 | 35 | v.value = '' |
|
36 | 36 | assert !v.valid? |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def test_string_field_validation_with_min_and_max_lengths |
|
40 | 40 | f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) |
|
41 | 41 | v = CustomValue.new(:custom_field => f, :value => '') |
|
42 | 42 | assert v.valid? |
|
43 | 43 | v.value = 'a' |
|
44 | 44 | assert !v.valid? |
|
45 | 45 | v.value = 'a' * 2 |
|
46 | 46 | assert v.valid? |
|
47 | 47 | v.value = 'a' * 6 |
|
48 | 48 | assert !v.valid? |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | def test_string_field_validation_with_regexp |
|
52 | 52 | f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') |
|
53 | 53 | v = CustomValue.new(:custom_field => f, :value => '') |
|
54 | 54 | assert v.valid? |
|
55 | 55 | v.value = 'abc' |
|
56 | 56 | assert !v.valid? |
|
57 | 57 | v.value = 'ABC' |
|
58 | 58 | assert v.valid? |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def test_date_field_validation |
|
62 | 62 | f = CustomField.new(:field_format => 'date') |
|
63 | 63 | v = CustomValue.new(:custom_field => f, :value => '') |
|
64 | 64 | assert v.valid? |
|
65 | 65 | v.value = 'abc' |
|
66 | 66 | assert !v.valid? |
|
67 | v.value = '1975-07-33' | |
|
68 | assert !v.valid? | |
|
67 | 69 | v.value = '1975-07-14' |
|
68 | 70 | assert v.valid? |
|
69 | 71 | end |
|
70 | 72 | |
|
71 | 73 | def test_list_field_validation |
|
72 | 74 | f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) |
|
73 | 75 | v = CustomValue.new(:custom_field => f, :value => '') |
|
74 | 76 | assert v.valid? |
|
75 | 77 | v.value = 'abc' |
|
76 | 78 | assert !v.valid? |
|
77 | 79 | v.value = 'value2' |
|
78 | 80 | assert v.valid? |
|
79 | 81 | end |
|
80 | 82 | |
|
81 | 83 | def test_int_field_validation |
|
82 | 84 | f = CustomField.new(:field_format => 'int') |
|
83 | 85 | v = CustomValue.new(:custom_field => f, :value => '') |
|
84 | 86 | assert v.valid? |
|
85 | 87 | v.value = 'abc' |
|
86 | 88 | assert !v.valid? |
|
87 | 89 | v.value = '123' |
|
88 | 90 | assert v.valid? |
|
89 | 91 | v.value = '+123' |
|
90 | 92 | assert v.valid? |
|
91 | 93 | v.value = '-123' |
|
92 | 94 | assert v.valid? |
|
93 | 95 | end |
|
94 | 96 | |
|
95 | 97 | def test_float_field_validation |
|
96 | 98 | v = CustomValue.new(:customized => User.find(:first), :custom_field => UserCustomField.find_by_name('Money')) |
|
97 | 99 | v.value = '11.2' |
|
98 | 100 | assert v.save |
|
99 | 101 | v.value = '' |
|
100 | 102 | assert v.save |
|
101 | 103 | v.value = '-6.250' |
|
102 | 104 | assert v.save |
|
103 | 105 | v.value = '6a' |
|
104 | 106 | assert !v.save |
|
105 | 107 | end |
|
106 | 108 | |
|
107 | 109 | def test_default_value |
|
108 | 110 | field = CustomField.find_by_default_value('Default string') |
|
109 | 111 | assert_not_nil field |
|
110 | 112 | |
|
111 | 113 | v = CustomValue.new(:custom_field => field) |
|
112 | 114 | assert_equal 'Default string', v.value |
|
113 | 115 | |
|
114 | 116 | v = CustomValue.new(:custom_field => field, :value => 'Not empty') |
|
115 | 117 | assert_equal 'Not empty', v.value |
|
116 | 118 | end |
|
117 | 119 | |
|
118 | 120 | def test_sti_polymorphic_association |
|
119 | 121 | # Rails uses top level sti class for polymorphic association. See #3978. |
|
120 | 122 | assert !User.find(4).custom_values.empty? |
|
121 | 123 | assert !CustomValue.find(2).customized.nil? |
|
122 | 124 | end |
|
123 | 125 | end |
General Comments 0
You need to be logged in to leave comments.
Login now