47 lines
1.7 KiB
Ruby
47 lines
1.7 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 :requested_entries, class_name: "Entry", foreign_key: :requested_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 :comments, dependent: :nullify
|
|
|
|
enum :role, %i[contributor reviewer admin]
|
|
|
|
validates :email, presence: true, uniqueness: true
|
|
validates :password, length: { minimum: 12 }, if: -> { password.present? }
|
|
|
|
scope :by_role, ->(role) { where(role: role) if role.present? }
|
|
scope :search_email, ->(q) { where("email LIKE ?", "%#{sanitize_sql_like(q)}%") if q.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
|