validates_timeliness 3.0.8 → 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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +21 -0
  3. data/CHANGELOG.rdoc +21 -1
  4. data/README.rdoc +20 -22
  5. data/Rakefile +5 -1
  6. data/gemfiles/rails_4_0.gemfile +19 -0
  7. data/gemfiles/rails_4_1.gemfile +19 -0
  8. data/gemfiles/rails_4_2.gemfile +19 -0
  9. data/lib/generators/validates_timeliness/install_generator.rb +0 -1
  10. data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +3 -3
  11. data/lib/validates_timeliness/attribute_methods.rb +42 -26
  12. data/lib/validates_timeliness/extensions/date_time_select.rb +2 -9
  13. data/lib/validates_timeliness/extensions/multiparameter_handler.rb +59 -54
  14. data/lib/validates_timeliness/extensions.rb +3 -4
  15. data/lib/validates_timeliness/helper_methods.rb +7 -6
  16. data/lib/validates_timeliness/orm/active_record.rb +70 -15
  17. data/lib/validates_timeliness/railtie.rb +2 -1
  18. data/lib/validates_timeliness/validator.rb +29 -4
  19. data/lib/validates_timeliness/version.rb +1 -1
  20. data/lib/validates_timeliness.rb +13 -13
  21. data/spec/spec_helper.rb +10 -5
  22. data/spec/support/config_helper.rb +10 -0
  23. data/spec/support/model_helpers.rb +4 -4
  24. data/spec/support/tag_matcher.rb +35 -0
  25. data/spec/support/test_model.rb +0 -1
  26. data/spec/validates_timeliness/attribute_methods_spec.rb +22 -7
  27. data/spec/validates_timeliness/conversion_spec.rb +41 -41
  28. data/spec/validates_timeliness/extensions/date_time_select_spec.rb +5 -4
  29. data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +28 -16
  30. data/spec/validates_timeliness/helper_methods_spec.rb +12 -12
  31. data/spec/validates_timeliness/orm/active_record_spec.rb +178 -44
  32. data/spec/validates_timeliness/validator/after_spec.rb +3 -3
  33. data/spec/validates_timeliness/validator/before_spec.rb +3 -3
  34. data/spec/validates_timeliness/validator/is_at_spec.rb +4 -4
  35. data/spec/validates_timeliness/validator/on_or_after_spec.rb +3 -3
  36. data/spec/validates_timeliness/validator/on_or_before_spec.rb +3 -3
  37. data/spec/validates_timeliness/validator_spec.rb +66 -25
  38. data/spec/validates_timeliness_spec.rb +8 -8
  39. data/validates_timeliness.gemspec +2 -2
  40. metadata +41 -60
  41. data/lib/validates_timeliness/orm/mongoid.rb +0 -54
  42. data/spec/validates_timeliness/orm/mongoid_spec.rb +0 -113
@@ -12,68 +12,68 @@ describe ValidatesTimeliness::Conversion do
12
12
  describe "#type_cast_value" do
13
13
  describe "for date type" do
14
14
  it "should return same value for date value" do
15
- type_cast_value(Date.new(2010, 1, 1), :date).should == Date.new(2010, 1, 1)
15
+ expect(type_cast_value(Date.new(2010, 1, 1), :date)).to eq(Date.new(2010, 1, 1))
16
16
  end
17
17
 
18
18
  it "should return date part of time value" do
