Skip to content

Instantly share code, notes, and snippets.

@jvans1
Forked from dhh/ticket.rb
Last active August 29, 2015 13:57
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 jvans1/9745395 to your computer and use it in GitHub Desktop.
Save jvans1/9745395 to your computer and use it in GitHub Desktop.
class Ticket < ActiveRecord::Base
class TicketConfirmer
attr_reader :ticket, :confirmation_errors
delegate :user, :grouper :to => :ticket
def initialize(ticket)
@ticket = ticket
@confirmation_errors = []
end
def can_confirm?
confirmation_errors.clear
run_confirmations
confirm_errors.any?
end
private
def run_confirmations
confirmation_errors << [:user, "can't book a Grouper at this time"] if user.blacklisted?
confirmation_errors << [:user, 'are already going to a Grouper on that day'] if user.has_existing_grouper?(grouper)
confirmation_errors << [:grouper, 'has already occurred!'] if grouper.full?
confirmation_errors << [:grouper, 'has already occurred!'] if grouper.past?
confirmation_errors << [:user, 'have already confirmed this ticket'] if confirmed?
end
end
belongs_to :grouper
belongs_to :user
validate :can_confirm , :on => :confirmation
private
def can_confirm
if (ticket_confirmer = TicketConfirmer.new(self)).can_confirm?
ticket_confirmer.confirmation_errors.each do |object, error|
errors.add(object, error)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment