From f6054dd80794f29db909a2188a0efa6f522c0de6 Mon Sep 17 00:00:00 2001 From: Richard Towers Date: Tue, 23 Jul 2024 09:12:31 +0100 Subject: [PATCH] Handle nil for config.govuk_time_zone --- lib/govuk_app_config/govuk_timezone.rb | 2 +- spec/lib/govuk_timezone_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/govuk_app_config/govuk_timezone.rb b/lib/govuk_app_config/govuk_timezone.rb index b6e1b26..986f64a 100644 --- a/lib/govuk_app_config/govuk_timezone.rb +++ b/lib/govuk_app_config/govuk_timezone.rb @@ -2,7 +2,7 @@ module GovukTimezone def self.configure(config) raise "govuk_app_config prevents configuring time_zone with config.time_zone - use config.govuk_time_zone instead" unless config.time_zone == "UTC" - if config.respond_to? :govuk_time_zone + if config.respond_to?(:govuk_time_zone) && config.govuk_time_zone.present? config.time_zone = config.govuk_time_zone else Rails.logger.info 'govuk_app_config changing time_zone from UTC (the rails default) to London (the GOV.UK default). Set config.govuk_time_zone = "UTC" if you need UTC.' diff --git a/spec/lib/govuk_timezone_spec.rb b/spec/lib/govuk_timezone_spec.rb index a62180d..2b1ff98 100644 --- a/spec/lib/govuk_timezone_spec.rb +++ b/spec/lib/govuk_timezone_spec.rb @@ -22,6 +22,13 @@ expect(config.time_zone).to eq("Shanghai") end + it "should default to London if config.govuk_time_zone is nil" do + config = Struct.new(:time_zone, :govuk_time_zone).new("UTC", nil) + expect(logger).to receive(:info) + GovukTimezone.configure(config) + expect(config.time_zone).to eq("London") + end + it "should raise an error if config.time_zone is set to anything other than the default UTC" do config = Struct.new(:time_zone).new("London") expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zone with config[.]time_zone/)