birth_date_validator 0.1.1 → 0.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 +4 -4
- data/lib/birth_date_validator/validator.rb +12 -3
- data/lib/birth_date_validator/version.rb +1 -1
- data/spec/birth_date_validator_spec.rb +16 -16
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdf56f43fbfe67dd19bc0acaf83e153d68d3e0e9
|
4
|
+
data.tar.gz: 889a7bb058a1fad32cc194a8cc960ce0de10e260
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 401052166287b650a981d935c56d9be68d8747ce92c387bab52885790fd93d28556523eb961a775ac254153bb82985428923a66e27b5199bd09f6a3a6c165bec
|
7
|
+
data.tar.gz: 92d01741c4825da57e3385572570dec00a681de5e02d4c52ea5ceb85ac362fd32d1a5a3a5c5583437c8123723efa168dcca963b31ca857e471dd99a2c9a61680
|
@@ -10,14 +10,23 @@
|
|
10
10
|
|
11
11
|
class BirthDateValidator < ActiveModel::EachValidator
|
12
12
|
def validate_each(record, attribute, value)
|
13
|
+
age_to_validate = age(value)
|
14
|
+
|
13
15
|
invalid = if options[:at_least].present?
|
14
|
-
|
16
|
+
age_to_validate < age(options[:at_least])
|
15
17
|
elsif options[:less_then].present?
|
16
|
-
|
18
|
+
age_to_validate >= age(options[:less_then])
|
17
19
|
elsif options[:range].present?
|
18
|
-
|
20
|
+
age_to_validate < age(options[:range].first) || age_to_validate > age(options[:range].last)
|
19
21
|
end
|
20
22
|
|
21
23
|
record.errors.add(attribute, options.fetch(:message, :invalid)) if invalid
|
22
24
|
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def age(birth_date)
|
29
|
+
now = Date.today
|
30
|
+
now.year - birth_date.year - ((now.month > birth_date.month || (now.month == birth_date.month && now.day >= birth_date.day)) ? 0 : 1)
|
31
|
+
end
|
23
32
|
end
|
@@ -2,6 +2,12 @@ require './spec_helper'
|
|
2
2
|
|
3
3
|
describe BirthDateValidator do
|
4
4
|
describe 'user has to have at least 18 years' do
|
5
|
+
context '18 years old' do
|
6
|
+
it 'is valid' do
|
7
|
+
expect(TestUserOverAge.new(birth_date: 18.years.ago)).to be_valid
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
5
11
|
context '19 years old' do
|
6
12
|
it 'is valid' do
|
7
13
|
expect(TestUserOverAge.new(birth_date: 19.years.ago)).to be_valid
|
@@ -17,27 +23,21 @@ describe BirthDateValidator do
|
|
17
23
|
expect(subject.errors[:birth_date][0]).to include 'older'
|
18
24
|
end
|
19
25
|
end
|
26
|
+
end
|
20
27
|
|
21
|
-
|
28
|
+
describe 'user has to have less then 18 years' do
|
29
|
+
context '17 years old' do
|
22
30
|
it 'is valid' do
|
23
|
-
expect(
|
31
|
+
expect(TestUserUnderAge.new(birth_date: 17.years.ago)).to be_valid
|
24
32
|
end
|
25
33
|
end
|
26
|
-
end
|
27
34
|
|
28
|
-
describe 'user has to have less then 18 years' do
|
29
35
|
context '19 years old' do
|
30
36
|
it 'is not valid' do
|
31
37
|
expect(TestUserUnderAge.new(birth_date: 19.years.ago)).to_not be_valid
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
35
|
-
context '17 years old' do
|
36
|
-
it 'is valid' do
|
37
|
-
expect(TestUserUnderAge.new(birth_date: 17.years.ago)).to be_valid
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
41
|
context '18 years old' do
|
42
42
|
it 'is not valid' do
|
43
43
|
expect(TestUserUnderAge.new(birth_date: 18.years.ago)).to_not be_valid
|
@@ -46,18 +46,18 @@ describe BirthDateValidator do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
describe 'user has to have from 18 and 30 years' do
|
49
|
-
context '17 years old' do
|
50
|
-
it 'is not valid' do
|
51
|
-
expect(TestUserRange.new(birth_date: 17.years.ago)).to_not be_valid
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
49
|
context '30 years old' do
|
56
50
|
it 'is valid' do
|
57
51
|
expect(TestUserRange.new(birth_date: 30.years.ago)).to be_valid
|
58
52
|
end
|
59
53
|
end
|
60
54
|
|
55
|
+
context '17 years old' do
|
56
|
+
it 'is not valid' do
|
57
|
+
expect(TestUserRange.new(birth_date: 17.years.ago)).to_not be_valid
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
61
|
context '31 years old' do
|
62
62
|
it 'is not valid' do
|
63
63
|
expect(TestUserRange.new(birth_date: 31.years.ago)).to_not be_valid
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: birth_date_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Giovanni Capuano
|
@@ -66,8 +66,8 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description:
|
70
|
-
|
69
|
+
description: Minimal birth date validator to check if the user has the right age to
|
70
|
+
sign in the website.
|
71
71
|
email: webmaster@giovannicapuano.net
|
72
72
|
executables: []
|
73
73
|
extensions: []
|