multiparameter_date_time 0.3.6 → 0.4.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a7fd23d65051eaecbc7c17b18806daa43cd2f24
|
4
|
+
data.tar.gz: 15ef75bee0a75d0887bafab21198a2fe77a2dc78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6772dcbcbffd2fd43250e041a4c1b9181efa09c2e6617fa927e3b9e9b778f9cd5c53963319ae488fa714e5025070234df93e78fffec44c291944875ce40a6f13
|
7
|
+
data.tar.gz: a9b1c7d94d95684354c515f8e9a3a4e1363863b9e505b0cca239fdd67010496f277f003dcbb73eea9acc621840a19c31eb5897044cfc118581f85ceafc5e2a14
|
@@ -2,27 +2,30 @@ require 'active_model/validator'
|
|
2
2
|
|
3
3
|
class IsValidMultiparameterDateTimeValidator < ActiveModel::EachValidator
|
4
4
|
def validate_each(record, attribute, value)
|
5
|
-
|
6
|
-
|
5
|
+
date_part_key = :"#{attribute}_date_part"
|
6
|
+
date_value = record.public_send(date_part_key)
|
7
7
|
|
8
|
-
|
8
|
+
time_part_key = :"#{attribute}_time_part"
|
9
|
+
time_value = record.public_send(time_part_key)
|
9
10
|
|
10
|
-
if
|
11
|
+
return if !options[:required] && date_value.blank? && time_value.blank?
|
12
|
+
|
13
|
+
if date_value.blank? && time_value.blank?
|
14
|
+
message = "Please enter a date and time for the #{record.class.name.titleize.downcase}."
|
15
|
+
record.errors.add(attribute, message)
|
16
|
+
elsif date_invalid?(date_value) || time_invalid?(time_value)
|
11
17
|
record.errors.add(attribute, self.class.invalid_format_error_message)
|
12
18
|
elsif date_value.blank?
|
13
|
-
|
14
|
-
message = record.errors.generate_message(key, :blank, default: 'Please enter a date.')
|
19
|
+
message = record.errors.generate_message(date_part_key, :blank, default: 'Please enter a date.')
|
15
20
|
record.errors.add(attribute, message)
|
16
21
|
elsif time_value.blank?
|
17
|
-
|
18
|
-
message = record.errors.generate_message(key, :blank, default: 'Please enter a time.')
|
22
|
+
message = record.errors.generate_message(time_part_key, :blank, default: 'Please enter a time.')
|
19
23
|
record.errors.add(attribute, message)
|
20
24
|
else
|
21
|
-
attribute_value = record.public_send(:"#{attribute}_time_part")
|
22
25
|
begin
|
23
26
|
Date.parse(date_value)
|
24
27
|
Time.zone.parse("#{date_value} #{time_value}")
|
25
|
-
Time.zone.parse(
|
28
|
+
Time.zone.parse(time_value)
|
26
29
|
rescue ArgumentError
|
27
30
|
record.errors.add(attribute, self.class.invalid_format_error_message)
|
28
31
|
end
|
@@ -16,7 +16,7 @@ describe IsValidMultiparameterDateTimeValidator do
|
|
16
16
|
model do
|
17
17
|
include MultiparameterDateTime
|
18
18
|
multiparameter_date_time :foo
|
19
|
-
validates :foo, is_valid_multiparameter_date_time: true
|
19
|
+
validates :foo, is_valid_multiparameter_date_time: true
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -386,6 +386,52 @@ describe IsValidMultiparameterDateTimeValidator do
|
|
386
386
|
end
|
387
387
|
end
|
388
388
|
end
|
389
|
+
|
390
|
+
context 'parameter specifying whether the field is required' do
|
391
|
+
context 'when nothing is set at all and the value is not required' do
|
392
|
+
with_model :ModelWithDatetimeRequired do
|
393
|
+
table do |t|
|
394
|
+
t.datetime :foo
|
395
|
+
end
|
396
|
+
|
397
|
+
model do
|
398
|
+
include MultiparameterDateTime
|
399
|
+
multiparameter_date_time :foo
|
400
|
+
validates :foo, is_valid_multiparameter_date_time: { required: false }
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
let(:record) { ModelWithDatetimeRequired.new }
|
405
|
+
|
406
|
+
it 'should show the missing datetime error' do
|
407
|
+
record.valid?
|
408
|
+
expect(record.errors[:foo]).to be_empty
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
context 'when nothing is set at all and a value is required' do
|
413
|
+
with_model :ModelWithDatetimeRequired do
|
414
|
+
table do |t|
|
415
|
+
t.datetime :foo
|
416
|
+
end
|
417
|
+
|
418
|
+
model do
|
419
|
+
include MultiparameterDateTime
|
420
|
+
multiparameter_date_time :foo
|
421
|
+
validates :foo, is_valid_multiparameter_date_time: { required: true }
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
let(:record) { ModelWithDatetimeRequired.new }
|
426
|
+
|
427
|
+
it 'should show the missing datetime error' do
|
428
|
+
record.valid?
|
429
|
+
expect(record.errors[:foo]).to eq [
|
430
|
+
'Please enter a date and time for the model with datetime required.'
|
431
|
+
]
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|
389
435
|
end
|
390
436
|
|
391
437
|
describe 'accepts dates in a variety of formats' do
|
@@ -27,431 +27,424 @@ describe MultiparameterDateTime do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
%w[ModelWithDateTime ActiveModelWithDateTime].each do |model_name|
|
30
|
-
|
31
|
-
|
30
|
+
context "for #{model_name}" do
|
31
|
+
let(:model) { model_name.constantize }
|
32
32
|
|
33
|
-
|
34
|
-
|
33
|
+
let(:record) do
|
34
|
+
model.new(foo_date_part: foo_date_part, foo_time_part: foo_time_part)
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
let(:record)
|
38
|
-
|
37
|
+
describe 'when a value is present' do
|
38
|
+
let(:record) { model.new(foo: Time.zone.parse('1/2/2003 04:05pm')) }
|
39
|
+
it 'assigns date_part' do
|
40
|
+
expect(record.foo_date_part).to eq '1/2/2003'
|
39
41
|
end
|
40
42
|
|
41
|
-
|
43
|
+
it 'assigns time_part' do
|
44
|
+
expect(record.foo_time_part).to eq '4:05 pm'
|
45
|
+
end
|
46
|
+
end
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
expect(subject.foo_date_part).to eq '1/2/2003'
|
47
|
-
end
|
48
|
+
describe 'setting a valid date and time' do
|
49
|
+
let(:foo_date_part) { '01/02/2000' }
|
50
|
+
let(:foo_time_part) { '9:30 pm EST' }
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
+
it 'does not raise an exception' do
|
53
|
+
expect { record }.not_to raise_exception
|
52
54
|
end
|
53
55
|
|
54
|
-
|
55
|
-
|
56
|
-
|
56
|
+
it 'sets the attribute to a DateTime object' do
|
57
|
+
expect(record.foo).to eq Time.zone.parse('1/2/2000 9:30 pm')
|
58
|
+
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
it 'has the original date input' do
|
61
|
+
expect(record.foo_date_part).to eq '01/02/2000'
|
62
|
+
end
|
61
63
|
|
62
|
-
|
63
|
-
|
64
|
-
|
64
|
+
it 'has the original time input' do
|
65
|
+
expect(record.foo_time_part).to eq '9:30 pm EST'
|
66
|
+
end
|
67
|
+
end
|
65
68
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
+
describe 'setting an invalid date' do
|
70
|
+
let(:foo_date_part) { 'bad input' }
|
71
|
+
let(:foo_time_part) { '9:30 pm' }
|
69
72
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
+
it 'does not raise an exception' do
|
74
|
+
expect { record }.not_to raise_exception
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'sets the attribute to :incomplete' do
|
78
|
+
expect(record.foo).to eq :incomplete
|
73
79
|
end
|
74
80
|
|
75
|
-
|
76
|
-
|
81
|
+
it 'has the original date' do
|
82
|
+
expect(record.foo_date_part).to eq 'bad input'
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'has the original time input' do
|
86
|
+
expect(record.foo_time_part).to eq '9:30 pm'
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'valid month but invalid day for the month' do
|
90
|
+
let(:foo_date_part) { '2/31/2015' }
|
77
91
|
let(:foo_time_part) { '9:30 pm' }
|
78
92
|
|
79
93
|
it 'does not raise an exception' do
|
80
|
-
expect {
|
94
|
+
expect { record }.not_to raise_exception
|
81
95
|
end
|
82
96
|
|
83
97
|
it 'sets the attribute to :incomplete' do
|
84
|
-
expect(
|
98
|
+
expect(record.foo).to eq :incomplete
|
85
99
|
end
|
86
100
|
|
87
101
|
it 'has the original date' do
|
88
|
-
expect(
|
102
|
+
expect(record.foo_date_part).to eq '2/31/2015'
|
89
103
|
end
|
90
104
|
|
91
105
|
it 'has the original time input' do
|
92
|
-
expect(
|
106
|
+
expect(record.foo_time_part).to eq '9:30 pm'
|
93
107
|
end
|
108
|
+
end
|
109
|
+
end
|
94
110
|
|
95
|
-
|
96
|
-
|
97
|
-
|
111
|
+
describe 'setting an impossible date' do
|
112
|
+
let(:foo_date_part) { '99/99/9999' }
|
113
|
+
let(:foo_time_part) { '12:30 pm' }
|
98
114
|
|
99
|
-
|
100
|
-
|
101
|
-
|
115
|
+
it 'does not raise an exception' do
|
116
|
+
expect { record }.not_to raise_exception
|
117
|
+
end
|
102
118
|
|
103
|
-
|
104
|
-
|
105
|
-
|
119
|
+
it 'sets the attribute to :incomplete' do
|
120
|
+
expect(record.foo).to eq :incomplete
|
121
|
+
end
|
106
122
|
|
107
|
-
|
108
|
-
|
109
|
-
|
123
|
+
it 'has the original date' do
|
124
|
+
expect(record.foo_date_part).to eq '99/99/9999'
|
125
|
+
end
|
110
126
|
|
111
|
-
|
112
|
-
|
113
|
-
end
|
114
|
-
end
|
127
|
+
it 'has the original time' do
|
128
|
+
expect(record.foo_time_part).to eq '12:30 pm'
|
115
129
|
end
|
130
|
+
end
|
116
131
|
|
117
|
-
|
118
|
-
|
119
|
-
|
132
|
+
describe 'setting an invalid time' do
|
133
|
+
let(:foo_date_part) { '01/02/2000' }
|
134
|
+
let(:foo_time_part) { 'bad input' }
|
120
135
|
|
121
|
-
|
122
|
-
|
123
|
-
|
136
|
+
it 'does not raise an exception' do
|
137
|
+
expect { record }.not_to raise_exception
|
138
|
+
end
|
124
139
|
|
125
|
-
|
126
|
-
|
127
|
-
|
140
|
+
it 'sets the attribute to :incomplete' do
|
141
|
+
expect(record.foo).to eq :incomplete
|
142
|
+
end
|
128
143
|
|
129
|
-
|
130
|
-
|
131
|
-
|
144
|
+
it 'has the original date input' do
|
145
|
+
expect(record.foo_date_part).to eq '01/02/2000'
|
146
|
+
end
|
132
147
|
|
133
|
-
|
134
|
-
|
135
|
-
end
|
148
|
+
it 'has the original time input' do
|
149
|
+
expect(record.foo_time_part).to eq 'bad input'
|
136
150
|
end
|
151
|
+
end
|
137
152
|
|
138
|
-
|
139
|
-
|
140
|
-
|
153
|
+
describe 'setting a impossible time' do
|
154
|
+
let(:foo_date_part) { '01/02/2000' }
|
155
|
+
let(:foo_time_part) { '99:99pm' }
|
141
156
|
|
142
|
-
|
143
|
-
|
144
|
-
|
157
|
+
it 'does not raise an exception' do
|
158
|
+
expect { record }.not_to raise_exception
|
159
|
+
end
|
145
160
|
|
146
|
-
|
147
|
-
|
148
|
-
|
161
|
+
it 'sets the attribute to :incomplete' do
|
162
|
+
expect(record.foo).to eq :incomplete
|
163
|
+
end
|
149
164
|
|
150
|
-
|
151
|
-
|
152
|
-
|
165
|
+
it 'has the original date input' do
|
166
|
+
expect(record.foo_date_part).to eq '01/02/2000'
|
167
|
+
end
|
153
168
|
|
154
|
-
|
155
|
-
|
156
|
-
end
|
169
|
+
it 'has the original time input' do
|
170
|
+
expect(record.foo_time_part).to eq '99:99pm'
|
157
171
|
end
|
172
|
+
end
|
158
173
|
|
159
|
-
|
160
|
-
|
161
|
-
let(:foo_time_part) { '99:99pm' }
|
174
|
+
describe 'setting a date but not a time' do
|
175
|
+
let(:record) { model.new(foo_date_part: '01/01/2000') }
|
162
176
|
|
163
|
-
|
164
|
-
|
165
|
-
|
177
|
+
it 'does not raise an exception' do
|
178
|
+
expect { record }.not_to raise_exception
|
179
|
+
end
|
166
180
|
|
167
|
-
|
168
|
-
|
169
|
-
|
181
|
+
it 'sets the attribute to :incomplete' do
|
182
|
+
expect(record.foo).to eq :incomplete
|
183
|
+
end
|
170
184
|
|
171
|
-
|
172
|
-
|
173
|
-
|
185
|
+
it 'has the original date' do
|
186
|
+
expect(record.foo_date_part).to eq '01/01/2000'
|
187
|
+
end
|
174
188
|
|
175
|
-
|
176
|
-
|
177
|
-
end
|
189
|
+
it 'has the nil for the time input' do
|
190
|
+
expect(record.foo_time_part).to eq nil
|
178
191
|
end
|
192
|
+
end
|
179
193
|
|
180
|
-
|
181
|
-
|
194
|
+
describe 'setting a time but not a date' do
|
195
|
+
let(:record) { model.new(foo_time_part: '12:30 pm') }
|
182
196
|
|
183
|
-
|
184
|
-
|
185
|
-
|
197
|
+
it 'does not raise an exception' do
|
198
|
+
expect { record }.not_to raise_exception
|
199
|
+
end
|
186
200
|
|
187
|
-
|
188
|
-
|
189
|
-
|
201
|
+
it 'sets the attribute to :incomplete' do
|
202
|
+
expect(record.foo).to eq :incomplete
|
203
|
+
end
|
190
204
|
|
191
|
-
|
192
|
-
|
193
|
-
|
205
|
+
it 'has the nil for the date input' do
|
206
|
+
expect(record.foo_date_part).to eq nil
|
207
|
+
end
|
194
208
|
|
195
|
-
|
196
|
-
|
197
|
-
end
|
209
|
+
it 'has the original time' do
|
210
|
+
expect(record.foo_time_part).to eq '12:30 pm'
|
198
211
|
end
|
212
|
+
end
|
199
213
|
|
200
|
-
|
201
|
-
|
214
|
+
describe 'setting incorrect time and date' do
|
215
|
+
let(:record) { model.new(foo_time_part: 'qwer',
|
216
|
+
foo_date_part: 'asdf') }
|
202
217
|
|
203
|
-
|
204
|
-
|
205
|
-
|
218
|
+
it 'does not raise an exception' do
|
219
|
+
expect { record }.not_to raise_exception
|
220
|
+
end
|
206
221
|
|
207
|
-
|
208
|
-
|
209
|
-
|
222
|
+
it 'sets the attribute to :incomplete' do
|
223
|
+
expect(record.foo).to eq :incomplete
|
224
|
+
end
|
210
225
|
|
211
|
-
|
212
|
-
|
213
|
-
|
226
|
+
it 'has the original date' do
|
227
|
+
expect(record.foo_date_part).to eq 'asdf'
|
228
|
+
end
|
214
229
|
|
215
|
-
|
216
|
-
|
217
|
-
end
|
230
|
+
it 'has the original time input' do
|
231
|
+
expect(record.foo_time_part).to eq 'qwer'
|
218
232
|
end
|
233
|
+
end
|
219
234
|
|
220
|
-
|
221
|
-
|
222
|
-
|
235
|
+
describe 'setting neither time nor a date' do
|
236
|
+
let(:record) { model.new(foo_time_part: '',
|
237
|
+
foo_date_part: '') }
|
223
238
|
|
224
|
-
|
225
|
-
|
226
|
-
|
239
|
+
it 'does not raise an exception' do
|
240
|
+
expect { record }.not_to raise_exception
|
241
|
+
end
|
227
242
|
|
228
|
-
|
229
|
-
|
230
|
-
|
243
|
+
it 'has nil for the attribute' do
|
244
|
+
expect(record.foo).to eq nil
|
245
|
+
end
|
231
246
|
|
232
|
-
|
233
|
-
|
234
|
-
|
247
|
+
it 'has the original date' do
|
248
|
+
expect(record.foo_date_part).to eq ''
|
249
|
+
end
|
235
250
|
|
236
|
-
|
237
|
-
|
238
|
-
end
|
251
|
+
it 'has the original time input' do
|
252
|
+
expect(record.foo_time_part).to eq ''
|
239
253
|
end
|
254
|
+
end
|
240
255
|
|
241
|
-
|
242
|
-
|
243
|
-
|
256
|
+
describe 'setting a DateTime directly' do
|
257
|
+
let(:record) { model.new(foo: Time.zone.parse("#{foo_date_part} #{foo_time_part}")) }
|
258
|
+
let(:foo_date_part) { '01/02/2000' }
|
259
|
+
let(:foo_time_part) { '12:30 pm' }
|
244
260
|
|
245
|
-
|
246
|
-
|
247
|
-
|
261
|
+
it 'does not raise an exception' do
|
262
|
+
expect { record }.not_to raise_exception
|
263
|
+
end
|
248
264
|
|
249
|
-
|
250
|
-
|
251
|
-
|
265
|
+
it 'sets the attribute to a DateTime object' do
|
266
|
+
expect(record.foo).to eq Time.zone.parse('01/02/2000 12:30 pm')
|
267
|
+
end
|
252
268
|
|
253
|
-
|
254
|
-
|
255
|
-
|
269
|
+
it 'has the original date' do
|
270
|
+
expect(record.foo_date_part).to eq '1/2/2000'
|
271
|
+
end
|
256
272
|
|
257
|
-
|
258
|
-
|
259
|
-
end
|
273
|
+
it 'has the original time input' do
|
274
|
+
expect(record.foo_time_part).to eq '12:30 pm'
|
260
275
|
end
|
276
|
+
end
|
261
277
|
|
262
|
-
|
263
|
-
|
264
|
-
let(:
|
278
|
+
describe 'setting a String directly' do
|
279
|
+
context 'When the string contains a date and time' do
|
280
|
+
let(:record) { model.new(foo: "#{foo_date_part} #{foo_time_part}") }
|
281
|
+
let(:foo_date_part) { '01/01/2000' }
|
265
282
|
let(:foo_time_part) { '12:30 pm' }
|
266
283
|
|
267
284
|
it 'does not raise an exception' do
|
268
|
-
expect {
|
285
|
+
expect { record }.not_to raise_exception
|
269
286
|
end
|
270
287
|
|
271
288
|
it 'sets the attribute to a DateTime object' do
|
272
|
-
expect(
|
289
|
+
expect(record.foo).to eq Time.zone.parse('01/01/2000 12:30pm')
|
273
290
|
end
|
274
291
|
|
275
292
|
it 'has the original date' do
|
276
|
-
expect(
|
293
|
+
expect(record.foo_date_part).to eq '01/01/2000'
|
277
294
|
end
|
278
295
|
|
279
|
-
it 'has the original time
|
280
|
-
expect(
|
296
|
+
it 'has the original time' do
|
297
|
+
expect(record.foo_time_part).to eq '12:30 pm'
|
281
298
|
end
|
282
299
|
end
|
283
300
|
|
284
|
-
|
285
|
-
|
286
|
-
let(:record) { model.new(foo: "#{foo_date_part} #{foo_time_part}") }
|
287
|
-
let(:foo_date_part) { '01/01/2000' }
|
288
|
-
let(:foo_time_part) { '12:30 pm' }
|
301
|
+
context 'When the string contains an iso8601 datetime' do
|
302
|
+
let(:record) { model.new(foo: '2011-12-03T01:00:00Z') }
|
289
303
|
|
290
|
-
|
291
|
-
|
292
|
-
end
|
293
|
-
|
294
|
-
it 'sets the attribute to a DateTime object' do
|
295
|
-
expect(subject.foo).to eq Time.zone.parse('01/01/2000 12:30pm')
|
296
|
-
end
|
297
|
-
|
298
|
-
it 'has the original date' do
|
299
|
-
expect(subject.foo_date_part).to eq '01/01/2000'
|
300
|
-
end
|
301
|
-
|
302
|
-
it 'has the original time' do
|
303
|
-
expect(subject.foo_time_part).to eq '12:30 pm'
|
304
|
-
end
|
304
|
+
it 'does not raise an exception' do
|
305
|
+
expect { record }.not_to raise_exception
|
305
306
|
end
|
306
307
|
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
it 'does not raise an exception' do
|
311
|
-
expect { subject }.not_to raise_exception
|
312
|
-
end
|
313
|
-
|
314
|
-
it 'sets the attribute to a DateTime object with the correct EST time' do
|
315
|
-
expect(subject.foo).to eq Time.zone.parse('12/2/2011 8:00 pm')
|
316
|
-
end
|
317
|
-
|
318
|
-
it 'has a date' do
|
319
|
-
expect(subject.foo_date_part).to eq '12/2/2011'
|
320
|
-
end
|
321
|
-
|
322
|
-
it 'has a time' do
|
323
|
-
expect(subject.foo_time_part).to eq '8:00 pm'
|
324
|
-
end
|
308
|
+
it 'sets the attribute to a DateTime object with the correct EST time' do
|
309
|
+
expect(record.foo).to eq Time.zone.parse('12/2/2011 8:00 pm')
|
325
310
|
end
|
326
311
|
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
it 'does not raise an exception' do
|
332
|
-
expect { subject }.not_to raise_exception
|
333
|
-
end
|
334
|
-
|
335
|
-
it 'sets the attribute to a DateTime object' do
|
336
|
-
expect(subject.foo).to eq Time.zone.parse('01/01/2000 12:00am')
|
337
|
-
end
|
338
|
-
|
339
|
-
it 'has the original date' do
|
340
|
-
expect(subject.foo_date_part).to eq '1/1/2000'
|
341
|
-
end
|
312
|
+
it 'has a date' do
|
313
|
+
expect(record.foo_date_part).to eq '12/2/2011'
|
314
|
+
end
|
342
315
|
|
343
|
-
|
344
|
-
|
345
|
-
end
|
316
|
+
it 'has a time' do
|
317
|
+
expect(record.foo_time_part).to eq '8:00 pm'
|
346
318
|
end
|
347
319
|
end
|
348
320
|
|
349
|
-
|
350
|
-
let(:record) { model.new(foo:
|
351
|
-
|
321
|
+
context 'When the string contains only a date' do
|
322
|
+
let(:record) { model.new(foo: "#{foo_date_part}") }
|
352
323
|
let(:foo_date_part) { '01/01/2000' }
|
353
324
|
|
354
325
|
it 'does not raise an exception' do
|
355
|
-
expect {
|
326
|
+
expect { record }.not_to raise_exception
|
356
327
|
end
|
357
328
|
|
358
|
-
it 'sets the attribute to a DateTime object
|
359
|
-
expect(
|
329
|
+
it 'sets the attribute to a DateTime object' do
|
330
|
+
expect(record.foo).to eq Time.zone.parse('01/01/2000 12:00am')
|
360
331
|
end
|
361
332
|
|
362
333
|
it 'has the original date' do
|
363
|
-
expect(
|
334
|
+
expect(record.foo_date_part).to eq '1/1/2000'
|
364
335
|
end
|
365
336
|
|
366
337
|
it 'has midnight for the time input' do
|
367
|
-
expect(
|
338
|
+
expect(record.foo_time_part).to eq '12:00 am'
|
368
339
|
end
|
369
340
|
end
|
341
|
+
end
|
370
342
|
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
343
|
+
describe 'setting a Date directly' do
|
344
|
+
let(:record) { model.new(foo: Date.parse(foo_date_part)) }
|
345
|
+
|
346
|
+
let(:foo_date_part) { '01/01/2000' }
|
347
|
+
|
348
|
+
it 'does not raise an exception' do
|
349
|
+
expect { record }.not_to raise_exception
|
378
350
|
end
|
379
351
|
|
380
|
-
|
381
|
-
|
352
|
+
it 'sets the attribute to a DateTime object in the current time zone' do
|
353
|
+
expect(record.foo).to eq Time.zone.parse('01/01/2000 12:00 am')
|
354
|
+
end
|
382
355
|
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
end
|
356
|
+
it 'has the original date' do
|
357
|
+
expect(record.foo_date_part).to eq '1/1/2000'
|
358
|
+
end
|
387
359
|
|
388
|
-
|
389
|
-
|
390
|
-
|
360
|
+
it 'has midnight for the time input' do
|
361
|
+
expect(record.foo_time_part).to eq '12:00 am'
|
362
|
+
end
|
363
|
+
end
|
391
364
|
|
392
|
-
|
393
|
-
|
394
|
-
|
365
|
+
describe 'setting to nil' do
|
366
|
+
it 'is nil' do
|
367
|
+
record = ModelWithDateTime.new
|
368
|
+
record.foo = Time.current
|
369
|
+
record.foo = nil
|
370
|
+
expect(record.foo).to eq nil
|
371
|
+
end
|
372
|
+
end
|
395
373
|
|
396
|
-
|
397
|
-
|
398
|
-
|
374
|
+
describe 'configuring the datetime format' do
|
375
|
+
let(:record) { model.new(foo: Time.zone.parse('01/09/2000 1:30 pm')) }
|
376
|
+
|
377
|
+
context 'when the date format is set' do
|
378
|
+
before do
|
379
|
+
MultiparameterDateTime.date_format = '%-m-%-e-%0y'
|
399
380
|
end
|
400
381
|
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
end
|
382
|
+
it 'formats the date properly' do
|
383
|
+
expect(record.foo_date_part).to eq '1-9-00'
|
384
|
+
end
|
405
385
|
|
406
|
-
|
407
|
-
|
408
|
-
|
386
|
+
it 'uses the default format for the time' do
|
387
|
+
expect(record.foo_time_part).to eq '1:30 pm'
|
388
|
+
end
|
409
389
|
|
410
|
-
|
411
|
-
|
412
|
-
|
390
|
+
after do
|
391
|
+
MultiparameterDateTime.date_format = MultiparameterDateTime::DEFAULT_DATE_FORMAT
|
392
|
+
end
|
393
|
+
end
|
413
394
|
|
414
|
-
|
415
|
-
|
416
|
-
|
395
|
+
context 'when the time format is set' do
|
396
|
+
before do
|
397
|
+
MultiparameterDateTime.time_format = '%H%M hours'
|
398
|
+
end
|
399
|
+
|
400
|
+
it 'formats the time properly' do
|
401
|
+
expect(record.foo_time_part).to eq '1330 hours'
|
402
|
+
end
|
403
|
+
|
404
|
+
it 'uses the default format for the date' do
|
405
|
+
expect(record.foo_date_part).to eq '1/9/2000'
|
406
|
+
end
|
407
|
+
|
408
|
+
after do
|
409
|
+
MultiparameterDateTime.time_format = MultiparameterDateTime::DEFAULT_TIME_FORMAT
|
417
410
|
end
|
418
411
|
end
|
412
|
+
end
|
419
413
|
|
420
|
-
|
421
|
-
|
422
|
-
|
414
|
+
describe 'using a custom date string formatter' do
|
415
|
+
let(:foo_date_part) { '1/12/15' }
|
416
|
+
let(:foo_time_part) { '9:30 pm EST' }
|
423
417
|
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
418
|
+
context 'when the date string formatter is nil' do
|
419
|
+
it 'parses the unmodified date string' do
|
420
|
+
MultiparameterDateTime.date_string_formatter = nil
|
421
|
+
expect(Time.zone).to receive(:parse)
|
422
|
+
.with("#{foo_date_part} #{foo_time_part}")
|
429
423
|
|
430
|
-
|
431
|
-
end
|
424
|
+
model.new(foo_date_part: foo_date_part, foo_time_part: foo_time_part)
|
432
425
|
end
|
426
|
+
end
|
433
427
|
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
end
|
428
|
+
context 'when the date string formatter is present' do
|
429
|
+
before do
|
430
|
+
class TestFormatter
|
431
|
+
def self.format(date_string)
|
432
|
+
'12/31/1995'
|
440
433
|
end
|
441
|
-
|
442
|
-
MultiparameterDateTime.date_string_formatter = TestFormatter
|
443
434
|
end
|
444
435
|
|
445
|
-
|
446
|
-
|
447
|
-
end
|
436
|
+
MultiparameterDateTime.date_string_formatter = TestFormatter
|
437
|
+
end
|
448
438
|
|
449
|
-
|
450
|
-
|
451
|
-
|
439
|
+
after do
|
440
|
+
MultiparameterDateTime.date_string_formatter = nil
|
441
|
+
end
|
452
442
|
|
453
|
-
|
454
|
-
|
443
|
+
it 'uses a formatted date string' do
|
444
|
+
expect(Time.zone).to receive(:parse)
|
445
|
+
.with("#{'12/31/1995'} #{foo_time_part}")
|
446
|
+
|
447
|
+
model.new(foo_date_part: foo_date_part, foo_time_part: foo_time_part)
|
455
448
|
end
|
456
449
|
end
|
457
450
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multiparameter_date_time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Case Commons, LLC
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2016-
|
14
|
+
date: 2016-05-20 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: american_date
|