refactors age calculation

This commit is contained in:
Juanjo Bazán
2017-01-25 17:16:17 +01:00
parent 754095f4d0
commit cc6d841978
2 changed files with 8 additions and 4 deletions

View File

@@ -42,11 +42,12 @@ class Poll
private
def voter_age(date_of_birth)
if date_of_birth.blank?
def voter_age(dob)
if dob.blank?
nil
else
((Date.today - date_of_birth.to_date).to_i / 365.25).to_i
now = Time.now.utc.to_date
now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
end
end

View File

@@ -250,7 +250,10 @@ class User < ActiveRecord::Base
if date_of_birth.blank?
nil
else
((Date.today - date_of_birth.to_date).to_i / 365.25).to_i
now = Time.now.utc.to_date
now.year - date_of_birth.year - (
(now.month > date_of_birth.month || (now.month == date_of_birth.month && now.day >= date_of_birth.day)
) ? 0 : 1)
end
end