validates_timeliness 3.0.14 → 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/README.rdoc +12 -9
- data/Rakefile +1 -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/templates/validates_timeliness.rb +3 -3
- data/lib/validates_timeliness/attribute_methods.rb +36 -36
- data/lib/validates_timeliness/extensions/date_time_select.rb +2 -9
- data/lib/validates_timeliness/extensions/multiparameter_handler.rb +59 -65
- data/lib/validates_timeliness/extensions.rb +3 -4
- data/lib/validates_timeliness/helper_methods.rb +8 -2
- data/lib/validates_timeliness/orm/active_record.rb +59 -13
- data/lib/validates_timeliness/validator.rb +20 -6
- data/lib/validates_timeliness/version.rb +1 -1
- data/lib/validates_timeliness.rb +9 -13
- data/spec/spec_helper.rb +4 -2
- 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 +10 -10
- 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 +8 -7
- data/spec/validates_timeliness/helper_methods_spec.rb +12 -12
- data/spec/validates_timeliness/orm/active_record_spec.rb +70 -80
- 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 +40 -29
- data/spec/validates_timeliness_spec.rb +8 -8
- data/validates_timeliness.gemspec +1 -1
- metadata +41 -67
- data/gemfiles/mongoid_2_1.gemfile +0 -16
- data/gemfiles/mongoid_2_2.gemfile +0 -16
- data/gemfiles/mongoid_2_3.gemfile +0 -16
- data/gemfiles/mongoid_2_4.gemfile +0 -16
- data/gemfiles/rails_3_0.gemfile +0 -15
- data/gemfiles/rails_3_1.gemfile +0 -15
- data/gemfiles/rails_3_2.gemfile +0 -15
- data/lib/validates_timeliness/orm/mongoid.rb +0 -63
- data/spec/validates_timeliness/orm/mongoid_spec.rb +0 -189
|
@@ -3,45 +3,44 @@ 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)
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
it "should validate a valid value string" do
|
|
19
|
-
|
|
20
|
-
r.birth_date = '2012-01-01'
|
|
21
|
+
record.birth_date = '2012-01-01'
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
record.valid?
|
|
24
|
+
expect(record.errors[:birth_date]).to be_empty
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
it "should validate a invalid value string" do
|
|
27
|
-
|
|
28
|
-
r.birth_date = 'not a date'
|
|
28
|
+
record.birth_date = 'not a date'
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
record.valid?
|
|
31
|
+
expect(record.errors[:birth_date]).not_to be_empty
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it "should validate a nil value" do
|
|
35
|
-
|
|
36
|
-
r.birth_date = nil
|
|
35
|
+
record.birth_date = nil
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
record.valid?
|
|
38
|
+
expect(record.errors[:birth_date]).to be_empty
|
|
40
39
|
end
|
|
41
40
|
end
|
|
42
41
|
|
|
43
42
|
it 'should determine type for attribute' do
|
|
44
|
-
Employee.timeliness_attribute_type(:birth_date).
|
|
43
|
+
expect(Employee.timeliness_attribute_type(:birth_date)).to eq :date
|
|
45
44
|
end
|
|
46
45
|
|
|
47
46
|
context 'attribute timezone awareness' do
|
|
@@ -59,17 +58,17 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
|
59
58
|
|
|
60
59
|
context 'for column attribute' do
|
|
61
60
|
it 'should be detected from column type' do
|
|
62
|
-
klass.timeliness_attribute_timezone_aware?(:birth_date).
|
|
63
|
-
klass.timeliness_attribute_timezone_aware?(:birth_time).
|
|
64
|
-
klass.timeliness_attribute_timezone_aware?(:birth_datetime).
|
|
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
|
|
65
64
|
end
|
|
66
65
|
end
|
|
67
66
|
|
|
68
67
|
context 'for non-column attribute' do
|
|
69
68
|
it 'should be detected from the validation type' do
|
|
70
|
-
klass.timeliness_attribute_timezone_aware?(:some_date).
|
|
71
|
-
klass.timeliness_attribute_timezone_aware?(:some_time).
|
|
72
|
-
klass.timeliness_attribute_timezone_aware?(:some_datetime).
|
|
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
|
|
73
72
|
end
|
|
74
73
|
end
|
|
75
74
|
end
|
|
@@ -83,36 +82,36 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
|
83
82
|
end
|
|
84
83
|
|
|
85
84
|
context 'value cache' do
|
|
85
|
+
let(:record) { EmployeeWithCache.new }
|
|
86
|
+
|
|
86
87
|
context 'for datetime column' do
|
|
87
88
|
it 'should store raw value' do
|
|
88
|
-
|
|
89
|
-
r.birth_datetime = datetime_string = '2010-01-01 12:30'
|
|
89
|
+
record.birth_datetime = datetime_string = '2010-01-01 12:30'
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_datetime')).to eq datetime_string
|
|
92
92
|
end
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
context 'for date column' do
|
|
96
96
|
it 'should store raw value' do
|
|
97
|
-
|
|
98
|
-
r.birth_date = date_string = '2010-01-01'
|
|
97
|
+
record.birth_date = date_string = '2010-01-01'
|
|
99
98
|
|
|
100
|
-
|
|
99
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to eq date_string
|
|
101
100
|
end
|
|
102
101
|
end
|
|
103
102
|
|
|
104
103
|
context 'for time column' do
|
|
105
104
|
it 'should store raw value' do
|
|
106
|
-
|
|
107
|
-
r.birth_time = time_string = '12:12'
|
|
105
|
+
record.birth_time = time_string = '12:12'
|
|
108
106
|
|
|
109
|
-
|
|
107
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_time')).to eq time_string
|
|
110
108
|
end
|
|
111
109
|
end
|
|
112
110
|
end
|
|
113
111
|
|
|
114
112
|
context "with plugin parser" do
|
|
115
113
|
with_config(:use_plugin_parser, true)
|
|
114
|
+
let(:record) { EmployeeWithParser.new }
|
|
116
115
|
|
|
117
116
|
class EmployeeWithParser < ActiveRecord::Base
|
|
118
117
|
self.table_name = 'employees'
|
|
@@ -123,48 +122,43 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
|
123
122
|
|
|
124
123
|
context "for a date column" do
|
|
125
124
|
it 'should parse a string value' do
|
|
126
|
-
Timeliness::Parser.
|
|
125
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
127
126
|
|
|
128
|
-
|
|
129
|
-
r.birth_date = '2010-01-01'
|
|
127
|
+
record.birth_date = '2010-01-01'
|
|
130
128
|
end
|
|
131
129
|
|
|
132
130
|
it 'should parse a invalid string value as nil' do
|
|
133
|
-
Timeliness::Parser.
|
|
134
|
-
|
|
135
|
-
|
|
131
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
132
|
+
|
|
133
|
+
record.birth_date = 'not valid'
|
|
136
134
|
end
|
|
137
135
|
|
|
138
136
|
it 'should store a Date value after parsing string' do
|
|
139
|
-
|
|
140
|
-
r.birth_date = '2010-01-01'
|
|
137
|
+
record.birth_date = '2010-01-01'
|
|
141
138
|
|
|
142
|
-
|
|
143
|
-
|
|
139
|
+
expect(record.birth_date).to be_kind_of(Date)
|
|
140
|
+
expect(record.birth_date).to eq Date.new(2010, 1, 1)
|
|
144
141
|
end
|
|
145
142
|
end
|
|
146
143
|
|
|
147
144
|
context "for a time column" do
|
|
148
145
|
it 'should parse a string value' do
|
|
149
|
-
Timeliness::Parser.
|
|
146
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
150
147
|
|
|
151
|
-
|
|
152
|
-
r.birth_time = '12:30'
|
|
148
|
+
record.birth_time = '12:30'
|
|
153
149
|
end
|
|
154
150
|
|
|
155
151
|
it 'should parse a invalid string value as nil' do
|
|
156
|
-
Timeliness::Parser.
|
|
152
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
157
153
|
|
|
158
|
-
|
|
159
|
-
r.birth_time = 'not valid'
|
|
154
|
+
record.birth_time = 'not valid'
|
|
160
155
|
end
|
|
161
156
|
|
|
162
157
|
it 'should store a Time value after parsing string' do
|
|
163
|
-
|
|
164
|
-
r.birth_time = '12:30'
|
|
158
|
+
record.birth_time = '12:30'
|
|
165
159
|
|
|
166
|
-
|
|
167
|
-
|
|
160
|
+
expect(record.birth_time).to be_kind_of(Time)
|
|
161
|
+
expect(record.birth_time).to eq Time.utc(2000, 1, 1, 12, 30)
|
|
168
162
|
end
|
|
169
163
|
end
|
|
170
164
|
|
|
@@ -172,31 +166,27 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
|
172
166
|
with_config(:default_timezone, 'Australia/Melbourne')
|
|
173
167
|
|
|
174
168
|
it 'should parse a string value' do
|
|
175
|
-
Timeliness::Parser.
|
|
169
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
176
170
|
|
|
177
|
-
|
|
178
|
-
r.birth_datetime = '2010-01-01 12:00'
|
|
171
|
+
record.birth_datetime = '2010-01-01 12:00'
|
|
179
172
|
end
|
|
180
173
|
|
|
181
174
|
it 'should parse a invalid string value as nil' do
|
|
182
|
-
Timeliness::Parser.
|
|
175
|
+
expect(Timeliness::Parser).to receive(:parse)
|
|
183
176
|
|
|
184
|
-
|
|
185
|
-
r.birth_datetime = 'not valid'
|
|
177
|
+
record.birth_datetime = 'not valid'
|
|
186
178
|
end
|
|
187
179
|
|
|
188
180
|
it 'should parse string into Time value' do
|
|
189
|
-
|
|
190
|
-
r.birth_datetime = '2010-01-01 12:00'
|
|
181
|
+
record.birth_datetime = '2010-01-01 12:00'
|
|
191
182
|
|
|
192
|
-
|
|
183
|
+
expect(record.birth_datetime).to be_kind_of(Time)
|
|
193
184
|
end
|
|
194
185
|
|
|
195
186
|
it 'should parse string as current timezone' do
|
|
196
|
-
|
|
197
|
-
r.birth_datetime = '2010-06-01 12:00'
|
|
187
|
+
record.birth_datetime = '2010-06-01 12:00'
|
|
198
188
|
|
|
199
|
-
|
|
189
|
+
expect(record.birth_datetime.utc_offset).to eq Time.zone.utc_offset
|
|
200
190
|
end
|
|
201
191
|
end
|
|
202
192
|
end
|
|
@@ -204,43 +194,43 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
|
204
194
|
|
|
205
195
|
context "reload" do
|
|
206
196
|
it 'should clear cache value' do
|
|
207
|
-
|
|
208
|
-
|
|
197
|
+
record = Employee.create!
|
|
198
|
+
record.birth_date = '2010-01-01'
|
|
209
199
|
|
|
210
|
-
|
|
200
|
+
record.reload
|
|
211
201
|
|
|
212
|
-
|
|
202
|
+
expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to be_nil
|
|
213
203
|
end
|
|
214
204
|
end
|
|
215
205
|
|
|
216
206
|
context "before_type_cast method" do
|
|
207
|
+
let(:record) { Employee.new }
|
|
208
|
+
|
|
217
209
|
it 'should be defined on class if ORM supports it' do
|
|
218
|
-
|
|
210
|
+
expect(record).to respond_to(:birth_datetime_before_type_cast)
|
|
219
211
|
end
|
|
220
212
|
|
|
221
213
|
it 'should return original value' do
|
|
222
|
-
|
|
223
|
-
r.birth_datetime = date_string = '2010-01-01'
|
|
214
|
+
record.birth_datetime = date_string = '2010-01-01'
|
|
224
215
|
|
|
225
|
-
|
|
216
|
+
expect(record.birth_datetime_before_type_cast).to eq date_string
|
|
226
217
|
end
|
|
227
218
|
|
|
228
219
|
it 'should return attribute if no attribute assignment has been made' do
|
|
229
220
|
datetime = Time.zone.local(2010,01,01)
|
|
230
221
|
Employee.create(:birth_datetime => datetime)
|
|
231
222
|
|
|
232
|
-
|
|
233
|
-
|
|
223
|
+
record = Employee.last
|
|
224
|
+
expect(record.birth_datetime_before_type_cast).to match(/#{datetime.utc.to_s[0...-4]}/)
|
|
234
225
|
end
|
|
235
226
|
|
|
236
227
|
context "with plugin parser" do
|
|
237
228
|
with_config(:use_plugin_parser, true)
|
|
238
229
|
|
|
239
230
|
it 'should return original value' do
|
|
240
|
-
|
|
241
|
-
r.birth_datetime = date_string = '2010-01-31'
|
|
231
|
+
record.birth_datetime = date_string = '2010-01-31'
|
|
242
232
|
|
|
243
|
-
|
|
233
|
+
expect(record.birth_datetime_before_type_cast).to eq date_string
|
|
244
234
|
end
|
|
245
235
|
end
|
|
246
236
|
|
|
@@ -248,7 +238,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|
|
248
238
|
|
|
249
239
|
context "define_attribute_methods" do
|
|
250
240
|
it "returns a falsy value if the attribute methods have already been generated" do
|
|
251
|
-
Employee.define_attribute_methods.
|
|
241
|
+
expect(Employee.define_attribute_methods).to be_falsey
|
|
252
242
|
end
|
|
253
243
|
end
|
|
254
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,31 +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)
|
|
21
19
|
end
|
|
22
20
|
|
|
23
21
|
it 'should add attribute to timeliness attributes set' do
|
|
24
|
-
PersonWithShim.timeliness_validated_attributes.
|
|
22
|
+
expect(PersonWithShim.timeliness_validated_attributes).not_to include(:birth_time)
|
|
25
23
|
|
|
26
24
|
PersonWithShim.validates :birth_time, :timeliness => {:is_at => "12:30"}
|
|
27
25
|
|
|
28
|
-
PersonWithShim.timeliness_validated_attributes.
|
|
26
|
+
expect(PersonWithShim.timeliness_validated_attributes).to include(:birth_time)
|
|
29
27
|
end
|
|
30
28
|
end
|
|
31
29
|
|
|
@@ -37,22 +35,22 @@ describe ValidatesTimeliness::Validator do
|
|
|
37
35
|
it 'should not be valid attribute is type cast to nil but raw value is non-nil invalid value' do
|
|
38
36
|
Person.validates_date :birth_date, :allow_nil => true
|
|
39
37
|
record = Person.new
|
|
40
|
-
record.
|
|
41
|
-
record.
|
|
42
|
-
record.
|
|
43
|
-
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')
|
|
44
42
|
end
|
|
45
43
|
|
|
46
44
|
describe ":allow_nil option" do
|
|
47
45
|
it 'should not allow nil by default' do
|
|
48
46
|
Person.validates_date :birth_date
|
|
49
|
-
invalid!(:birth_date,
|
|
47
|
+
invalid!(:birth_date, [nil], 'is not a valid date')
|
|
50
48
|
valid!(:birth_date, Date.today)
|
|
51
49
|
end
|
|
52
50
|
|
|
53
51
|
it 'should allow nil when true' do
|
|
54
52
|
Person.validates_date :birth_date, :allow_nil => true
|
|
55
|
-
valid!(:birth_date,
|
|
53
|
+
valid!(:birth_date, [nil])
|
|
56
54
|
end
|
|
57
55
|
|
|
58
56
|
context "with raw value cache" do
|
|
@@ -62,7 +60,7 @@ describe ValidatesTimeliness::Validator do
|
|
|
62
60
|
p = PersonWithShim.new
|
|
63
61
|
p.birth_date = 'bogus'
|
|
64
62
|
|
|
65
|
-
p.
|
|
63
|
+
expect(p).not_to be_valid
|
|
66
64
|
end
|
|
67
65
|
end
|
|
68
66
|
end
|
|
@@ -86,7 +84,7 @@ describe ValidatesTimeliness::Validator do
|
|
|
86
84
|
p = PersonWithShim.new
|
|
87
85
|
p.birth_date = 'bogus'
|
|
88
86
|
|
|
89
|
-
p.
|
|
87
|
+
expect(p).not_to be_valid
|
|
90
88
|
end
|
|
91
89
|
end
|
|
92
90
|
end
|
|
@@ -96,8 +94,8 @@ describe ValidatesTimeliness::Validator do
|
|
|
96
94
|
it 'should be split option into :on_or_after and :on_or_before values' do
|
|
97
95
|
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
|
|
98
96
|
Person.validates_date :birth_date, :between => [on_or_after, on_or_before]
|
|
99
|
-
Person.validators.first.options[:on_or_after].
|
|
100
|
-
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)
|
|
101
99
|
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
|
102
100
|
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
|
|
103
101
|
valid!(:birth_date, on_or_after)
|
|
@@ -109,14 +107,27 @@ describe ValidatesTimeliness::Validator do
|
|
|
109
107
|
it 'should be split option into :on_or_after and :on_or_before values' do
|
|
110
108
|
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
|
|
111
109
|
Person.validates_date :birth_date, :between => on_or_after..on_or_before
|
|
112
|
-
Person.validators.first.options[:on_or_after].
|
|
113
|
-
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)
|
|
114
112
|
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
|
115
113
|
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
|
|
116
114
|
valid!(:birth_date, on_or_after)
|
|
117
115
|
valid!(:birth_date, on_or_before)
|
|
118
116
|
end
|
|
119
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
|
|
120
131
|
end
|
|
121
132
|
|
|
122
133
|
describe ":ignore_usec option" do
|
|
@@ -148,13 +159,13 @@ describe ValidatesTimeliness::Validator do
|
|
|
148
159
|
it "should be valid when value matches format" do
|
|
149
160
|
person.birth_date = '11-12-1913'
|
|
150
161
|
person.valid?
|
|
151
|
-
person.errors[:birth_date].
|
|
162
|
+
expect(person.errors[:birth_date]).to be_empty
|
|
152
163
|
end
|
|
153
164
|
|
|
154
165
|
it "should not be valid when value does not match format" do
|
|
155
166
|
person.birth_date = '1913-12-11'
|
|
156
167
|
person.valid?
|
|
157
|
-
person.errors[:birth_date].
|
|
168
|
+
expect(person.errors[:birth_date]).to include('is not a valid date')
|
|
158
169
|
end
|
|
159
170
|
end
|
|
160
171
|
|
|
@@ -168,21 +179,21 @@ describe ValidatesTimeliness::Validator do
|
|
|
168
179
|
it "should be added when ignore_restriction_errors is false" do
|
|
169
180
|
with_config(:ignore_restriction_errors, false) do
|
|
170
181
|
person.valid?
|
|
171
|
-
person.errors[:birth_date].first.
|
|
182
|
+
expect(person.errors[:birth_date].first).to match("Error occurred validating birth_date")
|
|
172
183
|
end
|
|
173
184
|
end
|
|
174
185
|
|
|
175
186
|
it "should not be added when ignore_restriction_errors is true" do
|
|
176
187
|
with_config(:ignore_restriction_errors, true) do
|
|
177
188
|
person.valid?
|
|
178
|
-
person.errors[:birth_date].
|
|
189
|
+
expect(person.errors[:birth_date]).to be_empty
|
|
179
190
|
end
|
|
180
191
|
end
|
|
181
192
|
|
|
182
193
|
it 'should exit on first error' do
|
|
183
194
|
with_config(:ignore_restriction_errors, false) do
|
|
184
195
|
person.valid?
|
|
185
|
-
person.errors[:birth_date].
|
|
196
|
+
expect(person.errors[:birth_date].size).to eq(1)
|
|
186
197
|
end
|
|
187
198
|
end
|
|
188
199
|
end
|
|
@@ -191,17 +202,17 @@ describe ValidatesTimeliness::Validator do
|
|
|
191
202
|
describe "default" do
|
|
192
203
|
it 'should format date error value as yyyy-mm-dd' do
|
|
193
204
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
|
|
194
|
-
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')
|
|
195
206
|
end
|
|
196
207
|
|
|
197
208
|
it 'should format time error value as hh:nn:ss' do
|
|
198
209
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_time], :type => :time)
|
|
199
|
-
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')
|
|
200
211
|
end
|
|
201
212
|
|
|
202
213
|
it 'should format datetime error value as yyyy-mm-dd hh:nn:ss' do
|
|
203
214
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_datetime], :type => :datetime)
|
|
204
|
-
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')
|
|
205
216
|
end
|
|
206
217
|
end
|
|
207
218
|
|
|
@@ -212,7 +223,7 @@ describe ValidatesTimeliness::Validator do
|
|
|
212
223
|
|
|
213
224
|
it 'should use the default format for the type' do
|
|
214
225
|
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
|
|
215
|
-
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')
|
|
216
227
|
end
|
|
217
228
|
|
|
218
229
|
after :all do
|
|
@@ -226,7 +237,7 @@ describe ValidatesTimeliness::Validator do
|
|
|
226
237
|
Person.validates_date :birth_date, :invalid_date_message => 'custom invalid message'
|
|
227
238
|
invalid!(:birth_date, 'asdf', 'custom invalid message')
|
|
228
239
|
end
|
|
229
|
-
|
|
240
|
+
|
|
230
241
|
it 'should be used for invalid restriction' do
|
|
231
242
|
Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message'
|
|
232
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
|