diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cfe437..b0d8997 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 9.14.0 + +* Configure `time_zone` with `govuk_time_zone` [#392](/~https://github.com/alphagov/govuk_app_config/pull/392) + # 9.13.1 * Update dependencies diff --git a/README.md b/README.md index 995e2e8..6424ffa 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,17 @@ Some frontend apps support languages that are not defined in the i18n gem. This ## Time zone -This gem sets `config.time_zone` to `"London"` - this cannot currently be overridden in application config. +This gem sets `config.time_zone` to `"London"` by default. + +If you require a different time zone, you can set it with `config.govuk_time_zone`: + +```ruby +config.govuk_time_zone = "UTC" +``` + +Note that we've introduced a new config field (`govuk_time_zone`) here, because +it's otherwise not possible to distinguish between an app using UTC as the +default and an app explicitly asking for UTC. ## License diff --git a/lib/govuk_app_config/govuk_timezone.rb b/lib/govuk_app_config/govuk_timezone.rb index 9d80f08..986f64a 100644 --- a/lib/govuk_app_config/govuk_timezone.rb +++ b/lib/govuk_app_config/govuk_timezone.rb @@ -1,14 +1,12 @@ module GovukTimezone def self.configure(config) - case config.time_zone - when "UTC" - Rails.logger.info "govuk_app_config changing time_zone from UTC (the default) to London" - when "London" - Rails.logger.info "govuk_app_config always sets time_zone to London - there is no need to set config.time_zone in your app" + 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) && config.govuk_time_zone.present? + config.time_zone = config.govuk_time_zone else - raise "govuk_app_config prevents configuring time_zones other than London - config.time_zone was set to #{config.time_zone}" + 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.' + config.time_zone = "London" end - - config.time_zone = "London" end end diff --git a/lib/govuk_app_config/version.rb b/lib/govuk_app_config/version.rb index 58a4672..6a90ad9 100644 --- a/lib/govuk_app_config/version.rb +++ b/lib/govuk_app_config/version.rb @@ -1,3 +1,3 @@ module GovukAppConfig - VERSION = "9.13.1".freeze + VERSION = "9.14.0".freeze end diff --git a/spec/lib/govuk_timezone_spec.rb b/spec/lib/govuk_timezone_spec.rb index 9c41005..e9df702 100644 --- a/spec/lib/govuk_timezone_spec.rb +++ b/spec/lib/govuk_timezone_spec.rb @@ -1,9 +1,10 @@ +require "ostruct" require "spec_helper" require "govuk_app_config/govuk_timezone" RSpec.describe GovukError do describe ".configure" do - let(:config) { Rails::Railtie::Configuration.new } + let(:config) { OpenStruct.new } let(:logger) { instance_double("ActiveSupport::Logger") } before do @@ -17,16 +18,26 @@ expect(config.time_zone).to eq("London") end - it "should leave time_zones set to London as London" do - config.time_zone = "London" + it "should allow apps to set time_zone explicitly with config.govuk_time_zone" do + config.time_zone = "UTC" + config.govuk_time_zone = "Shanghai" + GovukTimezone.configure(config) + expect(config.time_zone).to eq("Shanghai") + end + + it "should default to London if config.govuk_time_zone is nil" do + config.time_zone = "UTC" + config.govuk_time_zone = nil expect(logger).to receive(:info) GovukTimezone.configure(config) expect(config.time_zone).to eq("London") end - it "should raise an error if configured with any other time zone" do + it "should raise an error if config.time_zone is set to anything other than the default UTC" do + config.time_zone = "London" + expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zone with config[.]time_zone/) config.time_zone = "Shanghai" - expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zones/) + expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zone with config[.]time_zone/) end end end