Files
sanasto-wiki/app/models/user.rb
2026-01-23 13:49:56 +01:00

44 lines
1.5 KiB
Ruby

class User < ApplicationRecord
has_secure_password
belongs_to :invited_by, class_name: "User", optional: true
has_many :invited_users, class_name: "User", foreign_key: :invited_by_id, dependent: :nullify
has_many :created_entries, class_name: "Entry", foreign_key: :created_by_id, dependent: :nullify
has_many :updated_entries, class_name: "Entry", foreign_key: :updated_by_id, dependent: :nullify
has_many :submitted_suggested_meanings,
class_name: "SuggestedMeaning",
foreign_key: :submitted_by_id,
dependent: :nullify
has_many :reviewed_suggested_meanings,
class_name: "SuggestedMeaning",
foreign_key: :reviewed_by_id,
dependent: :nullify
has_many :entry_versions, dependent: :nullify
has_many :comments, dependent: :nullify
enum :role, %i[contributor reviewer admin]
validates :email, presence: true, uniqueness: true
validates :password, length: { minimum: 12 }, if: -> { password.present? }
# Invitation token expires after 14 days
INVITATION_TOKEN_EXPIRY = 14.days
def invitation_expired?
return false if invitation_sent_at.nil?
invitation_sent_at < INVITATION_TOKEN_EXPIRY.ago
end
def invitation_pending?
invitation_token.present? && invitation_accepted_at.nil? && !invitation_expired?
end
def self.find_by_valid_invitation_token(token)
where(invitation_token: token)
.where(invitation_accepted_at: nil)
.where("invitation_sent_at > ?", INVITATION_TOKEN_EXPIRY.ago)
.first
end
end