sp-validates_timeliness 3.1.2
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 +7 -0
- data/CHANGELOG.rdoc +183 -0
- data/LICENSE +20 -0
- data/README.md +323 -0
- data/Rakefile +30 -0
- data/init.rb +1 -0
- data/lib/generators/validates_timeliness/install_generator.rb +16 -0
- data/lib/generators/validates_timeliness/templates/en.yml +16 -0
- data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +40 -0
- data/lib/jc-validates_timeliness.rb +1 -0
- data/lib/validates_timeliness.rb +70 -0
- data/lib/validates_timeliness/attribute_methods.rb +95 -0
- data/lib/validates_timeliness/conversion.rb +70 -0
- data/lib/validates_timeliness/extensions.rb +14 -0
- data/lib/validates_timeliness/extensions/date_time_select.rb +61 -0
- data/lib/validates_timeliness/extensions/multiparameter_handler.rb +80 -0
- data/lib/validates_timeliness/helper_methods.rb +23 -0
- data/lib/validates_timeliness/orm/active_record.rb +53 -0
- data/lib/validates_timeliness/orm/mongoid.rb +63 -0
- data/lib/validates_timeliness/railtie.rb +15 -0
- data/lib/validates_timeliness/validator.rb +117 -0
- data/lib/validates_timeliness/version.rb +3 -0
- data/spec/spec_helper.rb +109 -0
- data/spec/support/config_helper.rb +36 -0
- data/spec/support/model_helpers.rb +27 -0
- data/spec/support/tag_matcher.rb +35 -0
- data/spec/support/test_model.rb +59 -0
- data/spec/validates_timeliness/attribute_methods_spec.rb +86 -0
- data/spec/validates_timeliness/conversion_spec.rb +234 -0
- data/spec/validates_timeliness/extensions/date_time_select_spec.rb +163 -0
- data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +44 -0
- data/spec/validates_timeliness/helper_methods_spec.rb +30 -0
- data/spec/validates_timeliness/orm/active_record_spec.rb +244 -0
- data/spec/validates_timeliness/orm/mongoid_spec.rb +189 -0
- data/spec/validates_timeliness/validator/after_spec.rb +57 -0
- data/spec/validates_timeliness/validator/before_spec.rb +57 -0
- data/spec/validates_timeliness/validator/is_at_spec.rb +61 -0
- data/spec/validates_timeliness/validator/on_or_after_spec.rb +57 -0
- data/spec/validates_timeliness/validator/on_or_before_spec.rb +57 -0
- data/spec/validates_timeliness/validator_spec.rb +246 -0
- data/spec/validates_timeliness_spec.rb +43 -0
- data/validates_timeliness.gemspec +25 -0
- metadata +157 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTimeliness::Validator, ":after option" do
|
4
|
+
describe "for date type" do
|
5
|
+
before do
|
6
|
+
Person.validates_date :birth_date, :after => Date.new(2010, 1, 1)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be valid for same date value" do
|
10
|
+
invalid!(:birth_date, Date.new(2010, 1, 1), 'must be after 2010-01-01')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should not be valid for date before restriction" do
|
14
|
+
invalid!(:birth_date, Date.new(2009, 12, 31), 'must be after 2010-01-01')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid for date after restriction" do
|
18
|
+
valid!(:birth_date, Date.new(2010, 1, 2))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "for time type" do
|
23
|
+
before do
|
24
|
+
Person.validates_time :birth_time, :after => Time.mktime(2000, 1, 1, 12, 0, 0)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not be valid for same time as restriction" do
|
28
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be after 12:00:00')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not be valid for time before restriction" do
|
32
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be after 12:00:00')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be valid for time after restriction" do
|
36
|
+
valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "for datetime type" do
|
41
|
+
before do
|
42
|
+
Person.validates_datetime :birth_datetime, :after => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not be valid for same datetime as restriction" do
|
46
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be after 2010-01-01 12:00:00')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be valid for datetime is before restriction" do
|
50
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be after 2010-01-01 12:00:00')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be valid for datetime is after restriction" do
|
54
|
+
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTimeliness::Validator, ":before option" do
|
4
|
+
describe "for date type" do
|
5
|
+
before do
|
6
|
+
Person.validates_date :birth_date, :before => Date.new(2010, 1, 1)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be valid for date after restriction" do
|
10
|
+
invalid!(:birth_date, Date.new(2010, 1, 2), 'must be before 2010-01-01')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should not be valid for same date value" do
|
14
|
+
invalid!(:birth_date, Date.new(2010, 1, 1), 'must be before 2010-01-01')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid for date before restriction" do
|
18
|
+
valid!(:birth_date, Date.new(2009, 12, 31))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "for time type" do
|
23
|
+
before do
|
24
|
+
Person.validates_time :birth_time, :before => Time.mktime(2000, 1, 1, 12, 0, 0)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not be valid for time after restriction" do
|
28
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be before 12:00:00')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not be valid for same time as restriction" do
|
32
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be before 12:00:00')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be valid for time before restriction" do
|
36
|
+
valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "for datetime type" do
|
41
|
+
before do
|
42
|
+
Person.validates_datetime :birth_datetime, :before => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not be valid for datetime after restriction" do
|
46
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be before 2010-01-01 12:00:00')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not be valid for same datetime as restriction" do
|
50
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be before 2010-01-01 12:00:00')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be valid for datetime before restriction" do
|
54
|
+
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTimeliness::Validator, ":is_at option" do
|
4
|
+
before do
|
5
|
+
Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "for date type" do
|
9
|
+
before do
|
10
|
+
Person.validates_date :birth_date, :is_at => Date.new(2010, 1, 1)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should not be valid for date before restriction" do
|
14
|
+
invalid!(:birth_date, Date.new(2009, 12, 31), 'must be at 2010-01-01')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not be valid for date after restriction" do
|
18
|
+
invalid!(:birth_date, Date.new(2010, 1, 2), 'must be at 2010-01-01')
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should be valid for same date value" do
|
22
|
+
valid!(:birth_date, Date.new(2010, 1, 1))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "for time type" do
|
27
|
+
before do
|
28
|
+
Person.validates_time :birth_time, :is_at => Time.mktime(2000, 1, 1, 12, 0, 0)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not be valid for time before restriction" do
|
32
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be at 12:00:00')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should not be valid for time after restriction" do
|
36
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be at 12:00:00')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should be valid for same time as restriction" do
|
40
|
+
valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "for datetime type" do
|
45
|
+
before do
|
46
|
+
Person.validates_datetime :birth_datetime, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not be valid for datetime before restriction" do
|
50
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be at 2010-01-01 12:00:00')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not be valid for datetime after restriction" do
|
54
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be at 2010-01-01 12:00:00')
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should be valid for same datetime as restriction" do
|
58
|
+
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTimeliness::Validator, ":on_or_after option" do
|
4
|
+
describe "for date type" do
|
5
|
+
before do
|
6
|
+
Person.validates_date :birth_date, :on_or_after => Date.new(2010, 1, 1)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be valid for date before restriction" do
|
10
|
+
invalid!(:birth_date, Date.new(2009, 12, 31), 'must be on or after 2010-01-01')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be valid for same date value" do
|
14
|
+
valid!(:birth_date, Date.new(2010, 1, 1))
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid for date after restriction" do
|
18
|
+
valid!(:birth_date, Date.new(2010, 1, 2))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "for time type" do
|
23
|
+
before do
|
24
|
+
Person.validates_time :birth_time, :on_or_after => Time.mktime(2000, 1, 1, 12, 0, 0)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not be valid for time before restriction" do
|
28
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be on or after 12:00:00')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be valid for time after restriction" do
|
32
|
+
valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01))
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be valid for same time as restriction" do
|
36
|
+
valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "for datetime type" do
|
41
|
+
before do
|
42
|
+
Person.validates_datetime :birth_datetime, :on_or_after => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not be valid for datetime before restriction" do
|
46
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be on or after 2010-01-01 12:00:00')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be valid for same datetime as restriction" do
|
50
|
+
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0))
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be valid for datetime after restriction" do
|
54
|
+
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTimeliness::Validator, ":on_or_before option" do
|
4
|
+
describe "for date type" do
|
5
|
+
before do
|
6
|
+
Person.validates_date :birth_date, :on_or_before => Date.new(2010, 1, 1)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not be valid for date after restriction" do
|
10
|
+
invalid!(:birth_date, Date.new(2010, 1, 2), 'must be on or before 2010-01-01')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be valid for date before restriction" do
|
14
|
+
valid!(:birth_date, Date.new(2009, 12, 31))
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid for same date value" do
|
18
|
+
valid!(:birth_date, Date.new(2010, 1, 1))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "for time type" do
|
23
|
+
before do
|
24
|
+
Person.validates_time :birth_time, :on_or_before => Time.mktime(2000, 1, 1, 12, 0, 0)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not be valid for time after restriction" do
|
28
|
+
invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be on or before 12:00:00')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be valid for time before restriction" do
|
32
|
+
valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59))
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be valid for same time as restriction" do
|
36
|
+
valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "for datetime type" do
|
41
|
+
before do
|
42
|
+
Person.validates_datetime :birth_datetime, :on_or_before => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not be valid for datetime after restriction" do
|
46
|
+
invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be on or before 2010-01-01 12:00:00')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be valid for same datetime as restriction" do
|
50
|
+
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0))
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not be valid for datetime before restriction" do
|
54
|
+
valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,246 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesTimeliness::Validator do
|
4
|
+
before do
|
5
|
+
Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "Model.validates with :timeliness option" do
|
9
|
+
it 'should use plugin validator class' do
|
10
|
+
Person.validates :birth_date, :timeliness => {:is_at => Date.new(2010,1,1), :type => :date}
|
11
|
+
expect(Person.validators).to have(1).kind_of(ActiveModel::Validations::TimelinessValidator)
|
12
|
+
invalid!(:birth_date, Date.new(2010,1,2))
|
13
|
+
valid!(:birth_date, Date.new(2010,1,1))
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should use default to :datetime type' do
|
17
|
+
Person.validates :birth_datetime, :timeliness => {:is_at => Time.mktime(2010,1,1)}
|
18
|
+
expect(Person.validators.first.type).to eq(:datetime)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should add attribute to timeliness attributes set' do
|
22
|
+
expect(PersonWithShim.timeliness_validated_attributes).not_to include(:birth_time)
|
23
|
+
|
24
|
+
PersonWithShim.validates :birth_time, :timeliness => {:is_at => "12:30"}
|
25
|
+
|
26
|
+
expect(PersonWithShim.timeliness_validated_attributes).to include(:birth_time)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should not be valid for value which not valid date or time value' do
|
31
|
+
Person.validates_date :birth_date
|
32
|
+
invalid!(:birth_date, "Not a date", 'is not a valid date')
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should not be valid attribute is type cast to nil but raw value is non-nil invalid value' do
|
36
|
+
Person.validates_date :birth_date, :allow_nil => true
|
37
|
+
record = Person.new
|
38
|
+
allow(record).to receive(:birth_date).and_return(nil)
|
39
|
+
allow(record).to receive(:_timeliness_raw_value_for).and_return("Not a date")
|
40
|
+
expect(record).not_to be_valid
|
41
|
+
expect(record.errors[:birth_date].first).to eq('is not a valid date')
|
42
|
+
end
|
43
|
+
|
44
|
+
describe ":allow_nil option" do
|
45
|
+
it 'should not allow nil by default' do
|
46
|
+
Person.validates_date :birth_date
|
47
|
+
invalid!(:birth_date, [nil], 'is not a valid date')
|
48
|
+
valid!(:birth_date, Date.today)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should allow nil when true' do
|
52
|
+
Person.validates_date :birth_date, :allow_nil => true
|
53
|
+
valid!(:birth_date, [nil])
|
54
|
+
end
|
55
|
+
|
56
|
+
context "with raw value cache" do
|
57
|
+
it "should not be valid with an invalid format" do
|
58
|
+
PersonWithShim.validates_date :birth_date, :allow_nil => true
|
59
|
+
|
60
|
+
p = PersonWithShim.new
|
61
|
+
p.birth_date = 'bogus'
|
62
|
+
|
63
|
+
expect(p).not_to be_valid
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe ":allow_blank option" do
|
69
|
+
it 'should not allow blank by default' do
|
70
|
+
Person.validates_date :birth_date
|
71
|
+
invalid!(:birth_date, '', 'is not a valid date')
|
72
|
+
valid!(:birth_date, Date.today)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should allow blank when true' do
|
76
|
+
Person.validates_date :birth_date, :allow_blank => true
|
77
|
+
valid!(:birth_date, '')
|
78
|
+
end
|
79
|
+
|
80
|
+
context "with raw value cache" do
|
81
|
+
it "should not be valid with an invalid format" do
|
82
|
+
PersonWithShim.validates_date :birth_date, :allow_blank => true
|
83
|
+
|
84
|
+
p = PersonWithShim.new
|
85
|
+
p.birth_date = 'bogus'
|
86
|
+
|
87
|
+
expect(p).not_to be_valid
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe ":between option" do
|
93
|
+
describe "array value" do
|
94
|
+
it 'should be split option into :on_or_after and :on_or_before values' do
|
95
|
+
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
|
96
|
+
Person.validates_date :birth_date, :between => [on_or_after, on_or_before]
|
97
|
+
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
|
98
|
+
expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
|
99
|
+
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
100
|
+
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
|
101
|
+
valid!(:birth_date, on_or_after)
|
102
|
+
valid!(:birth_date, on_or_before)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "range value" do
|
107
|
+
it 'should be split option into :on_or_after and :on_or_before values' do
|
108
|
+
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
|
109
|
+
Person.validates_date :birth_date, :between => on_or_after..on_or_before
|
110
|
+
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
|
111
|
+
expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
|
112
|
+
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
113
|
+
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
|
114
|
+
valid!(:birth_date, on_or_after)
|
115
|
+
valid!(:birth_date, on_or_before)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "range with excluded end value" do
|
120
|
+
it 'should be split option into :on_or_after and :before values' do
|
121
|
+
on_or_after, before = Date.new(2010,1,1), Date.new(2010,1,3)
|
122
|
+
Person.validates_date :birth_date, :between => on_or_after...before
|
123
|
+
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
|
124
|
+
expect(Person.validators.first.options[:before]).to eq(before)
|
125
|
+
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
|
126
|
+
invalid!(:birth_date, before, "must be before 2010-01-03")
|
127
|
+
valid!(:birth_date, on_or_after)
|
128
|
+
valid!(:birth_date, before - 1)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe ":ignore_usec option" do
|
134
|
+
it "should not be valid when usec values don't match and option is false" do
|
135
|
+
Person.validates_datetime :birth_datetime, :on_or_before => Time.utc(2010,1,2,3,4,5), :ignore_usec => false
|
136
|
+
invalid!(:birth_datetime, Time.utc(2010,1,2,3,4,5,10000))
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should be valid when usec values dont't match and option is true" do
|
140
|
+
Person.validates_datetime :birth_datetime, :on_or_before => Time.utc(2010,1,2,3,4,5), :ignore_usec => true
|
141
|
+
valid!(:birth_datetime, Time.utc(2010,1,2,3,4,5,10000))
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe ":format option" do
|
146
|
+
class PersonWithFormatOption
|
147
|
+
include TestModel
|
148
|
+
include TestModelShim
|
149
|
+
attribute :birth_date, :date
|
150
|
+
attribute :birth_time, :time
|
151
|
+
attribute :birth_datetime, :datetime
|
152
|
+
validates_date :birth_date, :format => 'dd-mm-yyyy'
|
153
|
+
end
|
154
|
+
|
155
|
+
let(:person) { PersonWithFormatOption.new }
|
156
|
+
|
157
|
+
with_config(:use_plugin_parser, true)
|
158
|
+
|
159
|
+
it "should be valid when value matches format" do
|
160
|
+
person.birth_date = '11-12-1913'
|
161
|
+
person.valid?
|
162
|
+
expect(person.errors[:birth_date]).to be_empty
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should not be valid when value does not match format" do
|
166
|
+
person.birth_date = '1913-12-11'
|
167
|
+
person.valid?
|
168
|
+
expect(person.errors[:birth_date]).to include('is not a valid date')
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
describe "restriction value errors" do
|
173
|
+
let(:person) { Person.new(:birth_date => Date.today) }
|
174
|
+
|
175
|
+
before do
|
176
|
+
Person.validates_time :birth_date, :is_at => lambda { raise }, :before => lambda { raise }
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should be added when ignore_restriction_errors is false" do
|
180
|
+
with_config(:ignore_restriction_errors, false) do
|
181
|
+
person.valid?
|
182
|
+
expect(person.errors[:birth_date].first).to match("Error occurred validating birth_date")
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should not be added when ignore_restriction_errors is true" do
|
187
|
+
with_config(:ignore_restriction_errors, true) do
|
188
|
+
person.valid?
|
189
|
+
expect(person.errors[:birth_date]).to be_empty
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'should exit on first error' do
|
194
|
+
with_config(:ignore_restriction_errors, false) do
|
195
|
+
person.valid?
|
196
|
+
expect(person.errors[:birth_date]).to have(1).items
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "#format_error_value" do
|
202
|
+
describe "default" do
|
203
|
+
it 'should format date error value as yyyy-mm-dd' do
|
204
|
+
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
|
205
|
+
expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'should format time error value as hh:nn:ss' do
|
209
|
+
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_time], :type => :time)
|
210
|
+
expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('12:34:56')
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'should format datetime error value as yyyy-mm-dd hh:nn:ss' do
|
214
|
+
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_datetime], :type => :datetime)
|
215
|
+
expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('2010-01-01 12:34:56')
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe "with missing translation" do
|
220
|
+
before :all do
|
221
|
+
I18n.locale = :es
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'should use the default format for the type' do
|
225
|
+
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
|
226
|
+
expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
|
227
|
+
end
|
228
|
+
|
229
|
+
after :all do
|
230
|
+
I18n.locale = :en
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
context "custom error message" do
|
236
|
+
it 'should be used for invalid type' do
|
237
|
+
Person.validates_date :birth_date, :invalid_date_message => 'custom invalid message'
|
238
|
+
invalid!(:birth_date, 'asdf', 'custom invalid message')
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'should be used for invalid restriction' do
|
242
|
+
Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message'
|
243
|
+
invalid!(:birth_date, Time.now, 'custom before message')
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|