Moves Age calculation to a module instead of monkeypatching Date
This commit is contained in:
@@ -68,7 +68,7 @@ class Officing::Residence
|
||||
end
|
||||
|
||||
def allowed_age?
|
||||
date_of_birth.age_in_years <= User.minimum_required_age
|
||||
Age.in_years(date_of_birth) <= User.minimum_required_age
|
||||
end
|
||||
|
||||
def geozone
|
||||
|
||||
@@ -247,7 +247,7 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def age
|
||||
date_of_birth.try(:age_in_years)
|
||||
Age.in_years(date_of_birth)
|
||||
end
|
||||
|
||||
def save_requiring_finish_signup
|
||||
|
||||
@@ -32,7 +32,7 @@ class Verification::Management::Document
|
||||
end
|
||||
|
||||
def under_age?(response)
|
||||
response.date_of_birth && User.minimum_required_age < response.date_of_birth.age_in_years
|
||||
response.date_of_birth && User.minimum_required_age < Age.in_years(response.date_of_birth)
|
||||
end
|
||||
|
||||
def verified?
|
||||
|
||||
@@ -36,7 +36,7 @@ class Verification::Residence
|
||||
|
||||
def allowed_age
|
||||
return if errors[:date_of_birth].any?
|
||||
errors.add(:date_of_birth, I18n.t('verification.residence.new.error_not_allowed_age')) unless self.date_of_birth.age_in_years <= User.minimum_required_age
|
||||
errors.add(:date_of_birth, I18n.t('verification.residence.new.error_not_allowed_age')) unless Age.in_years(self.date_of_birth) <= User.minimum_required_age
|
||||
end
|
||||
|
||||
def document_number_uniqueness
|
||||
|
||||
1
config/initializers/age.rb
Normal file
1
config/initializers/age.rb
Normal file
@@ -0,0 +1 @@
|
||||
require 'age'
|
||||
@@ -1 +0,0 @@
|
||||
require 'date_ext'
|
||||
7
lib/age.rb
Normal file
7
lib/age.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
module Age
|
||||
def self.in_years(dob, now = Time.now.utc.to_date)
|
||||
return nil unless dob.present?
|
||||
# reference: http://stackoverflow.com/questions/819263/get-persons-age-in-ruby#comment21200772_819263
|
||||
now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
|
||||
end
|
||||
end
|
||||
@@ -1,8 +0,0 @@
|
||||
module AgeInYears
|
||||
def age_in_years(now = Time.now.utc.to_date)
|
||||
# reference: http://stackoverflow.com/questions/819263/get-persons-age-in-ruby#comment21200772_819263
|
||||
now.year - year - ((now.month > month || (now.month == month && now.day >= day)) ? 0 : 1)
|
||||
end
|
||||
end
|
||||
|
||||
Date.include(AgeInYears)
|
||||
58
spec/lib/age_spec.rb
Normal file
58
spec/lib/age_spec.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Age do
|
||||
describe '.in_years' do
|
||||
it "handles nils" do
|
||||
expect(Age.in_years(nil)).to be_nil
|
||||
end
|
||||
|
||||
it "calculates age correctly for common dates" do
|
||||
d = Date.new(1980, 3, 13)
|
||||
expect(Age.in_years(d, Date.new(2000, 3, 12))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 3, 13))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2000, 3, 14))).to eq(20)
|
||||
end
|
||||
|
||||
it "calculates age correctly for people born near a year's limit" do
|
||||
d = Date.new(1980, 12, 31)
|
||||
expect(Age.in_years(d, Date.new(2000, 12, 30))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 12, 31))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 1, 1))).to eq(20)
|
||||
|
||||
d = Date.new(1980, 1, 1)
|
||||
expect(Age.in_years(d, Date.new(2000, 12, 31))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 1, 1))).to eq(21)
|
||||
expect(Age.in_years(d, Date.new(2001, 1, 2))).to eq(21)
|
||||
end
|
||||
|
||||
it "calculates age correctly for people born around February the 29th" do
|
||||
# 1980 and 2000 are leap years. 2001 is a regular year
|
||||
d = Date.new(1980, 2, 29)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 27))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 28))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 29))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2000, 3, 1))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 2, 27))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 2, 28))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 3, 1))).to eq(21)
|
||||
|
||||
d = Date.new(1980, 2, 28)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 27))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 28))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 29))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2000, 3, 1))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 2, 27))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 2, 28))).to eq(21)
|
||||
expect(Age.in_years(d, Date.new(2001, 3, 1))).to eq(21)
|
||||
|
||||
d = Date.new(1980, 3, 1)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 27))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 28))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 2, 29))).to eq(19)
|
||||
expect(Age.in_years(d, Date.new(2000, 3, 1))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 2, 27))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 2, 28))).to eq(20)
|
||||
expect(Age.in_years(d, Date.new(2001, 3, 1))).to eq(21)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,55 +0,0 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Date do
|
||||
describe '#age_in_years' do
|
||||
it "calculates age correctly for common dates" do
|
||||
d = Date.new(1980, 3, 13)
|
||||
expect(d.age_in_years(Date.new(2000, 3, 12))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 3, 13))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2000, 3, 14))).to eq(20)
|
||||
end
|
||||
|
||||
it "calculates age correctly for people born near a year's limit" do
|
||||
d = Date.new(1980, 12, 31)
|
||||
expect(d.age_in_years(Date.new(2000, 12, 30))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 12, 31))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 1, 1))).to eq(20)
|
||||
|
||||
d = Date.new(1980, 1, 1)
|
||||
expect(d.age_in_years(Date.new(2000, 12, 31))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 1, 1))).to eq(21)
|
||||
expect(d.age_in_years(Date.new(2001, 1, 2))).to eq(21)
|
||||
end
|
||||
|
||||
it "calculates age correctly for people born around February the 29th" do
|
||||
# 1980 and 2000 are leap years. 2001 is a regular year
|
||||
d = Date.new(1980, 2, 29)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 27))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 28))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 29))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2000, 3, 1))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 2, 27))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 2, 28))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 3, 1))).to eq(21)
|
||||
|
||||
d = Date.new(1980, 2, 28)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 27))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 28))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 29))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2000, 3, 1))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 2, 27))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 2, 28))).to eq(21)
|
||||
expect(d.age_in_years(Date.new(2001, 3, 1))).to eq(21)
|
||||
|
||||
d = Date.new(1980, 3, 1)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 27))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 28))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 2, 29))).to eq(19)
|
||||
expect(d.age_in_years(Date.new(2000, 3, 1))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 2, 27))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 2, 28))).to eq(20)
|
||||
expect(d.age_in_years(Date.new(2001, 3, 1))).to eq(21)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user