41 lines
967 B
Ruby
41 lines
967 B
Ruby
class User < ApplicationRecord
|
|
has_secure_password
|
|
has_many :sessions, dependent: :destroy
|
|
has_many :moods, -> { order "recorded_at" }
|
|
has_many :modes
|
|
|
|
normalizes :email_address, with: ->(e) { e.strip.downcase }
|
|
|
|
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
|
|
|
|
def history
|
|
MoodCalendarService.generate_calendar(moods)
|
|
end
|
|
|
|
def current_mode
|
|
self.moods.last&.mode || "croisiere"
|
|
end
|
|
end
|