validates_timeliness 4.1.1 → 5.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,8 +20,8 @@ RSpec.describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
20
20
  "birth_datetime(6i)" => '14',
21
21
  }
22
22
  person.birth_datetime = nil
23
- @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
24
- should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'February', :day => 29, :hour => 12, :min => 13, :sec => 14)
23
+ @output = datetime_select(:person, :birth_datetime, include_blank: true, include_seconds: true)
24
+ should_have_datetime_selected(:birth_datetime, year: 2009, month: 'February', day: 29, hour: 12, min: 13, sec: 14)
25
25
  end
26
26
 
27
27
  it "should override object values and use params if present" do
@@ -34,26 +34,26 @@ RSpec.describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
34
34
  "birth_datetime(6i)" => '14',
35
35
  }
36
36
  person.birth_datetime = "2010-01-01 15:16:17"
37
- @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
38
- should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'February', :day => 29, :hour => 12, :min => 13, :sec => 14)
37
+ @output = datetime_select(:person, :birth_datetime, include_blank: true, include_seconds: true)
38
+ should_have_datetime_selected(:birth_datetime, year: 2009, month: 'February', day: 29, hour: 12, min: 13, sec: 14)
39
39
  end
40
40
 
41
41
  it "should use attribute values from object if no params" do
42
42
  person.birth_datetime = "2009-01-02 12:13:14"
43
- @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
44
- should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'January', :day => 2, :hour => 12, :min => 13, :sec => 14)
43
+ @output = datetime_select(:person, :birth_datetime, include_blank: true, include_seconds: true)
44
+ should_have_datetime_selected(:birth_datetime, year: 2009, month: 'January', day: 2, hour: 12, min: 13, sec: 14)
45
45
  end
46
46
 
47
47
  it "should use attribute values if params does not contain attribute params" do
48
48
  person.birth_datetime = "2009-01-02 12:13:14"
49
49
  @params["person"] = { }
50
- @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
51
- should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'January', :day => 2, :hour => 12, :min => 13, :sec => 14)
50
+ @output = datetime_select(:person, :birth_datetime, include_blank: true, include_seconds: true)
51
+ should_have_datetime_selected(:birth_datetime, year: 2009, month: 'January', day: 2, hour: 12, min: 13, sec: 14)
52
52
  end
53
53
 
54
54
  it "should not select values when attribute value is nil and has no param values" do
55
55
  person.birth_datetime = nil
56
- @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
56
+ @output = datetime_select(:person, :birth_datetime, include_blank: true, include_seconds: true)
57
57
  should_not_have_datetime_selected(:birth_datetime, :year, :month, :day, :hour, :min, :sec)
58
58
  end
59
59
  end
@@ -66,8 +66,8 @@ RSpec.describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
66
66
  "birth_date(3i)" => '29',
67
67
  }
68
68
  person.birth_date = nil
69
- @output = date_select(:person, :birth_date, :include_blank => true)
70
- should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29)
69
+ @output = date_select(:person, :birth_date, include_blank: true)
70
+ should_have_datetime_selected(:birth_date, year: 2009, month: 'February', day: 29)
71
71
  end
72
72
 
73
73
  it "should override object values and use params if present" do
@@ -77,26 +77,26 @@ RSpec.describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
77
77
  "birth_date(3i)" => '29',
78
78
  }
79
79
  person.birth_date = "2009-03-01"
80
- @output = date_select(:person, :birth_date, :include_blank => true)
81
- should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29)
80
+ @output = date_select(:person, :birth_date, include_blank: true)
81
+ should_have_datetime_selected(:birth_date, year: 2009, month: 'February', day: 29)
82
82
  end
83
83
 
84
84
  it "should select attribute values from object if no params" do
85
85
  person.birth_date = "2009-01-02"
86
- @output = date_select(:person, :birth_date, :include_blank => true)
87
- should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2)
86
+ @output = date_select(:person, :birth_date, include_blank: true)
87
+ should_have_datetime_selected(:birth_date, year: 2009, month: 'January', day: 2)
88
88
  end
89
89
 
90
90
  it "should select attribute values if params does not contain attribute params" do
91
91
  person.birth_date = "2009-01-02"
92
92
  @params["person"] = { }
93
- @output = date_select(:person, :birth_date, :include_blank => true)
94
- should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2)
93
+ @output = date_select(:person, :birth_date, include_blank: true)
94
+ should_have_datetime_selected(:birth_date, year: 2009, month: 'January', day: 2)
95
95
  end
96
96
 
97
97
  it "should not select values when attribute value is nil and has no param values" do
98
98
  person.birth_date = nil
99
- @output = date_select(:person, :birth_date, :include_blank => true)
99
+ @output = date_select(:person, :birth_date, include_blank: true)
100
100
  should_not_have_datetime_selected(:birth_time, :year, :month, :day)
101
101
  end
102
102
 
@@ -106,8 +106,8 @@ RSpec.describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
106
106
  "birth_date(2i)" => '2',
107
107
  }
108
108
 
109
- @output = date_select(:person, :birth_date, :include_blank => true, :discard_day => true)
110
- should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February')
109
+ @output = date_select(:person, :birth_date, include_blank: true, discard_day: true)
110
+ should_have_datetime_selected(:birth_date, year: 2009, month: 'February')
111
111
  should_not_have_datetime_selected(:birth_time, :day)
112
112
  expect(@output).to have_tag("input[id=person_birth_date_3i][type=hidden][value='1']")
113
113
  end
@@ -128,33 +128,33 @@ RSpec.describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
128
128
  "birth_time(6i)" => '14',
129
129
  }
130
130
  person.birth_time = nil
131
- @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
132
- should_have_datetime_selected(:birth_time, :hour => 12, :min => 13, :sec => 14)
131
+ @output = time_select(:person, :birth_time, include_blank: true, include_seconds: true)
132
+ should_have_datetime_selected(:birth_time, hour: 12, min: 13, sec: 14)
133
133
  end
134
134
 
135
135
  it "should select attribute values from object if no params" do
136
136
  person.birth_time = "2000-01-01 12:13:14"
137
- @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
138
- should_have_datetime_selected(:birth_time, :hour => 12, :min => 13, :sec => 14)
137
+ @output = time_select(:person, :birth_time, include_blank: true, include_seconds: true)
138
+ should_have_datetime_selected(:birth_time, hour: 12, min: 13, sec: 14)
139
139
  end
140
140
 
141
141
  it "should not select values when attribute value is nil and has no param values" do
142
142
  person.birth_time = nil
143
- @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
143
+ @output = time_select(:person, :birth_time, include_blank: true, include_seconds: true)
144
144
  should_not_have_datetime_selected(:birth_time, :hour, :min, :sec)
145
145
  end
146
146
  end
147
147
 
148
148
  def should_have_datetime_selected(field, datetime_hash)
149
149
  datetime_hash.each do |key, value|
150
- index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[key]
150
+ index = {year: 1, month: 2, day: 3, hour: 4, min: 5, sec: 6}[key]
151
151
  expect(@output).to have_tag("select[id=person_#{field}_#{index}i] option[selected=selected]", value.to_s)
152
152
  end
153
153
  end
154
154
 
155
155
  def should_not_have_datetime_selected(field, *attributes)
156
156
  attributes.each do |attribute|
157
- index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[attribute]
157
+ index = {year: 1, month: 2, day: 3, hour: 4, min: 5, sec: 6}[attribute]
158
158
  expect(@output).not_to have_tag("select[id=person_#{attribute}_#{index}i] option[selected=selected]")
159
159
  end
160
160
  end
@@ -1,36 +1,36 @@
1
1
  RSpec.describe 'ValidatesTimeliness::Extensions::MultiparameterHandler' do
2
2
 
3
3
  context "time column" do
4
- it 'should assign a string value for invalid date portion' do
4
+ it 'should be nil invalid date portion' do
5
5
  employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0])
6
- expect(employee.birth_datetime_before_type_cast).to eq '2000-02-31 12:00:00'
6
+ expect(employee.birth_datetime).to be_nil
7
7
  end
8
8
 
