Created
December 12, 2018 10:20
-
-
Save stephenrichards/fa97c8c5addb3de2e769e65989a37ae9 to your computer and use it in GitHub Desktop.
Fat controller
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 ThinController < ApplicationController | |
def swipe_out | |
@card = OysteCard.find(params[:id]) | |
authorize @card # uses a Pundit policy to determine whether or not the current user is authorised to update this card | |
@journey_end_station = Station.find(params[:swipe_out][journey_end_station_id]) | |
if @card.journey_start_station.nil? | |
flash[:alert] << "No start of journey recorded - you have been charged the maximum fare" | |
@card.balance -= MAXIMUM_FARE | |
else | |
if @card.loaded_with_season_ticket? | |
@fare = calculate_fare_with_season_ticket | |
else | |
@fare = calculate_fare | |
end | |
end | |
if @fare > 0 | |
@card.balance -= @fare | |
@card.record_transaction(@fare, @journey_end_station) | |
end | |
if @card.balance < 0 | |
flash[:alert] << 'You have no balance on your card - you must top up before travelling again' | |
render :swipe_out | |
else | |
redirect_to card_show_path | |
end | |
end | |
private | |
def calculate_fare_with_season_ticket | |
# all sorts of code to determine whether the season ticket covers the journey | |
# or whether there is a fare to pay | |
end | |
def calculate_fare | |
# work out fare | |
zones_travelled = [@card.start_journey_station.zone, journey_end_station.zone].sort | |
fare = FARES[zones_travelled] | |
@card.balance -= fare | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment