Skip to content

Instantly share code, notes, and snippets.

@mirrec
Created March 4, 2014 12:37
Show Gist options
  • Save mirrec/9345737 to your computer and use it in GitHub Desktop.
Save mirrec/9345737 to your computer and use it in GitHub Desktop.
Use exception for exceptional cases, and handle them properly in application
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
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