9
9
  it 'should assign a Time value for valid datetimes' do
10
10
  employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
11
- expect(employee.birth_datetime_before_type_cast).to eq Time.zone.local(2000, 2, 28, 12, 0, 0)
11
+ expect(employee.birth_datetime).to eq Time.zone.local(2000, 2, 28, 12, 0, 0)
12
12
  end
13
13
 
14
- it 'should assign a string value for incomplete time' do
14
+ it 'should be nil for incomplete date portion' do
15
15
  employee = record_with_multiparameter_attribute(:birth_datetime, [2000, nil, nil])
16
- expect(employee.birth_datetime_before_type_cast).to eq '2000-00-00'
16
+ expect(employee.birth_datetime).to be_nil
17
17
  end
18
18
  end
19
19
 
20
20
  context "date column" do
21
- it 'should assign a string value for invalid date' do
21
+ it 'should assign nil for invalid date' do
22
22
  employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 31])
23
- expect(employee.birth_date_before_type_cast).to eq '2000-02-31'
23
+ expect(employee.birth_date).to be_nil
24
24
  end
25
25
 
26
26
  it 'should assign a Date value for valid date' do
27
27
  employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 28])
28
- expect(employee.birth_date_before_type_cast).to eq Date.new(2000, 2, 28)
28
+ expect(employee.birth_date).to eq Date.new(2000, 2, 28)
29
29
  end
30
30
 
31
- it 'should assign a string value for incomplete date' do
31
+ it 'should assign hash values for incomplete date' do
32
32
  employee = record_with_multiparameter_attribute(:birth_date, [2000, nil, nil])
33
- expect(employee.birth_date_before_type_cast).to eq '2000-00-00'
33
+ expect(employee.birth_date).to be_nil
34
34
  end
35
35
  end
36
36
 
@@ -1,5 +1,4 @@
1
1
  RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
2
-
3
2
  context "validation methods" do
4
3
  let(:record) { Employee.new }
5
4
 
@@ -26,6 +25,7 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
26
25
  record.birth_date = 'not a date'
27
26
 
28
27
  record.valid?
28
+ expect(record.birth_date_before_type_cast).to eq 'not a date'
29
29
  expect(record.errors[:birth_date]).not_to be_empty
30
30
  end
31
31
 
@@ -37,10 +37,6 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
37
37
  end
38
38
  end
39
39
 
40
- it 'should determine type for attribute' do
41
- expect(Employee.timeliness_attribute_type(:birth_date)).to eq :date
42
- end
43
-
44
40
  context 'attribute timezone awareness' do
45
41
  let(:klass) {
46
42
  Class.new(ActiveRecord::Base) do
@@ -53,22 +49,6 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
53
49
  validates_datetime :some_datetime
54
50
  end
55
51
  }
56
-
57
- context 'for column attribute' do
58
- it 'should be detected from column type' do
59
- expect(klass.timeliness_attribute_timezone_aware?(:birth_date)).to be_falsey
60
- expect(klass.timeliness_attribute_timezone_aware?(:birth_time)).to be_falsey
61
- expect(klass.timeliness_attribute_timezone_aware?(:birth_datetime)).to be_truthy
62
- end
63
- end
64
-
65
- context 'for non-column attribute' do
66
- it 'should be detected from the validation type' do
67
- expect(klass.timeliness_attribute_timezone_aware?(:some_date)).to be_falsey
68
- expect(klass.timeliness_attribute_timezone_aware?(:some_time)).to be_falsey
69
- expect(klass.timeliness_attribute_timezone_aware?(:some_datetime)).to be_truthy
70
- end
71
- end
72
52
  end
73
53
 
74
54
  context "attribute write method" do
@@ -79,34 +59,6 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
79
59
  validates_datetime :birth_datetime, :allow_blank => true
80
60
  end
81
61
 
82
- context 'value cache' do
83
- let(:record) { EmployeeWithCache.new }
84
-
85
- context 'for datetime column' do
86
- it 'should store raw value' do
87
- record.birth_datetime = datetime_string = '2010-01-01 12:30'
88
-
89
- expect(record.read_timeliness_attribute_before_type_cast('birth_datetime')).to eq datetime_string
90
- end
91
- end
92
-
93
- context 'for date column' do
94
- it 'should store raw value' do
95
- record.birth_date = date_string = '2010-01-01'
96
-
97
- expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to eq date_string
98
- end
99
- end
100
-
101
- context 'for time column' do
102
- it 'should store raw value' do
103
- record.birth_time = time_string = '12:12'
104
-
105
- expect(record.read_timeliness_attribute_before_type_cast('birth_time')).to eq time_string
106
- end
107
- end
108
- end
109
-
110
62
  context "with plugin parser" do
111
63
  with_config(:use_plugin_parser, true)
112
64
  let(:record) { EmployeeWithParser.new }
@@ -118,17 +70,23 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
118
70
  validates_datetime :birth_datetime, :allow_blank => true
119
71
  end
120
72
 
73
+ before do
74
+ allow(Timeliness::Parser).to receive(:parse).and_call_original
75
+ end
76
+
121
77
  context "for a date column" do
122
78
  it 'should parse a string value' do
123
- expect(Timeliness::Parser).to receive(:parse)
124
-
125
79
  record.birth_date = '2010-01-01'
80
+ expect(record.birth_date).to eq(Date.new(2010, 1, 1))
81
+
82
+ expect(Timeliness::Parser).to have_received(:parse)
126
83
  end
127
84
 
128
85
  it 'should parse a invalid string value as nil' do
129
- expect(Timeliness::Parser).to receive(:parse)
130
-
131
86
  record.birth_date = 'not valid'
87
+ expect(record.birth_date).to be_nil
88
+
89
+ expect(Timeliness::Parser).to have_received(:parse)
132
90
  end
133
91
 
134
92
  it 'should store a Date value after parsing string' do
@@ -140,45 +98,85 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
140
98
  end
141
99
 
142
100
  context "for a time column" do
143
- it 'should parse a string value' do
144
- expect(Timeliness::Parser).to receive(:parse)
145
-
146
- record.birth_time = '12:30'
101
+ around do |example|
102
+ time_zone_aware_types = ActiveRecord::Base.time_zone_aware_types.dup
103
+ example.call
104
+ ActiveRecord::Base.time_zone_aware_types = time_zone_aware_types
147
105
  end
148
106
 
149
- it 'should parse a invalid string value as nil' do
150
- expect(Timeliness::Parser).to receive(:parse)
107
+ context 'timezone aware' do
108
+ with_config(:default_timezone, 'Australia/Melbourne')
109
+
110
+ before do
111
+ unless ActiveRecord::Base.time_zone_aware_types.include?(:time)
112
+ ActiveRecord::Base.time_zone_aware_types.push(:time)
113
+ end
114
+ end
115
+
116
+ it 'should parse a string value' do
117
+ record.birth_time = '12:30'
118
+
119
+ expect(record.birth_time).to eq('12:30'.in_time_zone)
120
+ expect(Timeliness::Parser).to have_received(:parse)
121
+ end
122
+
123
+ it 'should parse a invalid string value as nil' do
124
+ record.birth_time = 'not valid'
151
125
 
152
- record.birth_time = 'not valid'
126
+ expect(record.birth_time).to be_nil
127
+ expect(Timeliness::Parser).to have_received(:parse)
128
+ end
129
+
130
+ it 'should store a Time value after parsing string' do
131
+ record.birth_time = '12:30'
132
+
133
+ expect(record.birth_time).to eq('12:30'.in_time_zone)
134
+ expect(record.birth_time.utc_offset).to eq '12:30'.in_time_zone.utc_offset
135
+ end
153
136
  end
154
137
 
155
- it 'should store a Time value after parsing string' do
156
- record.birth_time = '12:30'
138
+ skip 'not timezone aware' do
139
+ before do
140
+ ActiveRecord::Base.time_zone_aware_types.delete(:time)
141
+ end
142
+
143
+ it 'should parse a string value' do
144
+ record.birth_time = '12:30'
145
+
146
+ expect(record.birth_time).to eq(Time.utc(2000,1,1,12,30))
147
+ expect(Timeliness::Parser).to have_received(:parse)
148
+ end
149
+
150
+ it 'should parse a invalid string value as nil' do
151
+ record.birth_time = 'not valid'
157
152
 
