37 lines
665 B
Ruby
37 lines
665 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 }
|
||
|
|
||
|
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
|