From 9cf92cdd1dc85567b90c69667641ecc677df3b39 Mon Sep 17 00:00:00 2001 From: "joseph@dxw.com" Date: Tue, 26 Nov 2024 18:43:30 +0000 Subject: [PATCH] Fallback to the default zone when a zone can't be found Previously we would only use the default if no parameter was give. If a parameter was a given that didn't match any zone, we return an empty result and this would cause problems. We now default to the default zone when a zone cannot be found. --- app/controllers/forecasts_controller.rb | 6 +++--- spec/controllers/forecasts_controller_spec.rb | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/forecasts_controller.rb b/app/controllers/forecasts_controller.rb index dc722fd3..32f808e6 100644 --- a/app/controllers/forecasts_controller.rb +++ b/app/controllers/forecasts_controller.rb @@ -48,9 +48,9 @@ def forecast_for_day(day, forecasts) end def zone - return Zone.default unless params[:zone] - - Zone.find_by(name: params[:zone]) + Zone.find_by!(name: params[:zone]) + rescue ActiveRecord::RecordNotFound + Zone.default end def date diff --git a/spec/controllers/forecasts_controller_spec.rb b/spec/controllers/forecasts_controller_spec.rb index aeedec25..667077b5 100644 --- a/spec/controllers/forecasts_controller_spec.rb +++ b/spec/controllers/forecasts_controller_spec.rb @@ -3,7 +3,6 @@ let(:barnet) { double("Barnet") } before do - allow(Zone).to receive(:find_by).and_return(barnet) allow(Zone).to receive(:default).and_return(central_london) allow(CercForecastService).to receive(:latest_forecasts_for).and_return(forecasts) end @@ -23,7 +22,9 @@ context "when zone IS given" do it "obtains forecasts for the given zone from the CercForecastService" do - get :show, params: {zone: double} + allow(Zone).to receive(:find_by).and_return(barnet) + + get :show, params: {zone: "Barnet"} expect(CercForecastService).to have_received(:latest_forecasts_for).with(barnet) end