Skip to content

Instantly share code, notes, and snippets.

@mariochavez
Forked from dhh/ticket.rb
Created August 6, 2022 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariochavez/e78b765d9f590b73536e5dc8f1bc8341 to your computer and use it in GitHub Desktop.
Save mariochavez/e78b765d9f590b73536e5dc8f1bc8341 to your computer and use it in GitHub Desktop.
class Ticket < ActiveRecord::Base
belongs_to :grouper
belongs_to :user
validate :user_cant_be_blacklisted, on: :confirmation
validate :user_cant_double_book, on: :confirmation
validate :grouper_cant_be_full, on: :confirmation
validate :grouper_cant_have_occurred, on: :confirmation
validate :ticket_cant_have_been_confirmed, on: :confirmation
def confirm
update confirmed: true if confirmable?
end
def confirmable?
valid? :confirmation
end
private
def user_cant_be_blacklisted
errors.add :user, "can't book a Grouper at this time" if user.blacklisted?
end
def user_cant_double_book
errors.add :user, 'are already going to a Grouper on that day' if user.has_existing_grouper?(grouper)
end
def grouper_cant_be_full
errors.add :grouper, 'has already occurred!' if grouper.full?
end
def grouper_cant_have_occurred
errors.add :grouper, 'has already occurred!' if grouper.past?
end
def ticket_cant_have_been_confirmed
errors.add :user, 'have already confirmed this ticket' if confirmed?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment