validates_timeliness 3.0.8 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.travis.yml +21 -0
- data/CHANGELOG.rdoc +21 -1
- data/README.rdoc +20 -22
- data/Rakefile +5 -1
- data/gemfiles/rails_4_0.gemfile +19 -0
- data/gemfiles/rails_4_1.gemfile +19 -0
- data/gemfiles/rails_4_2.gemfile +19 -0
- data/lib/generators/validates_timeliness/install_generator.rb +0 -1
- data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +3 -3
- data/lib/validates_timeliness/attribute_methods.rb +42 -26
- data/lib/validates_timeliness/extensions/date_time_select.rb +2 -9
- data/lib/validates_timeliness/extensions/multiparameter_handler.rb +59 -54
- data/lib/validates_timeliness/extensions.rb +3 -4
- data/lib/validates_timeliness/helper_methods.rb +7 -6
- data/lib/validates_timeliness/orm/active_record.rb +70 -15
- data/lib/validates_timeliness/railtie.rb +2 -1
- data/lib/validates_timeliness/validator.rb +29 -4
- data/lib/validates_timeliness/version.rb +1 -1
- data/lib/validates_timeliness.rb +13 -13
- data/spec/spec_helper.rb +10 -5
- data/spec/support/config_helper.rb +10 -0
- data/spec/support/model_helpers.rb +4 -4
- data/spec/support/tag_matcher.rb +35 -0
- data/spec/support/test_model.rb +0 -1
- data/spec/validates_timeliness/attribute_methods_spec.rb +22 -7
- data/spec/validates_timeliness/conversion_spec.rb +41 -41
- data/spec/validates_timeliness/extensions/date_time_select_spec.rb +5 -4
- data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +28 -16
- data/spec/validates_timeliness/helper_methods_spec.rb +12 -12
- data/spec/validates_timeliness/orm/active_record_spec.rb +178 -44
- data/spec/validates_timeliness/validator/after_spec.rb +3 -3
- data/spec/validates_timeliness/validator/before_spec.rb +3 -3
- data/spec/validates_timeliness/validator/is_at_spec.rb +4 -4
- data/spec/validates_timeliness/validator/on_or_after_spec.rb +3 -3
- data/spec/validates_timeliness/validator/on_or_before_spec.rb +3 -3
- data/spec/validates_timeliness/validator_spec.rb +66 -25
- data/spec/validates_timeliness_spec.rb +8 -8
- data/validates_timeliness.gemspec +2 -2
- metadata +41 -60
- data/lib/validates_timeliness/orm/mongoid.rb +0 -54
- data/spec/validates_timeliness/orm/mongoid_spec.rb +0 -113
|
@@ -3,108 +3,242 @@ require 'spec_helper'
|
|
|
3
3
|
describe ValidatesTimeliness, 'ActiveRecord' do
|
|
4
4
|
|
|
5
5
|
context "validation methods" do
|
|
6
|
+
let(:record) { Employee.new }
|
|
7
|
+
|
|
6
8
|
it 'should be defined for the class' do
|
|
7
|
-
ActiveRecord::Base.
|
|
8
|
-
ActiveRecord::Base.
|
|
9
|
-
ActiveRecord::Base.
|
|
9
|
+
expect(ActiveRecord::Base).to respond_to(:validates_date)
|
|
10
|
+
expect(ActiveRecord::Base).to respond_to(:validates_time)
|
|
11
|
+
expect(ActiveRecord::Base).to respond_to(:validates_datetime)
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
it 'should defines for the instance' do
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
expect(record).to respond_to(:validates_date)
|
|
16
|
+
expect(record).to respond_to(:validates_time)
|
|
17
|
+
expect(record).to respond_to(:validates_datetime)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should validate a valid value string" do
|
|
21
|
+
record.birth_date = '2012-01-01'
|
|
22
|
+
|
|
23
|
+
record.valid?
|
|
24
|
+
expect(record.errors[:birth_date]).to be_empty
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "should validate a invalid value string" do
|
|
28
|
+
record.birth_date = 'not a date'
|
|
29
|
+
|
|
30
|
+
record.valid?
|
|
31
|
+
expect(record.errors[:birth_date]).not_to be_empty
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "should validate a nil value" do
|
|
35
|
+
record.birth_date = nil
|
|
36
|
+
|
|
37
|
+
record.valid?
|
|
38
|
+
expect(record.errors[:birth_date]).to be_empty
|
|
16
39
|
end
|
|
17
40
|
end
|
|
18
41
|
|
|
19
42
|
it 'should determine type for attribute' do
|
|
20
|
-
Employee.timeliness_attribute_type(:birth_date).
|
|
43
|
+
expect(Employee.timeliness_attribute_type(:birth_date)).to eq :date
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context 'attribute timezone awareness' do
|
|
47
|
+
let(:klass) {
|
|
48
|
+
Class.new(ActiveRecord::Base) do
|
|
49
|
+
self.table_name = 'employees'
|
|
50
|
+
attr_accessor :some_date
|
|
51
|
+
attr_accessor :some_time
|
|
52
|
+
attr_accessor :some_datetime
|
|
53
|
+
validates_date :some_date
|
|
54
|
+
validates_time :some_time
|
|
55
|
+
validates_datetime :some_datetime
|
|
56
|
+
end
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
context 'for column attribute' do
|
|
60
|
+
it 'should be detected from column type' do
|
|
61
|
+
expect(klass.timeliness_attribute_timezone_aware?(:birth_date)).to be_falsey
|
|
62
|
+
expect(klass.timeliness_attribute_timezone_aware?(:birth_time)).to be_falsey
|
|
63
|
+
expect(klass.timeliness_attribute_timezone_aware?(:birth_datetime)).to be_truthy
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context 'for non-column attribute' do
|
|
68
|
+
it 'should be detected from the validation type' do
|
|
69
|
+
expect(klass.timeliness_attribute_timezone_aware?(:some_date)).to be_falsey
|
|
70
|
+
expect(klass.timeliness_attribute_timezone_aware?(:some_time)).to be_falsey
|
|
71
|
+
expect(klass.timeliness_attribute_timezone_aware?(:some_datetime)).to be_truthy
|
|
72
|
+
end
|
|
73
|
+
end
|
|
21
74
|
end
|
|
22
75
|
|
|
23
76
|
context "attribute write method" do
|
|
24
77
|
class EmployeeWithCache < ActiveRecord::Base
|
|
25
|
-
|
|
26
|
-
|
|
78
|
+
self.table_name = 'employees'
|
|
79
|
+
validates_date :birth_date, :allow_blank => true
|
|
80
|
+
validates_time :birth_time, :allow_blank => true
|
|
81
|
+
validates_datetime :birth_datetime, :allow_blank => true
|
|
27
82
|
end
|
|
28
83
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
84
|
+
context 'value cache' do
|
|
85
|
+
let(:record) { EmployeeWithCache.new }
|
|
86
|
+
|
|
87
|
+
context 'for datetime column' do
|
|
88
|
+
it 'should store raw value' do
|
|
89
|
+
record.birth_datetime = datetime_string = '2010-01-01 12:30'
|
|
90
|
+
|
|
91
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_datetime')).to eq datetime_string
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
context 'for date column' do
|
|
96
|
+
it 'should store raw value' do
|
|
97
|
+
record.birth_date = date_string = '2010-01-01'
|
|
98
|
+
|
|
99
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to eq date_string
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
context 'for time column' do
|
|
104
|
+
it 'should store raw value' do
|
|
105
|
+
record.birth_time = time_string = '12:12'
|
|
106
|
+
|
|
107
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_time')).to eq time_string
|
|
108
|
+
end
|
|
109
|
+
end
|
|
33
110
|
end
|
|
34
111
|
|
|
35
112
|
context "with plugin parser" do
|
|
36
113
|
with_config(:use_plugin_parser, true)
|
|
114
|
+
let(:record) { EmployeeWithParser.new }
|
|
37
115
|
|
|
38
116
|
class EmployeeWithParser < ActiveRecord::Base
|
|
39
|
-
|
|
40
|
-
validates_date :birth_date
|
|
41
|
-
|
|
117
|
+
self.table_name = 'employees'
|
|
118
|
+
validates_date :birth_date, :allow_blank => true
|
|
119
|
+
validates_time :birth_time, :allow_blank => true
|
|
120
|
+
validates_datetime :birth_datetime, :allow_blank => true
|
|
42
121
|
end
|
|
43
122
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
123
|
+
context "for a date column" do
|
|
124
|
+
it 'should parse a string value' do
|
|
125
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
126
|
+
|
|
127
|
+
record.birth_date = '2010-01-01'
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it 'should parse a invalid string value as nil' do
|
|
131
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
132
|
+
|
|
133
|
+
record.birth_date = 'not valid'
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
it 'should store a Date value after parsing string' do
|
|
137
|
+
record.birth_date = '2010-01-01'
|
|
138
|
+
|
|
139
|
+
expect(record.birth_date).to be_kind_of(Date)
|
|
140
|
+
expect(record.birth_date).to eq Date.new(2010, 1, 1)
|
|
141
|
+
end
|
|
48
142
|
end
|
|
49
143
|
|
|
50
|
-
context "for a
|
|
51
|
-
it 'should
|
|
52
|
-
|
|
53
|
-
r.birth_date = '2010-01-01'
|
|
144
|
+
context "for a time column" do
|
|
145
|
+
it 'should parse a string value' do
|
|
146
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
54
147
|
|
|
55
|
-
|
|
56
|
-
|
|
148
|
+
record.birth_time = '12:30'
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it 'should parse a invalid string value as nil' do
|
|
152
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
153
|
+
|
|
154
|
+
record.birth_time = 'not valid'
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it 'should store a Time value after parsing string' do
|
|
158
|
+
record.birth_time = '12:30'
|
|
159
|
+
|
|
160
|
+
expect(record.birth_time).to be_kind_of(Time)
|
|
161
|
+
expect(record.birth_time).to eq Time.utc(2000, 1, 1, 12, 30)
|
|
57
162
|
end
|
|
58
163
|
end
|
|
59
164
|
|
|
60
165
|
context "for a datetime column" do
|
|
61
166
|
with_config(:default_timezone, 'Australia/Melbourne')
|
|
62
167
|
|
|
168
|
+
it 'should parse a string value' do
|
|
169
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
170
|
+
|
|
171
|
+
record.birth_datetime = '2010-01-01 12:00'
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
it 'should parse a invalid string value as nil' do
|
|
175
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
176
|
+
|
|
177
|
+
record.birth_datetime = 'not valid'
|
|
178
|
+
end
|
|
179
|
+
|
|
63
180
|
it 'should parse string into Time value' do
|
|
64
|
-
|
|
65
|
-
r.birth_datetime = '2010-01-01 12:00'
|
|
181
|
+
record.birth_datetime = '2010-01-01 12:00'
|
|
66
182
|
|
|
67
|
-
|
|
183
|
+
expect(record.birth_datetime).to be_kind_of(Time)
|
|
68
184
|
end
|
|
69
185
|
|
|
70
186
|
it 'should parse string as current timezone' do
|
|
71
|
-
|
|
72
|
-
r.birth_datetime = '2010-06-01 12:00'
|
|
187
|
+
record.birth_datetime = '2010-06-01 12:00'
|
|
73
188
|
|
|
74
|
-
|
|
189
|
+
expect(record.birth_datetime.utc_offset).to eq Time.zone.utc_offset
|
|
75
190
|
end
|
|
76
191
|
end
|
|
77
192
|
end
|
|
78
193
|
end
|
|
79
194
|
|
|
80
|
-
context "
|
|
81
|
-
it 'should
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
195
|
+
context "reload" do
|
|
196
|
+
it 'should clear cache value' do
|
|
197
|
+
record = Employee.create!
|
|
198
|
+
record.birth_date = '2010-01-01'
|
|
199
|
+
|
|
200
|
+
record.reload
|
|
85
201
|
|
|
86
|
-
|
|
202
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to be_nil
|
|
87
203
|
end
|
|
88
204
|
end
|
|
89
205
|
|
|
90
206
|
context "before_type_cast method" do
|
|
207
|
+
let(:record) { Employee.new }
|
|
208
|
+
|
|
91
209
|
it 'should be defined on class if ORM supports it' do
|
|
92
|
-
|
|
210
|
+
expect(record).to respond_to(:birth_datetime_before_type_cast)
|
|
93
211
|
end
|
|
94
212
|
|
|
95
213
|
it 'should return original value' do
|
|
96
|
-
|
|
97
|
-
r.birth_datetime = date_string = '2010-01-01'
|
|
214
|
+
record.birth_datetime = date_string = '2010-01-01'
|
|
98
215
|
|
|
99
|
-
|
|
216
|
+
expect(record.birth_datetime_before_type_cast).to eq date_string
|
|
100
217
|
end
|
|
101
218
|
|
|
102
219
|
it 'should return attribute if no attribute assignment has been made' do
|
|
103
220
|
datetime = Time.zone.local(2010,01,01)
|
|
104
221
|
Employee.create(:birth_datetime => datetime)
|
|
105
222
|
|
|
106
|
-
|
|
107
|
-
|
|
223
|
+
record = Employee.last
|
|
224
|
+
expect(record.birth_datetime_before_type_cast).to match(/#{datetime.utc.to_s[0...-4]}/)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
context "with plugin parser" do
|
|
228
|
+
with_config(:use_plugin_parser, true)
|
|
229
|
+
|
|
230
|
+
it 'should return original value' do
|
|
231
|
+
record.birth_datetime = date_string = '2010-01-31'
|
|
232
|
+
|
|
233
|
+
expect(record.birth_datetime_before_type_cast).to eq date_string
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
context "define_attribute_methods" do
|
|
240
|
+
it "returns a falsy value if the attribute methods have already been generated" do
|
|
241
|
+
expect(Employee.define_attribute_methods).to be_falsey
|
|
108
242
|
end
|
|
109
243
|
end
|
|
110
244
|
end
|
|
@@ -25,15 +25,15 @@ describe ValidatesTimeliness::Validator, ":after option" do
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it "should not be valid for same time as restriction" do
|
|
28
|
-
invalid!(:birth_time, Time.
|
|
28
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0), 'must be after 12:00:00')
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should not be valid for time before restriction" do
|
|
32
|
-
invalid!(:birth_time, Time.
|
|
32
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59), 'must be after 12:00:00')
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should be valid for time after restriction" do
|
|
36
|
-
valid!(:birth_time, Time.
|
|
36
|
+
valid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01))
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
@@ -25,15 +25,15 @@ describe ValidatesTimeliness::Validator, ":before option" do
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it "should not be valid for time after restriction" do
|
|
28
|
-
invalid!(:birth_time, Time.
|
|
28
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01), 'must be before 12:00:00')
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should not be valid for same time as restriction" do
|
|
32
|
-
invalid!(:birth_time, Time.
|
|
32
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0), 'must be before 12:00:00')
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should be valid for time before restriction" do
|
|
36
|
-
valid!(:birth_time, Time.
|
|
36
|
+
valid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59))
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe ValidatesTimeliness::Validator, ":is_at option" do
|
|
4
4
|
before do
|
|
5
|
-
Timecop.freeze(Time.
|
|
5
|
+
Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0))
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
describe "for date type" do
|
|
@@ -29,15 +29,15 @@ describe ValidatesTimeliness::Validator, ":is_at option" do
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should not be valid for time before restriction" do
|
|
32
|
-
invalid!(:birth_time, Time.
|
|
32
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59), 'must be at 12:00:00')
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should not be valid for time after restriction" do
|
|
36
|
-
invalid!(:birth_time, Time.
|
|
36
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01), 'must be at 12:00:00')
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
it "should be valid for same time as restriction" do
|
|
40
|
-
valid!(:birth_time, Time.
|
|
40
|
+
valid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0))
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
|
|
@@ -25,15 +25,15 @@ describe ValidatesTimeliness::Validator, ":on_or_after option" do
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it "should not be valid for time before restriction" do
|
|
28
|
-
invalid!(:birth_time, Time.
|
|
28
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59), 'must be on or after 12:00:00')
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should be valid for time after restriction" do
|
|
32
|
-
valid!(:birth_time, Time.
|
|
32
|
+
valid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01))
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should be valid for same time as restriction" do
|
|
36
|
-
valid!(:birth_time, Time.
|
|
36
|
+
valid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0))
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
@@ -25,15 +25,15 @@ describe ValidatesTimeliness::Validator, ":on_or_before option" do
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it "should not be valid for time after restriction" do
|
|
28
|
-
invalid!(:birth_time, Time.
|
|
28
|
+
invalid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01), 'must be on or before 12:00:00')
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should be valid for time before restriction" do
|
|
32
|
-
valid!(:birth_time, Time.
|
|
32
|
+
valid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59))
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it "should be valid for same time as restriction" do
|
|
36
|
-
valid!(:birth_time, Time.
|
|
36
|
+
valid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0))
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
@@ -1,23 +1,29 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe ValidatesTimeliness::Validator do
|
|
4
|
-
NIL = [nil]
|
|
5
|
-
|
|
6
4
|
before do
|
|
7
|
-
Timecop.freeze(Time.
|
|
5
|
+
Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0))
|
|
8
6
|
end
|
|
9
7
|
|
|
10
8
|
describe "Model.validates with :timeliness option" do
|
|
11
9
|
it 'should use plugin validator class' do
|
|
12
10
|
Person.validates :birth_date, :timeliness => {:is_at => Date.new(2010,1,1), :type => :date}
|
|
13
|
-
Person.validators.
|
|
11
|
+
expect(Person.validators.select { |v| v.is_a?(ActiveModel::Validations::TimelinessValidator) }.size).to eq(1)
|
|
14
12
|
invalid!(:birth_date, Date.new(2010,1,2))
|
|
15
13
|
valid!(:birth_date, Date.new(2010,1,1))
|
|
16
14
|
end
|
|
17
15
|
|
|
18
16
|
it 'should use default to :datetime type' do
|
|
19
17
|
Person.validates :birth_datetime, :timeliness => {:is_at => Time.mktime(2010,1,1)}
|
|
20
|
-
Person.validators.first.type.
|
|
18
|
+
expect(Person.validators.first.type).to eq(:datetime)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'should add attribute to timeliness attributes set' do
|
|
22
|
+
expect(PersonWithShim.timeliness_validated_attributes).not_to include(:birth_time)
|
|
23
|
+
|
|
24
|
+
PersonWithShim.validates :birth_time, :timeliness => {:is_at => "12:30"}
|
|
25
|
+
|
|
26
|
+
expect(PersonWithShim.timeliness_validated_attributes).to include(:birth_time)
|
|
21
27
|
end
|
|
22
28
|
end
|
|
23
29
|
|
|
@@ -29,22 +35,33 @@ describe ValidatesTimeliness::Validator do
|
|
|
29
35
|
it 'should not be valid attribute is type cast to nil but raw value is non-nil invalid value' do
|
|
30
36
|
Person.validates_date :birth_date, :allow_nil => true
|
|
31
37
|
record = Person.new
|
|
32
|
-
record.
|
|
33
|
-
record.
|
|
34
|
-
record.
|
|
35
|
-
record.errors[:birth_date].first.
|
|
38
|
+
allow(record).to receive(:birth_date).and_return(nil)
|
|
39
|
+
allow(record).to receive(:read_timeliness_attribute_before_type_cast).and_return("Not a date")
|
|
40
|
+
expect(record).not_to be_valid
|
|
41
|
+
expect(record.errors[:birth_date].first).to eq('is not a valid date')
|
|
36
42
|
end
|
|
37
43
|
|
|
38
44
|
describe ":allow_nil option" do
|
|
39
45
|
it 'should not allow nil by default' do
|
|
40
46
|
Person.validates_date :birth_date
|
|
41
|
-
invalid!(:birth_date,
|
|
47
|
+
invalid!(:birth_date, [nil], 'is not a valid date')
|
|
42
48
|
valid!(:birth_date, Date.today)
|
|
43
49
|
end
|
|
44
50
|
|
|
45
51
|
it 'should allow nil when true' do
|
|
46
52
|
Person.validates_date :birth_date, :allow_nil => true
|
|
47
|
-
valid!(:birth_date,
|
|
53
|
+
valid!(:birth_date, [nil])
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
context "with raw value cache" do
|
|
57
|
+
it "should not be valid with an invalid format" do
|
|
58
|
+
PersonWithShim.validates_date :birth_date, :allow_nil => true
|
|
59
|
+
|
|
60
|
+
p = PersonWithShim.new
|
|
61
|
+
p.birth_date = 'bogus'
|
|
62
|
+
|
|
63
|
+
expect(p).not_to be_valid
|
|
64
|
+
end
|
|
48
65
|
end
|
|
49
66
|
end
|
|
50
67
|
|
|
@@ -59,6 +76,17 @@ describe ValidatesTimeliness::Validator do
|
|
|
59
76
|
Person.validates_date :birth_date, :allow_blank => true
|
|
60
77
|
valid!(:birth_date, '')
|
|
61
78
|
end
|
|
79
|
+
|
|
80
|
+
context "with raw value cache" do
|
|
81
|
+
it "should not be valid with an invalid format" do
|
|
82
|
+
PersonWithShim.validates_date :birth_date, :allow_blank => true
|
|
83
|
+
|
|
84
|
+
p = PersonWithShim.new
|
|
85
|
+
p.birth_date = 'bogus'
|
|
86
|
+
|
|
87
|
+
expect(p).not_to be_valid
|
|
88
|
+
end
|
|
89
|
+
end
|
|
62
90
|
end
|
|
63
91
|
|
|
64
92
|
describe ":between option" do
|
|
@@ -66,8 +94,8 @@ describe ValidatesTimeliness::Validator do
|
|
|
66
94
|
it 'should be split option into :on_or_after and :on_or_before values' do
|
|
67
95
|
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
|
|
68
96
|
Person.validates_date :birth_date, :between => [on_or_after, on_or_before]
|
|
69
|
-
Person.validators.first.options[:on_or_after].
|
|
70
|
-
Person.validators.first.options[:on_or_before].
|
|
97
|
+
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
|
|
98
|
+
expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
|
|
71
99
|
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
|
72
100
|
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
|
|
73
101
|
valid!(:birth_date, on_or_after)
|
|
@@ -79,14 +107,27 @@ describe ValidatesTimeliness::Validator do
|
|
|
79
107
|
it 'should be split option into :on_or_after and :on_or_before values' do
|
|
80
108
|
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
|
|
81
109
|
Person.validates_date :birth_date, :between => on_or_after..on_or_before
|
|
82
|
-
Person.validators.first.options[:on_or_after].
|
|
83
|
-
Person.validators.first.options[:on_or_before].
|
|
110
|
+
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
|
|
111
|
+
expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
|
|
84
112
|
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
|
85
113
|
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
|
|
86
114
|
valid!(:birth_date, on_or_after)
|
|
87
115
|
valid!(:birth_date, on_or_before)
|
|
88
116
|
end
|
|
89
117
|
end
|
|
118
|
+
|
|
119
|
+
describe "range with excluded end value" do
|
|
120
|
+
it 'should be split option into :on_or_after and :before values' do
|
|
121
|
+
on_or_after, before = Date.new(2010,1,1), Date.new(2010,1,3)
|
|
122
|
+
Person.validates_date :birth_date, :between => on_or_after...before
|
|
123
|
+
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
|
|
124
|
+
expect(Person.validators.first.options[:before]).to eq(before)
|
|
125
|
+
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
|
126
|
+
invalid!(:birth_date, before, "must be before 2010-01-03")
|
|
127
|
+
valid!(:birth_date, on_or_after)
|
|
128
|
+
valid!(:birth_date, before - 1)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
90
131
|
end
|
|
91
132
|
|
|
92
133
|
describe ":ignore_usec option" do
|
|
@@ -118,13 +159,13 @@ describe ValidatesTimeliness::Validator do
|
|
|
118
159
|
it "should be valid when value matches format" do
|
|
119
160
|
person.birth_date = '11-12-1913'
|
|
120
161
|
person.valid?
|
|
121
|
-
person.errors[:birth_date].
|
|
162
|
+
expect(person.errors[:birth_date]).to be_empty
|
|
122
163
|
end
|
|
123
164
|
|
|
124
165
|
it "should not be valid when value does not match format" do
|
|
125
166
|
person.birth_date = '1913-12-11'
|
|
126
167
|
person.valid?
|
|
127
|
-
person.errors[:birth_date].
|
|
168
|
+
expect(person.errors[:birth_date]).to include('is not a valid date')
|
|
128
169
|
end
|
|
129
170
|
end
|
|
130
171
|
|
|
@@ -138,21 +179,21 @@ describe ValidatesTimeliness::Validator do
|
|
|
138
179
|
it "should be added when ignore_restriction_errors is false" do
|
|
139
180
|
with_config(:ignore_restriction_errors, false) do
|
|
140
181
|
person.valid?
|
|
141
|
-
person.errors[:birth_date].first.
|
|
182
|
+
expect(person.errors[:birth_date].first).to match("Error occurred validating birth_date")
|
|
142
183
|
end
|
|
143
184
|
end
|
|
144
185
|
|
|
145
186
|
it "should not be added when ignore_restriction_errors is true" do
|
|
146
187
|
with_config(:ignore_restriction_errors, true) do
|
|
147
188
|
person.valid?
|
|
148
|
-
person.errors[:birth_date].
|
|
189
|
+
expect(person.errors[:birth_date]).to be_empty
|
|
149
190
|
end
|
|
150
191
|
end
|
|
151
192
|
|
|
152
193
|
it 'should exit on first error' do
|
|
153
194
|
with_config(:ignore_restriction_errors, false) do
|
|
154
195
|
person.valid?
|
|
155
|
-
person.errors[:birth_date].
|
|
196
|
+
expect(person.errors[:birth_date].size).to eq(1)
|
|
156
197
|
end
|
|
157
198
|
end
|
|
158
199
|
end
|
|
@@ -161,17 +202,17 @@ describe ValidatesTimeliness::Validator do
|
|
|
161
202
|
describe "default" do
|
|
162
203
|
it 'should format date error value as yyyy-mm-dd' do
|
|
163
204
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
|
|
164
|
-
validator.format_error_value(Date.new(2010,1,1)).
|
|
205
|
+
expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
|
|
165
206
|
end
|
|
166
207
|
|
|
167
208
|
it 'should format time error value as hh:nn:ss' do
|
|
168
209
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_time], :type => :time)
|
|
169
|
-
validator.format_error_value(Time.mktime(2010,1,1,12,34,56)).
|
|
210
|
+
expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('12:34:56')
|
|
170
211
|
end
|
|
171
212
|
|
|
172
213
|
it 'should format datetime error value as yyyy-mm-dd hh:nn:ss' do
|
|
173
214
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_datetime], :type => :datetime)
|
|
174
|
-
validator.format_error_value(Time.mktime(2010,1,1,12,34,56)).
|
|
215
|
+
expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('2010-01-01 12:34:56')
|
|
175
216
|
end
|
|
176
217
|
end
|
|
177
218
|
|
|
@@ -182,7 +223,7 @@ describe ValidatesTimeliness::Validator do
|
|
|
182
223
|
|
|
183
224
|
it 'should use the default format for the type' do
|
|
184
225
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
|
|
185
|
-
validator.format_error_value(Date.new(2010,1,1)).
|
|
226
|
+
expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
|
|
186
227
|
end
|
|
187
228
|
|
|
188
229
|
after :all do
|
|
@@ -196,7 +237,7 @@ describe ValidatesTimeliness::Validator do
|
|
|
196
237
|
Person.validates_date :birth_date, :invalid_date_message => 'custom invalid message'
|
|
197
238
|
invalid!(:birth_date, 'asdf', 'custom invalid message')
|
|
198
239
|
end
|
|
199
|
-
|
|
240
|
+
|
|
200
241
|
it 'should be used for invalid restriction' do
|
|
201
242
|
Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message'
|
|
202
243
|
invalid!(:birth_date, Time.now, 'custom before message')
|
|
@@ -3,39 +3,39 @@ require 'spec_helper'
|
|
|
3
3
|
describe ValidatesTimeliness do
|
|
4
4
|
|
|
5
5
|
it 'should alias use_euro_formats to remove_us_formats on Timeliness gem' do
|
|
6
|
-
Timeliness.
|
|
6
|
+
expect(Timeliness).to respond_to(:remove_us_formats)
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
it 'should alias to date_for_time_type to dummy_date_for_time_type on Timeliness gem' do
|
|
10
|
-
Timeliness.
|
|
10
|
+
expect(Timeliness).to respond_to(:dummy_date_for_time_type)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
describe "config" do
|
|
14
14
|
it 'should delegate default_timezone to Timeliness gem' do
|
|
15
|
-
Timeliness.
|
|
15
|
+
expect(Timeliness).to receive(:default_timezone=)
|
|
16
16
|
ValidatesTimeliness.default_timezone = :utc
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
it 'should delegate dummy_date_for_time_type to Timeliness gem' do
|
|
20
|
-
Timeliness.
|
|
21
|
-
Timeliness.
|
|
20
|
+
expect(Timeliness).to receive(:dummy_date_for_time_type)
|
|
21
|
+
expect(Timeliness).to receive(:dummy_date_for_time_type=)
|
|
22
22
|
array = ValidatesTimeliness.dummy_date_for_time_type
|
|
23
23
|
ValidatesTimeliness.dummy_date_for_time_type = array
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
context "parser" do
|
|
27
27
|
it 'should delegate add_formats to Timeliness gem' do
|
|
28
|
-
Timeliness.
|
|
28
|
+
expect(Timeliness).to receive(:add_formats)
|
|
29
29
|
ValidatesTimeliness.parser.add_formats
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
it 'should delegate remove_formats to Timeliness gem' do
|
|
33
|
-
Timeliness.
|
|
33
|
+
expect(Timeliness).to receive(:remove_formats)
|
|
34
34
|
ValidatesTimeliness.parser.remove_formats
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
it 'should delegate remove_us_formats to Timeliness gem' do
|
|
38
|
-
Timeliness.
|
|
38
|
+
expect(Timeliness).to receive(:remove_us_formats)
|
|
39
39
|
ValidatesTimeliness.parser.remove_us_formats
|
|
40
40
|
end
|
|
41
41
|
end
|
|
@@ -12,9 +12,9 @@ Gem::Specification.new do |s|
|
|
|
12
12
|
s.homepage = %q{http://github.com/adzap/validates_timeliness}
|
|
13
13
|
|
|
14
14
|
s.require_paths = ["lib"]
|
|
15
|
-
s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb }
|
|
15
|
+
s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb Appraisals Travis.yml } - Dir['gemsfiles/*']
|
|
16
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
17
17
|
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
|
|
18
18
|
|
|
19
|
-
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.
|
|
19
|
+
s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.7"])
|
|
20
20
|
end
|