multiparameter_date_time 0.3.4 → 0.3.5

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: 6ddf8a324cd35af351af34df75e7a89f3c159554
4
- data.tar.gz: a025da039da052889e1fd2627eaa3e94cce1341b
3
+ metadata.gz: fd4b646671a89153334e24e8e5965616725df430
4
+ data.tar.gz: f2e6aef9a92bd20cb6334b53e62b23156404476d
5
5
  SHA512:
6
- metadata.gz: 9a2131a2c0a35f1c7eb7f1037134a714f58c21258eaae2259d603dfb2d765d74c813a69fbc35b11a806f428f46e97f07baf5a092fcbea31145f8d5f6add08592
7
- data.tar.gz: ad58b2a5835233d8bf5256a64f8d4529f8ca248539c08eccf78c87213f4d34dbd4a2a480a539f10b084bea0950aeaa95a563816fce3653c508ba79e2d6969b81
6
+ metadata.gz: a43cc2612bc431a19085a49264dd4045c10f9b6169edbbc99a4248a95d628b067822c13333b2192d17576172aab3efe44547a26da8821f1d6116009a0c2ce092
7
+ data.tar.gz: a067365b7f8aad02982b715b2aac8ffa22ec9fb491c8ba66cd694549e175fb39fcf9e1efd31771a8ee40b786e3f71f39de6422624f9322190103d40d4cc8d465
data/README.md CHANGED
@@ -79,7 +79,7 @@ validates :published_at, presence: true, is_valid_multiparameter_date_time: true
79
79
  IsValidMultiparameterDateTimeValidator.invalid_format_error_message
80
80
  ```
81
81
 
82
- ## Contributing
82
+ ### Contributing
83
83
 
84
84
  1. Fork it
85
85
  2. Create your feature branch (`git checkout -b my-new-feature`)
@@ -87,6 +87,25 @@ IsValidMultiparameterDateTimeValidator.invalid_format_error_message
87
87
  4. Push to the branch (`git push origin my-new-feature`)
88
88
  5. Create new Pull Request
89
89
 
90
+ ### Customizing the error message via I18n
91
+
92
+ This gem supports custom error messages using the I18n gem. There is one message key
93
+ for a missing date part and another for a missing time part. Default messages are
94
+ provided if you choose not to supply your own.
95
+
96
+ ```yaml
97
+ en:
98
+ activerecord:
99
+ errors:
100
+ models:
101
+ <your_model>:
102
+ attributes:
103
+ ended_at_time_part:
104
+ blank: '<your message>'
105
+ ended_at_date_part:
106
+ blank: '<your message>'
107
+ ```
108
+
90
109
  ## License
91
110
 
92
111
  Copyright © 2012–2013 Case Commons, LLC. License is available in the LICENSE file.
@@ -10,9 +10,13 @@ class IsValidMultiparameterDateTimeValidator < ActiveModel::EachValidator
10
10
  if date_invalid?(date_value) || time_invalid?(time_value)
11
11
  record.errors.add(attribute, self.class.invalid_format_error_message)
12
12
  elsif date_value.blank?
13
- record.errors.add(attribute, 'Please enter a date.')
13
+ key = :"#{attribute}_date_part"
14
+ message = record.errors.generate_message(key, :blank, default: 'Please enter a date.')
15
+ record.errors.add(attribute, message)
14
16
  elsif time_value.blank?
15
- record.errors.add(attribute, 'Please enter a time.')
17
+ key = :"#{attribute}_time_part"
18
+ message = record.errors.generate_message(key, :blank, default: 'Please enter a time.')
19
+ record.errors.add(attribute, message)
16
20
  else
17
21
  attribute_value = record.public_send(:"#{attribute}_time_part")
18
22
  begin
@@ -1,3 +1,3 @@
1
1
  module MultiparameterDateTime
2
- VERSION = '0.3.4'
2
+ VERSION = '0.3.5'
3
3
  end
@@ -21,6 +21,6 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency 'activesupport', '~> 4.0'
22
22
 
23
23
  gem.add_development_dependency 'activerecord'
24
- gem.add_development_dependency 'rspec', '~> 2.11'
24
+ gem.add_development_dependency 'rspec', '~> 3'
25
25
  gem.add_development_dependency 'with_model', '~> 1.0'
26
26
  end
@@ -22,12 +22,14 @@ describe IsValidMultiparameterDateTimeValidator do
22
22
 
23
23
  shared_examples_for "a valid time" do
24
24
  it "should not have an error" do
25
+ record.valid?
25
26
  record.errors[:foo].should be_empty
26
27
  end
27
28
  end
28
29
 
29
30
  shared_examples_for "a badly formatted date or time" do
30
31
  it "should show the bad format error" do
32
+ record.valid?
31
33
  record.errors[:foo].should == [bad_format_error]
32
34
  end
33
35
  end
@@ -47,8 +49,6 @@ describe IsValidMultiparameterDateTimeValidator do
47
49
  let(:missing_time_error) { 'Please enter a time.' }
48
50
  let(:missing_date_error) { 'Please enter a date.' }
49
51
 
50
- before { record.valid? }
51
-
52
52
  context "with valid date" do
53
53
  let(:date_string) { '01/01/2001' }
54
54
 
@@ -124,8 +124,35 @@ describe IsValidMultiparameterDateTimeValidator do
124
124
  [' ', nil].each do |time_value|
125
125
  context "with time = #{time_value.inspect}" do
126
126
  let(:time_string) { time_value }
127
- it "should show the missing time error" do
128
- record.errors[:foo].should == [missing_time_error]
127
+
128
+ context 'and default error messages' do
129
+ it 'should show the missing time error' do
130
+ record.valid?
131
+ expect(record.errors[:foo]).to match_array [missing_time_error]
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ [' ', nil].each do |time_value|
138
+ context 'with I18n error messages' do
139
+ let(:time_string) { time_value }
140
+
141
+ before do
142
+ translation = {
143
+ activerecord: {
144
+ errors: {
145
+ models: {
146
+ model_with_datetime: {
147
+ attributes: {
148
+ foo_time_part: { blank: 'custom error' } } } } } } }
149
+
150
+ I18n.backend.store_translations :en, translation
151
+ end
152
+
153
+ it 'should show the I18n missing time error' do
154
+ record.valid?
155
+ expect(record.errors[:foo]).to match_array ['custom error']
129
156
  end
130
157
  end
131
158
  end
@@ -192,6 +219,7 @@ describe IsValidMultiparameterDateTimeValidator do
192
219
  let(:time_string) { '12:31pm' }
193
220
 
194
221
  it "should show the missing date error" do
222
+ record.valid?
195
223
  record.errors[:foo].should == [missing_date_error]
196
224
  end
197
225
  end
@@ -202,10 +230,11 @@ describe IsValidMultiparameterDateTimeValidator do
202
230
  it_should_behave_like "a badly formatted date or time"
203
231
  end
204
232
 
205
- [" ", nil].each do |time_value|
233
+ [' ', nil].each do |time_value|
206
234
  context "with time = #{time_value.inspect}" do
207
235
  let(:time_string) { time_value }
208
- it "should not have an error" do
236
+ it 'should not have an error' do
237
+ record.valid?
209
238
  record.errors[:foo].should be_empty
210
239
  end
211
240
  end
@@ -213,9 +242,36 @@ describe IsValidMultiparameterDateTimeValidator do
213
242
  end
214
243
  end
215
244
 
245
+ [' ', nil].each do |date_value|
246
+ context "with valid time" do
247
+ let(:date_string) { date_value }
248
+ let(:time_string) { '12:31pm' }
249
+
250
+ before do
251
+ translation = {
252
+ activerecord: {
253
+ errors: {
254
+ models: {
255
+ model_with_datetime: {
256
+ attributes: {
257
+ foo_date_part: { blank: 'custom error' } } } } } } }
258
+
259
+ I18n.backend.store_translations :en, translation
260
+ end
261
+
262
+ context 'and I18n error messages' do
263
+ it "should show the missing date error" do
264
+ record.valid?
265
+ record.errors[:foo].should == ['custom error']
266
+ end
267
+ end
268
+ end
269
+ end
270
+
216
271
  context "when the datetime is set directly" do
217
272
  let(:record) { ModelWithDatetime.new(foo: Time.current) }
218
273
  it "should not have an error" do
274
+ record.valid?
219
275
  record.errors[:foo].should be_empty
220
276
  end
221
277
  end
@@ -223,6 +279,7 @@ describe IsValidMultiparameterDateTimeValidator do
223
279
  context "when the datetime is set directly to nil" do
224
280
  let(:record) { ModelWithDatetime.new(foo: nil) }
225
281
  it "should not have an error" do
282
+ record.valid?
226
283
  record.errors[:foo].should be_empty
227
284
  end
228
285
  end
@@ -230,6 +287,7 @@ describe IsValidMultiparameterDateTimeValidator do
230
287
  context "when nothing is set at all" do
231
288
  let(:record) { ModelWithDatetime.new }
232
289
  it "should not have an error" do
290
+ record.valid?
233
291
  record.errors[:foo].should be_empty
234
292
  end
235
293
  end
@@ -277,10 +335,10 @@ describe IsValidMultiparameterDateTimeValidator do
277
335
  context "when the date format is set" do
278
336
  before do
279
337
  MultiparameterDateTime.date_format = '%-m-%-e-%0y'
280
- record.valid?
281
338
  end
282
339
 
283
340
  it "should show the bad format error" do
341
+ record.valid?
284
342
  record.errors[:foo].should == [
285
343
  'Please enter a valid date and time using the following formats: 1-29-00, 5:15 pm'
286
344
  ]
@@ -296,10 +354,10 @@ describe IsValidMultiparameterDateTimeValidator do
296
354
 
297
355
  before do
298
356
  MultiparameterDateTime.time_format = '%H%M hours'
299
- record.valid?
300
357
  end
301
358
 
302
359
  it "should show the bad format error" do
360
+ record.valid?
303
361
  record.errors[:foo].should == [
304
362
  'Please enter a valid date and time using the following formats: 1/29/2000, 1715 hours'
305
363
  ]
@@ -329,11 +387,9 @@ describe IsValidMultiparameterDateTimeValidator do
329
387
  end
330
388
 
331
389
  describe ".invalid_format_error_message" do
332
- subject { IsValidMultiparameterDateTimeValidator.invalid_format_error_message }
333
-
334
390
  it do
335
- should ==
336
- 'Please enter a valid date and time using the following formats: 1/29/2000, 5:15 pm'
391
+ expected_error = 'Please enter a valid date and time using the following formats: 1/29/2000, 5:15 pm'
392
+ expect(described_class.invalid_format_error_message).to eq expected_error
337
393
  end
338
394
  end
339
395
  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.4
4
+ version: 0.3.5
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: 2015-06-09 00:00:00.000000000 Z
14
+ date: 2015-06-23 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: american_date
@@ -61,14 +61,14 @@ dependencies:
61
61
  requirements:
62
62
  - - "~>"
63
63
  - !ruby/object:Gem::Version
64
- version: '2.11'
64
+ version: '3'
65
65
  type: :development
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
68
68
  requirements:
69
69
  - - "~>"
70
70
  - !ruby/object:Gem::Version
71
- version: '2.11'
71
+ version: '3'
72
72
  - !ruby/object:Gem::Dependency
73
73
  name: with_model
74
74
  requirement: !ruby/object:Gem::Requirement