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: 7dbe4e334a2cdfcaffabfc6e283e801b6ec7a2c4
4
- data.tar.gz: f1dc19849afde4d9ca07c7e8c0d11bbcb82a5ebd
3
+ metadata.gz: 3a7fd23d65051eaecbc7c17b18806daa43cd2f24
4
+ data.tar.gz: 15ef75bee0a75d0887bafab21198a2fe77a2dc78
5
5
  SHA512:
6
- metadata.gz: 4246599e275e0200c000547dcbeffa5fa0be0731aaaba88c424311d80b63e0cbe4994cb41825cf49e0845a8393171571fe119fc3220cd7de77495bc64afafb4c
7
- data.tar.gz: e0c95dcb0ddcfb50a5ef77b57fc108e15413720194dd191552fd8630a30cdf509605b3e997678bcb18cf9eb47dd9ab898d5c946e70c2e10b8b056ea44e074c6a
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
- date_value = record.public_send(:"#{attribute}_date_part")
6
- time_value = record.public_send(:"#{attribute}_time_part")
5
+ date_part_key = :"#{attribute}_date_part"
6
+ date_value = record.public_send(date_part_key)
7
7
 
8
- return if date_value.blank? && time_value.blank?
8
+ time_part_key = :"#{attribute}_time_part"
9
+ time_value = record.public_send(time_part_key)
9
10
 
10
- if date_invalid?(date_value) || time_invalid?(time_value)
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
- key = :"#{attribute}_date_part"
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
- key = :"#{attribute}_time_part"
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(attribute_value)
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
@@ -1,3 +1,3 @@
1
1
  module MultiparameterDateTime
2
- VERSION = '0.3.6'
2
+ VERSION = '0.4.0'
3
3
  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, allow_blank: 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
- let(:parent_model) { model_name.constantize }
31
- let(:subclass_model) { Class.new(parent_model) }
30
+ context "for #{model_name}" do
31
+ let(:model) { model_name.constantize }
32
32
 
33
- [false, true].each do |use_subclass|
34
- let(:model) { use_subclass ? subclass_model : parent_model }
33
+ let(:record) do
34
+ model.new(foo_date_part: foo_date_part, foo_time_part: foo_time_part)
35
+ end
35
36
 
36
- context "for #{"a subclass of " if use_subclass}#{model_name}" do
37
- let(:record) do
38
- model.new(foo_date_part: foo_date_part, foo_time_part: foo_time_part)
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
- subject { record }
43
+ it 'assigns time_part' do
44
+ expect(record.foo_time_part).to eq '4:05 pm'
45
+ end
46
+ end
42
47
 
43
- describe 'when a value is present' do
44
- let(:record) { model.new(foo: Time.zone.parse('1/2/2003 04:05pm')) }
45
- it 'assigns date_part' do
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
- it 'assigns time_part' do
50
- expect(subject.foo_time_part).to eq '4:05 pm'
51
- end
52
+ it 'does not raise an exception' do
53
+ expect { record }.not_to raise_exception
52
54
  end
53
55
 
54
- describe 'setting a valid date and time' do
55
- let(:foo_date_part) { '01/02/2000' }
56
- let(:foo_time_part) { '9:30 pm EST' }
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
- it 'does not raise an exception' do
59
- expect { subject }.not_to raise_exception
60
- end
60
+ it 'has the original date input' do
61
+ expect(record.foo_date_part).to eq '01/02/2000'
62
+ end
61
63
 
62
- it 'sets the attribute to a DateTime object' do
63
- expect(subject.foo).to eq Time.zone.parse('1/2/2000 9:30 pm')
64
- end
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
- it 'has the original date input' do
67
- expect(subject.foo_date_part).to eq '01/02/2000'
68
- end
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
- it 'has the original time input' do
71
- expect(subject.foo_time_part).to eq '9:30 pm EST'
72
- end
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
- describe 'setting an invalid date' do
76
- let(:foo_date_part) { 'bad input' }
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 { subject }.not_to raise_exception
94
+ expect { record }.not_to raise_exception
81
95
  end
82
96
 
83
97
  it 'sets the attribute to :incomplete' do
84
- expect(subject.foo).to eq :incomplete
98
+ expect(record.foo).to eq :incomplete
85
99
  end
86
100
 
87
101
  it 'has the original date' do
88
- expect(subject.foo_date_part).to eq 'bad input'
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(subject.foo_time_part).to eq '9:30 pm'
106
+ expect(record.foo_time_part).to eq '9:30 pm'
93
107
  end
108
+ end
109
+ end
94
110
 
95
- context 'valid month but invalid day for the month' do
96
- let(:foo_date_part) { '2/31/2015' }
97
- let(:foo_time_part) { '9:30 pm' }
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
- it 'does not raise an exception' do
100
- expect { subject }.not_to raise_exception
101
- end
115
+ it 'does not raise an exception' do
116
+ expect { record }.not_to raise_exception
117
+ end
102
118
 
103
- it 'sets the attribute to :incomplete' do
104
- expect(subject.foo).to eq :incomplete
105
- end
119
+ it 'sets the attribute to :incomplete' do
120
+ expect(record.foo).to eq :incomplete
121
+ end
106
122
 
107
- it 'has the original date' do
108
- expect(subject.foo_date_part).to eq '2/31/2015'
109
- end
123
+ it 'has the original date' do
124
+ expect(record.foo_date_part).to eq '99/99/9999'
125
+ end
110
126
 
111
- it 'has the original time input' do
112
- expect(subject.foo_time_part).to eq '9:30 pm'
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
- describe 'setting an impossible date' do
118
- let(:foo_date_part) { '99/99/9999' }
119
- let(:foo_time_part) { '12:30 pm' }
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
- it 'does not raise an exception' do
122
- expect { subject }.not_to raise_exception
123
- end
136
+ it 'does not raise an exception' do
137
+ expect { record }.not_to raise_exception
138
+ end
124
139
 
125
- it 'sets the attribute to :incomplete' do
126
- expect(subject.foo).to eq :incomplete
127
- end
140
+ it 'sets the attribute to :incomplete' do
141
+ expect(record.foo).to eq :incomplete
142
+ end
128
143
 
129
- it 'has the original date' do
130
- expect(subject.foo_date_part).to eq '99/99/9999'
131
- end
144
+ it 'has the original date input' do
145
+ expect(record.foo_date_part).to eq '01/02/2000'
146
+ end
132
147
 
133
- it 'has the original time' do
134
- expect(subject.foo_time_part).to eq '12:30 pm'
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
- describe 'setting an invalid time' do
139
- let(:foo_date_part) { '01/02/2000' }
140
- let(:foo_time_part) { 'bad input' }
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
- it 'does not raise an exception' do
143
- expect { subject }.not_to raise_exception
144
- end
157
+ it 'does not raise an exception' do
158
+ expect { record }.not_to raise_exception
159
+ end
145
160
 
146
- it 'sets the attribute to :incomplete' do
147
- expect(subject.foo).to eq :incomplete
148
- end
161
+ it 'sets the attribute to :incomplete' do
162
+ expect(record.foo).to eq :incomplete
163
+ end
149
164
 
150
- it 'has the original date input' do
151
- expect(subject.foo_date_part).to eq '01/02/2000'
152
- end
165
+ it 'has the original date input' do
166
+ expect(record.foo_date_part).to eq '01/02/2000'
167
+ end
153
168
 
154
- it 'has the original time input' do
155
- expect(subject.foo_time_part).to eq 'bad input'
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
- describe 'setting a impossible time' do
160
- let(:foo_date_part) { '01/02/2000' }
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
- it 'does not raise an exception' do
164
- expect { subject }.not_to raise_exception
165
- end
177
+ it 'does not raise an exception' do
178
+ expect { record }.not_to raise_exception
179
+ end
166
180
 
167
- it 'sets the attribute to :incomplete' do
168
- expect(subject.foo).to eq :incomplete
169
- end
181
+ it 'sets the attribute to :incomplete' do
182
+ expect(record.foo).to eq :incomplete
183
+ end
170
184
 
171
- it 'has the original date input' do
172
- expect(subject.foo_date_part).to eq '01/02/2000'
173
- end
185
+ it 'has the original date' do
186
+ expect(record.foo_date_part).to eq '01/01/2000'
187
+ end
174
188
 
175
- it 'has the original time input' do
176
- expect(subject.foo_time_part).to eq '99:99pm'
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
- describe 'setting a date but not a time' do
181
- let(:record) { model.new(foo_date_part: '01/01/2000') }
194
+ describe 'setting a time but not a date' do
195
+ let(:record) { model.new(foo_time_part: '12:30 pm') }
182
196
 
183
- it 'does not raise an exception' do
184
- expect { subject }.not_to raise_exception
185
- end
197
+ it 'does not raise an exception' do
198
+ expect { record }.not_to raise_exception
199
+ end
186
200
 
187
- it 'sets the attribute to :incomplete' do
188
- expect(subject.foo).to eq :incomplete
189
- end
201
+ it 'sets the attribute to :incomplete' do
202
+ expect(record.foo).to eq :incomplete
203
+ end
190
204
 
191
- it 'has the original date' do
192
- expect(subject.foo_date_part).to eq '01/01/2000'
193
- end
205
+ it 'has the nil for the date input' do
206
+ expect(record.foo_date_part).to eq nil
207
+ end
194
208
 
195
- it 'has the nil for the time input' do
196
- expect(subject.foo_time_part).to eq nil
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
- describe 'setting a time but not a date' do
201
- let(:record) { model.new(foo_time_part: '12:30 pm') }
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
- it 'does not raise an exception' do
204
- expect { subject }.not_to raise_exception
205
- end
218
+ it 'does not raise an exception' do
219
+ expect { record }.not_to raise_exception
220
+ end
206
221
 
207
- it 'sets the attribute to :incomplete' do
208
- expect(subject.foo).to eq :incomplete
209
- end
222
+ it 'sets the attribute to :incomplete' do
223
+ expect(record.foo).to eq :incomplete
224
+ end
210
225
 
211
- it 'has the nil for the date input' do
212
- expect(subject.foo_date_part).to eq nil
213
- end
226
+ it 'has the original date' do
227
+ expect(record.foo_date_part).to eq 'asdf'
228
+ end
214
229
 
215
- it 'has the original time' do
216
- expect(subject.foo_time_part).to eq '12:30 pm'
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
- describe 'setting incorrect time and date' do
221
- let(:record) { model.new(foo_time_part: 'qwer',
222
- foo_date_part: 'asdf') }
235
+ describe 'setting neither time nor a date' do
236
+ let(:record) { model.new(foo_time_part: '',
237
+ foo_date_part: '') }
223
238
 
224
- it 'does not raise an exception' do
225
- expect { subject }.not_to raise_exception
226
- end
239
+ it 'does not raise an exception' do
240
+ expect { record }.not_to raise_exception
241
+ end
227
242
 
228
- it 'sets the attribute to :incomplete' do
229
- expect(subject.foo).to eq :incomplete
230
- end
243
+ it 'has nil for the attribute' do
244
+ expect(record.foo).to eq nil
245
+ end
231
246
 
232
- it 'has the original date' do
233
- expect(subject.foo_date_part).to eq 'asdf'
234
- end
247
+ it 'has the original date' do
248
+ expect(record.foo_date_part).to eq ''
249
+ end
235
250
 
236
- it 'has the original time input' do
237
- expect(subject.foo_time_part).to eq 'qwer'
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
- describe 'setting neither time nor a date' do
242
- let(:record) { model.new(foo_time_part: '',
243
- foo_date_part: '') }
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
- it 'does not raise an exception' do
246
- expect { subject }.not_to raise_exception
247
- end
261
+ it 'does not raise an exception' do
262
+ expect { record }.not_to raise_exception
263
+ end
248
264
 
249
- it 'has nil for the attribute' do
250
- expect(subject.foo).to eq nil
251
- end
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
- it 'has the original date' do
254
- expect(subject.foo_date_part).to eq ''
255
- end
269
+ it 'has the original date' do
270
+ expect(record.foo_date_part).to eq '1/2/2000'
271
+ end
256
272
 
257
- it 'has the original time input' do
258
- expect(subject.foo_time_part).to eq ''
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
- describe 'setting a DateTime directly' do
263
- let(:record) { model.new(foo: Time.zone.parse("#{foo_date_part} #{foo_time_part}")) }
264
- let(:foo_date_part) { '01/02/2000' }
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 { subject }.not_to raise_exception
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(subject.foo).to eq Time.zone.parse('01/02/2000 12:30 pm')
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(subject.foo_date_part).to eq '1/2/2000'
293
+ expect(record.foo_date_part).to eq '01/01/2000'
277
294
  end
278
295
 
