Created
March 4, 2014 12:37
-
-
Save mirrec/9345737 to your computer and use it in GitHub Desktop.
Use exception for exceptional cases, and handle them properly in application
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 SeasonManager | |
NoSeason = Class.new(StandardError) | |
# actual league that we players are playing, if we are in the right date window | |
def self.active | |
Season.active | |
end | |
# in season break, it return next season | |
def self.upcoming | |
Season.upcoming | |
end | |
def self.for_ordering | |
if active && active.upcoming_round? | |
active | |
elsif upcoming | |
upcoming | |
else | |
raise SeasonManager::NoSeason.new("we are missing upcoming season, create one in admin") | |
end | |
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 SeasonTicketsController < ApplicationController | |
rescue_from SeasonManager::NoSeason, :with => :no_season_apologize | |
def create | |
@season_ticket = SeasonTicket.new(params[:season_ticket]) | |
# this call can raise SeasonManager::NoSeason | |
@season_ticket.season = SeasonManager.for_ordering | |
# ... | |
end | |
def no_season_apologize | |
flash[:alert] = t("season_tickets.messages.no_season_apologize.alert") | |
redirect_to main_app.profile_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment