-
-
Save jvans1/9745395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ticket < ActiveRecord::Base | |
class TicketConfirmer | |
attr_reader :ticket | |
delegate :user, :grouper :to => :ticket | |
def initialize(ticket) | |
@ticket = ticket | |
@confirmation_errors = [] | |
end | |
def can_confirm? | |
run_confirmations | |
confirm_errors.any? | |
end | |
def confirmation_errors | |
@confirmation_errors | |
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 | |
def confirm | |
update confirmed: true if confirmable? | |
end | |
def confirmable? | |
valid? :confirmation | |
end | |
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