app.kluk.fr/app/models/user.rb

58 lines
1.4 KiB
Ruby
Raw Normal View History

2026-01-12 15:52:27 +01:00
class User < ApplicationRecord
has_secure_password
has_many :sessions, dependent: :destroy
2026-02-26 11:05:15 +01:00
has_many :moods, -> { order "recorded_at" }
2026-03-15 19:26:52 +01:00
has_many :modes
2026-03-19 21:21:28 +01:00
has_many :day_logs
2026-01-12 15:52:27 +01:00
normalizes :email_address, with: ->(e) { e.strip.downcase }
2026-01-12 19:24:51 +01:00
generates_token_for :invitation, expires_in: 5.days do
invitation_accepted_at
end
def invite_user!(email)
invited_user = User.create!(
email_address: email,
password: SecureRandom.hex(16) # temporary password
)
# Send invitation email using the generated token
token = invited_user.generate_token_for(:invitation)
UserMailer.invitation_email(invited_user, token).deliver_later
invited_user
end
def self.find_by_invitation_token(token)
find_by_token_for(:invitation, token)
end
def invitation_token
generate_token_for(:invitation)
end
2026-01-29 17:05:34 +01:00
def history
2026-03-21 20:28:56 +01:00
MoodCalendarService.generate_calendar(self)
2026-01-29 17:05:34 +01:00
end
2026-02-20 10:30:12 +01:00
2026-03-14 16:31:00 +01:00
def current_mode
2026-02-20 10:30:12 +01:00
self.moods.last&.mode || "croisiere"
end
2026-03-22 16:32:01 +01:00
# Le jour où l'humeur actuelle a commencé : le plus ancien des derniers
# enregistrements consécutifs dans le mode actuel.
def current_mode_since
since = nil
moods.reorder(recorded_at: :desc).pluck(:mode_id, :recorded_at).each do |mode_id, recorded_at|
break if since && mode_id != since[:mode_id]
since = { mode_id: mode_id, day: recorded_at.to_date }
end
since&.dig(:day)
end
2026-03-22 16:32:01 +01:00
def current_day_log
self.day_logs.find_by(day: Date.today)
end
2026-01-12 15:52:27 +01:00
end