diff --git a/README.md b/README.md index 87a84d9..995e2e8 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Adds the basics of a GOV.UK application: - Statsd client for reporting stats (deprecated; use Prometheus instead) - Rails logging - Content Security Policy generation for frontend apps +- Sets the time zone to London ## Installation @@ -181,6 +182,9 @@ GovukContentSecurityPolicy.configure Some frontend apps support languages that are not defined in the i18n gem. This provides them with our own custom rules for these languages. +## Time zone + +This gem sets `config.time_zone` to `"London"` - this cannot currently be overridden in application config. ## License diff --git a/lib/govuk_app_config.rb b/lib/govuk_app_config.rb index 2546419..939bb23 100644 --- a/lib/govuk_app_config.rb +++ b/lib/govuk_app_config.rb @@ -9,5 +9,6 @@ if defined?(Rails) require "govuk_app_config/govuk_json_logging" require "govuk_app_config/govuk_content_security_policy" + require "govuk_app_config/govuk_timezone" require "govuk_app_config/railtie" end diff --git a/lib/govuk_app_config/govuk_timezone.rb b/lib/govuk_app_config/govuk_timezone.rb new file mode 100644 index 0000000..9d80f08 --- /dev/null +++ b/lib/govuk_app_config/govuk_timezone.rb @@ -0,0 +1,14 @@ +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" + else + raise "govuk_app_config prevents configuring time_zones other than London - config.time_zone was set to #{config.time_zone}" + end + + config.time_zone = "London" + end +end diff --git a/lib/govuk_app_config/railtie.rb b/lib/govuk_app_config/railtie.rb index 63ccc77..1d76f67 100644 --- a/lib/govuk_app_config/railtie.rb +++ b/lib/govuk_app_config/railtie.rb @@ -14,6 +14,10 @@ class Railtie < Rails::Railtie end end + initializer "govuk_app_config.configure_timezone", before: "active_support.initialize_time_zone" do |app| + GovukTimezone.configure(app.config) + end + config.before_initialize do GovukJsonLogging.configure if ENV["GOVUK_RAILS_JSON_LOGGING"] end diff --git a/spec/lib/govuk_timezone_spec.rb b/spec/lib/govuk_timezone_spec.rb new file mode 100644 index 0000000..9c41005 --- /dev/null +++ b/spec/lib/govuk_timezone_spec.rb @@ -0,0 +1,32 @@ +require "spec_helper" +require "govuk_app_config/govuk_timezone" + +RSpec.describe GovukError do + describe ".configure" do + let(:config) { Rails::Railtie::Configuration.new } + let(:logger) { instance_double("ActiveSupport::Logger") } + + before do + allow(Rails).to receive(:logger).and_return(logger) + end + + it "should override the default UTC time_zone to London" do + config.time_zone = "UTC" + expect(logger).to receive(:info) + GovukTimezone.configure(config) + expect(config.time_zone).to eq("London") + end + + it "should leave time_zones set to London as London" do + config.time_zone = "London" + 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 + config.time_zone = "Shanghai" + expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zones/) + end + end +end