158
- expect(record.birth_time).to be_kind_of(Time)
159
- expect(record.birth_time).to eq Time.utc(2000, 1, 1, 12, 30)
153
+ expect(record.birth_time).to be_nil
154
+ expect(Timeliness::Parser).to have_received(:parse)
155
+ end
156
+
157
+ it 'should store a Time value in utc' do
158
+ record.birth_time = '12:30'
159
+
160
+ expect(record.birth_time.utc_offset).to eq Time.now.utc.utc_offset
161
+ end
160
162
  end
161
163
  end
162
164
 
163
165
  context "for a datetime column" do
164
166
  with_config(:default_timezone, 'Australia/Melbourne')
165
167
 
166
- it 'should parse a string value' do
167
- expect(Timeliness::Parser).to receive(:parse)
168
-
168
+ it 'should parse a string value into Time value' do
169
169
  record.birth_datetime = '2010-01-01 12:00'
170
+
171
+ expect(record.birth_datetime).to eq Time.zone.local(2010,1,1,12,00)
172
+ expect(Timeliness::Parser).to have_received(:parse)
170
173
  end
171
174
 
172
175
  it 'should parse a invalid string value as nil' do
173
- expect(Timeliness::Parser).to receive(:parse)
174
-
175
176
  record.birth_datetime = 'not valid'
176
- end
177
-
178
- it 'should parse string into Time value' do
179
- record.birth_datetime = '2010-01-01 12:00'
180
177
 
181
- expect(record.birth_datetime).to be_kind_of(Time)
178
+ expect(record.birth_datetime).to be_nil
179
+ expect(Timeliness::Parser).to have_received(:parse)
182
180
  end
183
181
 
184
182
  it 'should parse string as current timezone' do
@@ -189,66 +187,4 @@ RSpec.describe ValidatesTimeliness, 'ActiveRecord' do
189
187
  end
190
188
  end
191
189
  end
192
-
193
- context "reload" do
194
- it 'should clear cache value' do
195
- record = Employee.create!
196
- record.birth_date = '2010-01-01'
197
-
198
- record.reload
199
-
200
- expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to be_nil
201
- end
202
- end
203
-
204
- context "before_type_cast method" do
205
- let(:record) { Employee.new }
206
-
207
- it 'should be defined on class if ORM supports it' do
208
- expect(record).to respond_to(:birth_datetime_before_type_cast)
209
- end
210
-
211
- it 'should return original value' do
212
- record.birth_datetime = date_string = '2010-01-01'
213
-
214
- expect(record.birth_datetime_before_type_cast).to eq date_string
215
- end
216
-
217
- it 'should return attribute if no attribute assignment has been made' do
218
- datetime = Time.zone.local(2010,01,01)
219
- Employee.create(:birth_datetime => datetime)
220
-
221
- record = Employee.last
222
- expect(record.birth_datetime_before_type_cast).to match(/#{datetime.utc.to_s[0...-4]}/)
223
- end
224
-
225
- context "with plugin parser" do
226
- with_config(:use_plugin_parser, true)
227
-
228
- it 'should return original value' do
229
- record.birth_datetime = date_string = '2010-01-31'
230
-
231
- expect(record.birth_datetime_before_type_cast).to eq date_string
232
- end
233
- end
234
-
235
- end
236
-
237
- context "define_attribute_methods" do
238
- it "returns a falsy value if the attribute methods have already been generated" do
239
- expect(Employee.define_attribute_methods).to be_falsey
240
- end
241
- end
242
-
243
- context "undefine_attribute_methods" do
244
- it "returns remove attribute methods that have already been generated" do
245
- Employee.define_attribute_methods
246
-
247
- expect(Employee.instance_methods).to include(:birth_datetime)
248
-
249
- Employee.undefine_attribute_methods
250
-
251
- expect(Employee.instance_methods).to_not include(:birth_datetime)
252
- end
253
- end
254
190
  end