2019-06-04 13:33:00 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-06 13:58:49 +02:00
|
|
|
AVG_DAYS_PER_YEAR = 365.2425
|
|
|
|
|
2019-06-04 13:33:00 +02:00
|
|
|
# This table will save the user's profile data needed for statistical purposes.
|
|
|
|
# GDPR requires that an user can delete his account at any time but we need to keep the statistics original data to being able to
|
|
|
|
# rebuild them at any time.
|
|
|
|
# The data will be kept even if the user is deleted, but it will be unlinked from the user's account (ie. anonymized)
|
2020-03-25 10:16:47 +01:00
|
|
|
class StatisticProfile < ApplicationRecord
|
2019-06-03 17:25:04 +02:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :group
|
2019-06-06 13:58:49 +02:00
|
|
|
belongs_to :role
|
2019-06-03 17:25:04 +02:00
|
|
|
|
2019-06-04 16:50:23 +02:00
|
|
|
has_many :subscriptions, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :subscriptions, allow_destroy: false
|
|
|
|
|
|
|
|
has_many :reservations, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :reservations, allow_destroy: false
|
|
|
|
|
2021-06-21 17:39:48 +02:00
|
|
|
# bought packs
|
|
|
|
has_many :statistic_profile_prepaid_packs, dependent: :destroy
|
|
|
|
has_many :prepaid_packs, through: :statistic_profile_prepaid_packs
|
|
|
|
|
2019-06-05 16:21:39 +02:00
|
|
|
# Trainings that were validated by an admin
|
|
|
|
has_many :statistic_profile_trainings, dependent: :destroy
|
|
|
|
has_many :trainings, through: :statistic_profile_trainings
|
|
|
|
|
2019-06-06 16:34:53 +02:00
|
|
|
# Projects that the current user is the author
|
2023-02-14 13:10:58 +01:00
|
|
|
has_many :my_projects, foreign_key: :author_statistic_profile_id, class_name: 'Project', dependent: :destroy, inverse_of: :author
|
2019-06-06 16:34:53 +02:00
|
|
|
|
2021-10-21 09:28:41 +02:00
|
|
|
validate :check_birthday_in_past
|
2021-10-20 17:31:30 +02:00
|
|
|
|
2019-06-04 13:33:00 +02:00
|
|
|
def str_gender
|
2024-03-21 18:21:11 +01:00
|
|
|
return '' if gender.blank?
|
|
|
|
|
2019-06-04 13:33:00 +02:00
|
|
|
gender ? 'male' : 'female'
|
|
|
|
end
|
|
|
|
|
|
|
|
def age
|
|
|
|
if birthday.present?
|
2023-02-14 13:10:58 +01:00
|
|
|
now = Time.current.utc.to_date
|
2019-06-06 13:58:49 +02:00
|
|
|
(now - birthday).to_f / AVG_DAYS_PER_YEAR
|
2019-06-04 13:33:00 +02:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
2021-10-20 17:31:30 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_birthday_in_past
|
2022-05-04 08:50:10 +02:00
|
|
|
errors.add(:birthday, I18n.t('statistic_profile.birthday_in_past')) if birthday.present? && birthday > Date.current
|
2021-10-20 17:31:30 +02:00
|
|
|
end
|
2019-06-03 17:25:04 +02:00
|
|
|
end
|