tabletop-companion/app/models/table_invite.rb

39 lines
739 B
Ruby
Raw Normal View History

2024-05-29 14:24:18 +00:00
# frozen_string_literal: true
class TableInvite < ApplicationRecord
belongs_to :table
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP },
presence: true,
length: { maximum: 100 }
2024-05-29 15:47:31 +00:00
scope :not_responded, -> { where(accepted_at: nil, declined_at: nil) }
2024-05-29 14:24:18 +00:00
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