Module: UserDateOfBirth
- Extended by:
- ActiveSupport::Concern
- Included in:
- User
- Defined in:
- app/models/concerns/user_date_of_birth.rb
Overview
This module contains all the methods of a user related to his date of birth.
Instance Method Summary collapse
- #age ⇒ Object
- #birthday_this_year ⇒ Object
- #build_date_of_birth_profile_field ⇒ Object
- #date_of_birth ⇒ Object
- #date_of_birth=(date_of_birth) ⇒ Object
- #date_of_birth_profile_field ⇒ Object
- #find_or_build_date_of_birth_profile_field ⇒ Object
- #find_or_create_date_of_birth_profile_field ⇒ Object
- #localized_date_of_birth ⇒ Object
- #localized_date_of_birth=(str) ⇒ Object
Instance Method Details
#age ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/concerns/user_date_of_birth.rb', line 48 def age cached do now = Time.now.utc.to_date dob = self.date_of_birth if dob now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1) else nil end end end |
#birthday_this_year ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/concerns/user_date_of_birth.rb', line 60 def birthday_this_year cached do begin date_of_birth.change(:year => Time.zone.now.year) rescue if date_of_birth.try(:month) == 2 && date_of_birth.try(:day) == 29 date_of_birth.change(year: Time.zone.now.year, month: 3, day: 1) else nil end end end end |
#build_date_of_birth_profile_field ⇒ Object
20 21 22 23 |
# File 'app/models/concerns/user_date_of_birth.rb', line 20 def build_date_of_birth_profile_field raise 'profile field already exists' if date_of_birth_profile_field @date_of_birth_profile_field = profile_fields.build( type: "ProfileFieldTypes::Date", label: 'date_of_birth' ) end |
#date_of_birth ⇒ Object
10 11 12 |
# File 'app/models/concerns/user_date_of_birth.rb', line 10 def date_of_birth cached { date_of_birth_profile_field.value.to_date if date_of_birth_profile_field.value if date_of_birth_profile_field } end |
#date_of_birth=(date_of_birth) ⇒ Object
13 14 15 |
# File 'app/models/concerns/user_date_of_birth.rb', line 13 def date_of_birth=( date_of_birth ) find_or_build_date_of_birth_profile_field.value = date_of_birth end |
#date_of_birth_profile_field ⇒ Object
17 18 19 |
# File 'app/models/concerns/user_date_of_birth.rb', line 17 def date_of_birth_profile_field @date_of_birth_profile_field ||= profile_fields.where( type: "ProfileFieldTypes::Date", label: 'date_of_birth' ).limit(1).first end |
#find_or_build_date_of_birth_profile_field ⇒ Object
25 26 27 |
# File 'app/models/concerns/user_date_of_birth.rb', line 25 def find_or_build_date_of_birth_profile_field date_of_birth_profile_field || build_date_of_birth_profile_field end |
#find_or_create_date_of_birth_profile_field ⇒ Object
33 34 35 |
# File 'app/models/concerns/user_date_of_birth.rb', line 33 def find_or_create_date_of_birth_profile_field date_of_birth_profile_field || ( build_date_of_birth_profile_field.save && date_of_birth_profile_field) end |
#localized_date_of_birth ⇒ Object
37 38 39 |
# File 'app/models/concerns/user_date_of_birth.rb', line 37 def localized_date_of_birth I18n.localize self.date_of_birth if self.date_of_birth end |
#localized_date_of_birth=(str) ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/models/concerns/user_date_of_birth.rb', line 40 def localized_date_of_birth=(str) begin self.date_of_birth = str.to_date rescue self.date_of_birth = nil end end |