adds User#age

This commit is contained in:
Juanjo Bazán
2017-01-25 14:17:49 +01:00
parent 8d8d822323
commit 0b2454fb97
2 changed files with 15 additions and 0 deletions

View File

@@ -246,6 +246,14 @@ class User < ActiveRecord::Base
"#{name} (#{email})" "#{name} (#{email})"
end end
def age
if date_of_birth.blank?
nil
else
((Date.today - date_of_birth.to_date).to_i / 365.25).to_i
end
end
def save_requiring_finish_signup def save_requiring_finish_signup
begin begin
self.registering_with_oauth = true self.registering_with_oauth = true

View File

@@ -65,6 +65,13 @@ describe User do
end end
end end
describe "#age" do
it "is the rounded integer age based on the date_of_birth" do
user = create(:user, date_of_birth: 33.years.ago)
expect(user.age).to eq(33)
end
end
describe 'preferences' do describe 'preferences' do
describe 'email_on_comment' do describe 'email_on_comment' do
it 'should be false by default' do it 'should be false by default' do