19
- type_cast_value(Time.mktime(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
19
+ expect(type_cast_value(Time.mktime(2010, 1, 1, 0, 0, 0), :date)).to eq(Date.new(2010, 1, 1))
20
20
  end
21
21
 
22
22
  it "should return date part of datetime value" do
23
- type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
23
+ expect(type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date)).to eq(Date.new(2010, 1, 1))
24
24
  end
25
25
 
26
26
  it 'should return nil for invalid value types' do
27
- type_cast_value(12, :date).should == nil
27
+ expect(type_cast_value(12, :date)).to eq(nil)
28
28
  end
29
29
  end
30
30
 
31
31
  describe "for time type" do
32
32
  it "should return same value for time value matching dummy date part" do
33
- type_cast_value(Time.utc(2000, 1, 1, 0, 0, 0), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
33
+ expect(type_cast_value(Time.utc(2000, 1, 1, 0, 0, 0), :time)).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
34
34
  end
35
35
 
36
36
  it "should return dummy time value with same time part for time value with different date" do
37
- type_cast_value(Time.utc(2010, 1, 1, 0, 0, 0), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
37
+ expect(type_cast_value(Time.utc(2010, 1, 1, 0, 0, 0), :time)).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
38
38
  end
39
39
 
40
40
  it "should return dummy time only for date value" do
41
- type_cast_value(Date.new(2010, 1, 1), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
41
+ expect(type_cast_value(Date.new(2010, 1, 1), :time)).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
42
42
  end
43
43
 
44
44
  it "should return dummy date with time part for datetime value" do
45
- type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :time).should == Time.utc(2000, 1, 1, 12, 34, 56)
45
+ expect(type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :time)).to eq(Time.utc(2000, 1, 1, 12, 34, 56))
46
46
  end
47
47
 
48
48
  it 'should return nil for invalid value types' do
49
- type_cast_value(12, :time).should == nil
49
+ expect(type_cast_value(12, :time)).to eq(nil)
50
50
  end
51
51
  end
52
52
 
53
53
  describe "for datetime type" do
54
54
  it "should return Date as Time value" do
55
- type_cast_value(Date.new(2010, 1, 1), :datetime).should == Time.local_time(2010, 1, 1, 0, 0, 0)
55
+ expect(type_cast_value(Date.new(2010, 1, 1), :datetime)).to eq(Time.local(2010, 1, 1, 0, 0, 0))
56
56
  end
57
57
 
58
58
  it "should return same Time value" do
59
59
  value = Time.utc(2010, 1, 1, 12, 34, 56)
60
- type_cast_value(Time.utc(2010, 1, 1, 12, 34, 56), :datetime).should == value
60
+ expect(type_cast_value(Time.utc(2010, 1, 1, 12, 34, 56), :datetime)).to eq(value)
61
61
  end
62
62
 
63
63
  it "should return as Time with same component values" do
64
- type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
64
+ expect(type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime)).to eq(Time.utc(2010, 1, 1, 12, 34, 56))
65
65
  end
66
66
 
67
67
  it "should return same Time in correct zone if timezone aware" do
68
68
  @timezone_aware = true
69
69
  value = Time.utc(2010, 1, 1, 12, 34, 56)
70
70
  result = type_cast_value(value, :datetime)
71
- result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
72
- result.zone.should == 'EST'
71
+ expect(result).to eq(Time.zone.local(2010, 1, 1, 23, 34, 56))
72
+ expect(result.zone).to eq('AEDT')
73
73
  end
74
74
 
75
75
  it 'should return nil for invalid value types' do
76
- type_cast_value(12, :datetime).should == nil
76
+ expect(type_cast_value(12, :datetime)).to eq(nil)
77
77
  end
78
78
  end
79
79
 
@@ -82,41 +82,41 @@ describe ValidatesTimeliness::Conversion do
82
82
 
83
83
  it "should ignore usec on time values when evaluated" do
84
84
  value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
85
- type_cast_value(value, :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
85
+ expect(type_cast_value(value, :datetime)).to eq(Time.utc(2010, 1, 1, 12, 34, 56))
86
86
  end
87
87
 
88
88
  it "should ignore usec and return time in correct zone if timezone aware" do
89
89
  @timezone_aware = true
90
90
  value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
91
91
  result = type_cast_value(value, :datetime)
92
- result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
93
- result.zone.should == 'EST'
92
+ expect(result).to eq(Time.zone.local(2010, 1, 1, 23, 34, 56))
93
+ expect(result.zone).to eq('AEDT')
94
94
  end
95
95
  end
96
96
  end
97
97
 
98
98
  describe "#dummy_time" do
99
99
  it 'should return Time with dummy date values but same time components' do
100
- dummy_time(Time.utc(2010, 11, 22, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
100
+ expect(dummy_time(Time.utc(2010, 11, 22, 12, 34, 56))).to eq(Time.utc(2000, 1, 1, 12, 34, 56))
101
101
  end
102
102
 
103
103
  it 'should return same value for Time which already has dummy date values' do
104
- dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
104
+ expect(dummy_time(Time.utc(2000, 1, 1, 12, 34, 56))).to eq(Time.utc(2000, 1, 1, 12, 34, 56))
105
105
  end
106
106
 
107
107
  it 'should return time component values shifted to current zone if timezone aware' do
108
108
  @timezone_aware = true
109
- dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.zone.local(2000, 1, 1, 23, 34, 56)
109
+ expect(dummy_time(Time.utc(2000, 1, 1, 12, 34, 56))).to eq(Time.zone.local(2000, 1, 1, 23, 34, 56))
110
110
  end
111
111
 
112
112
  it 'should return base dummy time value for Date value' do
113
- dummy_time(Date.new(2010, 11, 22)).should == Time.utc(2000, 1, 1, 0, 0, 0)
113
+ expect(dummy_time(Date.new(2010, 11, 22))).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
114
114
  end
115
115
 
116
116
  describe "with custom dummy date" do
117
117
  it 'should return dummy time with custom dummy date' do
118
118
  with_config(:dummy_date_for_time_type, [2010, 1, 1] ) do
119
- dummy_time(Time.utc(1999, 11, 22, 12, 34, 56)).should == Time.utc(2010, 1, 1, 12, 34, 56)
119
+ expect(dummy_time(Time.utc(1999, 11, 22, 12, 34, 56))).to eq(Time.utc(2010, 1, 1, 12, 34, 56))
120
120
  end
121
121
  end
122
122
  end
@@ -127,56 +127,56 @@ describe ValidatesTimeliness::Conversion do
127
127
 
128
128
  it 'should return Date object as is' do
129
129
  value = Date.new(2010,1,1)
130
- evaluate_option_value(value, person).should == value
130
+ expect(evaluate_option_value(value, person)).to eq(value)
131
131
  end
132
132
 
133
133
  it 'should return Time object as is' do
134
134
  value = Time.mktime(2010,1,1)
135
- evaluate_option_value(value, person).should == value
135
+ expect(evaluate_option_value(value, person)).to eq(value)
136
136
  end
137
137
 
138
138
  it 'should return DateTime object as is' do
139
139
  value = DateTime.new(2010,1,1,0,0,0)
140
- evaluate_option_value(value, person).should == value
140
+ expect(evaluate_option_value(value, person)).to eq(value)
141
141
  end
142
142
 
143
143
  it 'should return Time value returned from proc with 0 arity' do
144
144
  value = Time.mktime(2010,1,1)
145
- evaluate_option_value(lambda { value }, person).should == value
145
+ expect(evaluate_option_value(lambda { value }, person)).to eq(value)
146
146
  end
147
147
 
148
148
  it 'should return Time value returned by record attribute call in proc arity of 1' do
149
149
  value = Time.mktime(2010,1,1)
150
150
  person.birth_time = value
151
- evaluate_option_value(lambda {|r| r.birth_time }, person).should == value
151
+ expect(evaluate_option_value(lambda {|r| r.birth_time }, person)).to eq(value)
152
152
  end
153
153
 
154
154
  it 'should return Time value for attribute method symbol which returns Time' do
155
155
  value = Time.mktime(2010,1,1)
156
156
  person.birth_time = value
157
- evaluate_option_value(:birth_time, person).should == value
157
+ expect(evaluate_option_value(:birth_time, person)).to eq(value)
158
158
  end
159
159
 
160
160
  it 'should return Time value is default zone from string time value' do
161
161
  value = '2010-01-01 12:00:00'
162
- evaluate_option_value(value, person).should == Time.utc(2010,1,1,12,0,0)
162
+ expect(evaluate_option_value(value, person)).to eq(Time.utc(2010,1,1,12,0,0))
163
163
  end
164
164
 
165
165
  it 'should return Time value is current zone from string time value if timezone aware' do
166
166
  @timezone_aware = true
167
167
  value = '2010-01-01 12:00:00'
168
- evaluate_option_value(value, person).should == Time.zone.local(2010,1,1,12,0,0)
168
+ expect(evaluate_option_value(value, person)).to eq(Time.zone.local(2010,1,1,12,0,0))
169
169
  end
170
170
 
171
171
  it 'should return Time value in default zone from proc which returns string time' do
172
172
  value = '2010-01-01 12:00:00'
173
- evaluate_option_value(lambda { value }, person).should == Time.utc(2010,1,1,12,0,0)
173
+ expect(evaluate_option_value(lambda { value }, person)).to eq(Time.utc(2010,1,1,12,0,0))
174
174
  end
175
175
 
176
176
  it 'should return Time value for attribute method symbol which returns string time value' do
177
177
  value = '2010-01-01 12:00:00'
178
178
  person.birth_time = value
179
- evaluate_option_value(:birth_time, person).should == Time.utc(2010,1,1,12,0,0)
179
+ expect(evaluate_option_value(:birth_time, person)).to eq(Time.zone.local(2010,1,1,12,0,0))
180
180
  end
181
181
 
182
182
  context "restriction shorthand" do
@@ -185,17 +185,17 @@ describe ValidatesTimeliness::Conversion do
185
185
  end
186
186
 
187
187
  it 'should evaluate :now as current time' do
188
- evaluate_option_value(:now, person).should == Time.now
188
+ expect(evaluate_option_value(:now, person)).to eq(Time.now)
189
189
  end
190
190
 
191
191
  it 'should evaluate :today as current time' do
192
- evaluate_option_value(:today, person).should == Date.today
192
+ expect(evaluate_option_value(:today, person)).to eq(Date.today)
193
193
  end
194
194
 
195
195
  it 'should not use shorthand if symbol if is record method' do
196
196
  time = 1.day.from_now
197
- person.stub!(:now).and_return(time)
198
- evaluate_option_value(:now, person).should == time
197
+ allow(person).to receive(:now).and_return(time)
198
+ expect(evaluate_option_value(:now, person)).to eq(time)
199
199
  end
200
200
  end
201
201
  end
@@ -205,7 +205,7 @@ describe ValidatesTimeliness::Conversion do
205
205
  with_config(:use_plugin_parser, true)
206
206
 
207
207
  it 'should use timeliness' do
208
- Timeliness::Parser.should_receive(:parse)
208
+ expect(Timeliness::Parser).to receive(:parse)
209
209
  parse('2000-01-01')
210
210
  end
211
211
  end
@@ -215,20 +215,20 @@ describe ValidatesTimeliness::Conversion do
215
215
 
216
216
  it 'should use Time.zone.parse attribute is timezone aware' do
217
217
  @timezone_aware = true
218
- Time.zone.should_receive(:parse)
218
+ expect(Time.zone).to receive(:parse)
219
219
  parse('2000-01-01')
220
220
  end
221
221
 
222
222
  it 'should use value#to_time if use_plugin_parser setting is false and attribute is not timezone aware' do
223
223
  @timezone_aware = false
224
224
  value = '2000-01-01'
225
- value.should_receive(:to_time)
225
+ expect(value).to receive(:to_time)
226
226
  parse(value)
227
227
  end
228
228
  end
229
229
 
230
230
  it 'should return nil if value is nil' do
231
- parse(nil).should be_nil
231
+ expect(parse(nil)).to be_nil
232
232
  end
233
233
  end
234
234
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ValidatesTimeliness::Extensions::DateTimeSelect do
3
+ describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
4
4
  include ActionView::Helpers::DateHelper
5
5
  attr_reader :person, :params
6
6
 
@@ -111,7 +111,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
111
111
  @output = date_select(:person, :birth_date, :include_blank => true, :discard_day => true)
112
112
  should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February')
113
113
  should_not_have_datetime_selected(:birth_time, :day)
114
- @output.should have_tag("input[id=person_birth_date_3i][type=hidden][value='1']")
114
+ expect(@output).to have_tag("input[id=person_birth_date_3i][type=hidden][value='1']")
115
115
  end
116
116
  end
117
117
 
@@ -150,14 +150,15 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
150
150
  def should_have_datetime_selected(field, datetime_hash)
151
151
  datetime_hash.each do |key, value|
152
152
  index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[key]
153
- @output.should have_tag("select[id=person_#{field}_#{index}i] option[selected=selected]", value.to_s)
153
+ expect(@output).to have_tag("select[id=person_#{field}_#{index}i] option[selected=selected]", value.to_s)
154
154
  end
155
155
  end
156
156
 
157
157
  def should_not_have_datetime_selected(field, *attributes)
158
158
  attributes.each do |attribute|
159
159
  index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[attribute]
160
- @output.should_not have_tag("select[id=person_#{attribute}_#{index}i] option[selected=selected]")
160
+ expect(@output).not_to have_tag("select[id=person_#{attribute}_#{index}i] option[selected=selected]")
161
161
  end
162
162
  end
163
+
163
164
  end
@@ -1,33 +1,45 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ValidatesTimeliness::Extensions::MultiparameterHandler do
4
- let(:employee) { Employee.new }
3
+ describe 'ValidatesTimeliness::Extensions::MultiparameterHandler' do
5
4
 
6
5
  context "time column" do
7
- it 'should return string value for invalid date portion' do
8
- multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0])
9
- employee.birth_datetime_before_type_cast.should == '2000-02-31 12:00:00'
6
+ it 'should assign a string value for invalid date portion' do
7
+ employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0])
8
+ expect(employee.birth_datetime_before_type_cast).to eq '2000-02-31 12:00:00'
10
9
  end
