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 |
This comment has been minimized.
This comment has been minimized.
wrong error message on line 32, not errors.add :grouper, 'is already full!' if grouper.full? |
This comment has been minimized.
This comment has been minimized.
I didn't know that this is possible and judging from all the |
This comment has been minimized.
This comment has been minimized.
David, how would you handle the logic in the controller which sets the redirect based on the error message? I think to couple the controller code to the message text is not optimal, in case one changes the message text. I suppose one could define constants for these, and go off of the constants. And then the question is whether the controller should have all the logic pairing up the specific error message with the right redirect. I would tend to write code directly in the controller at first, and then refactor into a separate class where I could verify the logic separate from the actual controller, per my example here: shakacode/fat-code-refactoring-techniques#6 |
This comment has been minimized.
This comment has been minimized.
I wish there was an |
This comment has been minimized.
I like this much more than that mess of policy objects. Wouldn't this mean that if a view calls
ticket.confirmable?
somewhere, e.g. to see if a "Confirm" button should be shown, after that point the ticket might have a bunch of errors set on it? Might have strange interactions if you further down in the view you showticket.errors
. Not that I've ever found this to be an issue in real life though.