-
-
Save pier-oliviert/9673715 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::GrouperValidator < ActiveModel::Validator | |
def validate(record) | |
record.errors.add :grouper, 'has already occurred!' if record.grouper.full? | |
record.errors.add :grouper, 'has already occurred!' if record.grouper.past? | |
end | |
end |
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::UserValidator < ActiveModel::Validator | |
def validate(record) | |
record.errors.add :user, "can't book a Grouper at this time" if record.user.blacklisted? | |
record.errors.add :user, 'are already going to a Grouper on that day' if record.user.has_existing_grouper?(record.grouper) | |
end | |
end |
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 | |
belongs_to :grouper | |
belongs_to :user | |
validate confirmation_validations, 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 confirmation_validations | |
validates_with Ticket::UserValidator, Ticket::GrouperValidation | |
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