Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set time_zone to London in all GOV.UK apps #381

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/govuk_app_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions lib/govuk_app_config/govuk_timezone.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/govuk_app_config/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions spec/lib/govuk_timezone_spec.rb
Original file line number Diff line number Diff line change
@@ -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