tabletop-companion/app/models/table_invite.rb

39 lines
739 B
Ruby

# frozen_string_literal: true
class TableInvite < ApplicationRecord
belongs_to :table
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP },
presence: true,
length: { maximum: 100 }
scope :not_responded, -> { where(accepted_at: nil, declined_at: nil) }
def accepted?
accepted_at.present?
end
def declined?
declined_at.present?
end
def responded?
accepted? || declined?
end
def accept!
raise "Already declined" if declined?
user = User.find_by(email:)
user.tables << table
user.save
update(accepted_at: Time.zone.now)
end
def decline!
raise "Already accepted" if accepted?
update(declined_at: Time.zone.now)
end
end