279
- it 'has the original time input' do
280
- expect(subject.foo_time_part).to eq '12:30 pm'
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
- describe 'setting a String directly' do
285
- context 'When the string contains a date and time' do
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
- it 'does not raise an exception' do
291
- expect { subject }.not_to raise_exception
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
- context 'When the string contains an iso8601 datetime' do
308
- let(:record) { model.new(foo: '2011-12-03T01:00:00Z') }
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
- context 'When the string contains only a date' do
328
- let(:record) { model.new(foo: "#{foo_date_part}") }
329
- let(:foo_date_part) { '01/01/2000' }
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
- it 'has midnight for the time input' do
344
- expect(subject.foo_time_part).to eq '12:00 am'
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
- describe 'setting a Date directly' do
350
- let(:record) { model.new(foo: Date.parse(foo_date_part)) }
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 { subject }.not_to raise_exception
326
+ expect { record }.not_to raise_exception
356
327
  end
357
328
 
358
- it 'sets the attribute to a DateTime object in the current time zone' do
359
- expect(subject.foo).to eq Time.zone.parse('01/01/2000 12:00 am')
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(subject.foo_date_part).to eq '1/1/2000'
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(subject.foo_time_part).to eq '12:00 am'
338
+ expect(record.foo_time_part).to eq '12:00 am'
368
339
  end
369
340
  end
341
+ end
370
342
 
371
- describe 'setting to nil' do
372
- it 'is nil' do
373
- record = ModelWithDateTime.new
374
- record.foo = Time.current
375
- record.foo = nil
376
- expect(record.foo).to eq nil
377
- end
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
- describe 'configuring the datetime format' do
381
- let(:record) { model.new(foo: Time.zone.parse('01/09/2000 1:30 pm')) }
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
- context 'when the date format is set' do
384
- before do
385
- MultiparameterDateTime.date_format = '%-m-%-e-%0y'
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
- it 'formats the date properly' do
389
- expect(subject.foo_date_part).to eq '1-9-00'
390
- end
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
- it 'uses the default format for the time' do
393
- expect(subject.foo_time_part).to eq '1:30 pm'
394
- end
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
- after do
397
- MultiparameterDateTime.date_format = MultiparameterDateTime::DEFAULT_DATE_FORMAT
398
- end
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
- context 'when the time format is set' do
402
- before do
403
- MultiparameterDateTime.time_format = '%H%M hours'
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
- it 'formats the time properly' do
407
- expect(subject.foo_time_part).to eq '1330 hours'
408
- end
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
- it 'uses the default format for the date' do
411
- expect(subject.foo_date_part).to eq '1/9/2000'
412
- end
390
+ after do
391
+ MultiparameterDateTime.date_format = MultiparameterDateTime::DEFAULT_DATE_FORMAT
392
+ end
393
+ end
413
394
 
414
- after do
415
- MultiparameterDateTime.time_format = MultiparameterDateTime::DEFAULT_TIME_FORMAT
416
- end
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
- describe 'using a custom date string formatter' do
421
- let(:foo_date_part) { '1/12/15' }
422
- let(:foo_time_part) { '9:30 pm EST' }
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
- context 'when the date string formatter is nil' do
425
- it 'parses the unmodified date string' do
426
- MultiparameterDateTime.date_string_formatter = nil
427
- expect(Time.zone).to receive(:parse)
428
- .with("#{foo_date_part} #{foo_time_part}")
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
- model.new(foo_date_part: foo_date_part, foo_time_part: foo_time_part)
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
- context 'when the date string formatter is present' do
435
- before do
436
- class TestFormatter
437
- def self.format(date_string)
438
- '12/31/1995'
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
- after do
446
- MultiparameterDateTime.date_string_formatter = nil
447
- end
436
+ MultiparameterDateTime.date_string_formatter = TestFormatter
437
+ end
448
438
 
449
- it 'uses a formatted date string' do
450
- expect(Time.zone).to receive(:parse)
451
- .with("#{'12/31/1995'} #{foo_time_part}")
439
+ after do
440
+ MultiparameterDateTime.date_string_formatter = nil
441
+ end
452
442
 
453
- model.new(foo_date_part: foo_date_part, foo_time_part: foo_time_part)
454
- end
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.3.6
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-01-11 00:00:00.000000000 Z
14
+ date: 2016-05-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: american_date