11
10
 
12
- it 'should return Time value for valid datetimes' do
13
- multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
14
- employee.birth_datetime_before_type_cast.should be_kind_of(Time)
11
+ it 'should assign a Time value for valid datetimes' do
12
+ employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
13
+ expect(employee.birth_datetime_before_type_cast).to eq Time.zone.local(2000, 2, 28, 12, 0, 0)
14
+ end
15
+
16
+ it 'should assign a string value for incomplete time' do
17
+ employee = record_with_multiparameter_attribute(:birth_datetime, [2000, nil, nil])
18
+ expect(employee.birth_datetime_before_type_cast).to eq '2000-00-00'
15
19
  end
16
20
  end
17
21
 
18
22
  context "date column" do
19
- it 'should return string value for invalid date' do
20
- multiparameter_attribute(:birth_date, [2000, 2, 31])
21
- employee.birth_date_before_type_cast.should == '2000-02-31'
23
+ it 'should assign a string value for invalid date' do
24
+ employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 31])
25
+ expect(employee.birth_date_before_type_cast).to eq '2000-02-31'
22
26
  end
23
27
 
24
- it 'should return Date value for valid date' do
25
- multiparameter_attribute(:birth_date, [2000, 2, 28])
26
- employee.birth_date_before_type_cast.should be_kind_of(Date)
28
+ it 'should assign a Date value for valid date' do
29
+ employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 28])
30
+ expect(employee.birth_date_before_type_cast).to eq Date.new(2000, 2, 28)
31
+ end
32
+
33
+ it 'should assign a string value for incomplete date' do
34
+ employee = record_with_multiparameter_attribute(:birth_date, [2000, nil, nil])
35
+ expect(employee.birth_date_before_type_cast).to eq '2000-00-00'
27
36
  end
28
37
  end
29
38
 
30
- def multiparameter_attribute(name, values)
31
- employee.send(:execute_callstack_for_multiparameter_attributes, name.to_s => values)
39
+ def record_with_multiparameter_attribute(name, values)
40
+ hash = {}
41
+ values.each_with_index {|value, index| hash["#{name}(#{index+1}i)"] = value.to_s }
42
+ Employee.new(hash)
32
43
  end
44
+
33
45
  end
@@ -1,30 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ValidatesTimeliness, 'HelperMethods' do
4
+ let(:record) { Person.new }
5
+
4
6
  it 'should define class validation methods' do
5
- Person.should respond_to(:validates_date)
6
- Person.should respond_to(:validates_time)
7
- Person.should respond_to(:validates_datetime)
7
+ expect(Person).to respond_to(:validates_date)
8
+ expect(Person).to respond_to(:validates_time)
9
+ expect(Person).to respond_to(:validates_datetime)
8
10
  end
9
11
 
10
12
  it 'should define instance validation methods' do
11
- Person.new.should respond_to(:validates_date)
12
- Person.new.should respond_to(:validates_time)
13
- Person.new.should respond_to(:validates_datetime)
13
+ expect(record).to respond_to(:validates_date)
14
+ expect(record).to respond_to(:validates_time)
15
+ expect(record).to respond_to(:validates_datetime)
14
16
  end
15
17
 
16
18
  it 'should validate instance using class validation defined' do
17
19
  Person.validates_date :birth_date
18
- r = Person.new
19
- r.valid?
20
+ record.valid?
20
21
 
21
- r.errors[:birth_date].should_not be_empty
22
+ expect(record.errors[:birth_date]).not_to be_empty
22
23
  end
23
24
 
24
25
  it 'should validate instance using instance valiation method' do
25
- r = Person.new
26
- r.validates_date :birth_date
26
+ record.validates_date :birth_date
27
27
 
28
- r.errors[:birth_date].should_not be_empty
28
+ expect(record.errors[:birth_date]).not_to be_empty
29
29
  end
30
30
  end