-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from alphagov/emergency-banner
Add Emergency Banner handling
- Loading branch information
Showing
18 changed files
with
224 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<% emergency_banner = GovukWebBanners::EmergencyBanner.new %> | ||
<% if emergency_banner.active? %> | ||
<%= render "govuk_publishing_components/components/emergency_banner", { | ||
campaign_class: emergency_banner.campaign_class, | ||
heading: emergency_banner.heading, | ||
link: emergency_banner.link, | ||
link_text: emergency_banner.link_text, | ||
short_description: emergency_banner.short_description, | ||
homepage: local_assigns[:homepage] || false, | ||
} %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "govuk_app_config/govuk_error" | ||
require "redis" | ||
|
||
module GovukWebBanners | ||
class EmergencyBanner | ||
attr_reader :campaign_class, :heading, :short_description, :link, :link_text | ||
|
||
def initialize | ||
content = content_from_redis | ||
|
||
@campaign_class = content[:campaign_class].presence | ||
@heading = content[:heading].presence | ||
@short_description = content[:short_description].presence | ||
@link = content[:link].presence | ||
@link_text = content[:link_text].presence | ||
end | ||
|
||
def active? | ||
[campaign_class, heading].all? | ||
end | ||
|
||
private | ||
|
||
def redis_client | ||
Rails.application.config.emergency_banner_redis_client | ||
end | ||
|
||
def content_from_redis | ||
Rails.cache.fetch("#emergency_banner/config", expires_in: 1.minute) do | ||
redis_client.hgetall("emergency_banner").try(:symbolize_keys) | ||
end | ||
rescue StandardError => e | ||
GovukError.notify(e, extra: { context: "Emergency Banner Redis" }) | ||
{} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
//= link govuk_web_banners_manifest.js | ||
|
||
// Everything below here is needed for the layout_for_public component in the system tests | ||
|
||
//= link govuk_publishing_components/vendor/lux/lux-measurer.js | ||
//= link govuk_publishing_components/vendor/lux/lux-reporter.js | ||
//= link govuk_publishing_components/rum-loader.js | ||
//= link govuk_publishing_components/load-analytics.js | ||
//= link govuk_publishing_components/ie.js | ||
|
||
//= link application.css | ||
//= link application.js | ||
//= link favicon.ico | ||
//= link favicon.svg | ||
//= link govuk-icon-mask.svg | ||
|
||
//= link govuk-icon-180.png | ||
//= link govuk-opengraph-image.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Empty - needed for the layout_for_public component in the system tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Empty - needed for the layout_for_public component in the system tests */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<%= render "govuk_publishing_components/components/layout_for_public", { | ||
emergency_banner: render("govuk_web_banners/emergency_banner") | ||
} | ||
%> |
4 changes: 4 additions & 0 deletions
4
spec/dummy/app/views/banner_pages/emergency_on_homepage.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<%= render "govuk_publishing_components/components/layout_for_public", { | ||
emergency_banner: render("govuk_web_banners/emergency_banner", homepage: true) | ||
} | ||
%> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Rails.application.config.emergency_banner_redis_client = Redis.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# Fixture - elements for request tests with dummy app | ||
banners: | ||
- name: Banner 1 | ||
suggestion_text: "This banner appears at /pages-with-banners!" | ||
suggestion_text: "This banner appears at /recruitment-with-banners!" | ||
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)" | ||
survey_url: https://google.com | ||
page_paths: | ||
- /page-with-banners | ||
- /recruitment-with-banners |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
RSpec.describe "Emergency Banners" do | ||
context "getting a path with the banner partial" do | ||
context "with the emergency banner active" do | ||
before do | ||
allow_any_instance_of(Redis).to receive(:hgetall).with("emergency_banner").and_return( | ||
heading: "Emergency!", | ||
campaign_class: "notable-death", | ||
) | ||
end | ||
|
||
it "shows a banner in the page" do | ||
get "/emergency" | ||
|
||
expect(response.body).to include("Emergency!") | ||
expect(response.body).not_to include("gem-c-emergency-banner__heading--homepage") | ||
end | ||
|
||
it "shows the homepage varient banner in the homepage" do | ||
get "/" | ||
|
||
expect(response.body).to include("Emergency!") | ||
expect(response.body).to include("gem-c-emergency-banner__heading--homepage") | ||
end | ||
end | ||
|
||
context "with the emergency banner inactive" do | ||
before do | ||
allow_any_instance_of(Redis).to receive(:hgetall).with("emergency_banner").and_return({}) | ||
end | ||
|
||
it "does not show a banner in the page" do | ||
get "/emergency" | ||
|
||
expect(response.body).not_to include("Emergency!") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
RSpec.describe GovukWebBanners::EmergencyBanner do | ||
let(:redis_client) { instance_double(Redis, hgetall: {}) } | ||
|
||
before do | ||
Rails.application.config.emergency_banner_redis_client = redis_client | ||
end | ||
|
||
describe "caching" do | ||
context "with a Rails cache" do | ||
it "caches calls to the redis client for one minute" do | ||
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache.lookup_store(:memory_store)) | ||
Rails.cache.clear | ||
|
||
described_class.new | ||
described_class.new | ||
|
||
expect(redis_client).to have_received(:hgetall).once | ||
|
||
travel_to(Time.now + 61.seconds) | ||
|
||
described_class.new | ||
expect(redis_client).to have_received(:hgetall).twice | ||
|
||
travel_back | ||
end | ||
end | ||
end | ||
|
||
describe "#active?" do | ||
subject(:emergency_banner) { described_class.new } | ||
|
||
context "with the emergency banner inactive" do | ||
it "returns false" do | ||
expect(emergency_banner.active?).to be false | ||
end | ||
end | ||
|
||
context "with the emergency banner active" do | ||
let(:redis_client) { instance_double(Redis, hgetall: { heading: "Emergency!", campaign_class: "notable-death" }) } | ||
|
||
it "returns true" do | ||
expect(emergency_banner.active?).to be true | ||
end | ||
end | ||
|
||
context "with a failing call to Redis fails" do | ||
before do | ||
allow(redis_client).to receive(:hgetall).with("emergency_banner").and_raise(StandardError) | ||
end | ||
|
||
it "returns false" do | ||
expect(emergency_banner.active?).to be false | ||
end | ||
end | ||
end | ||
end |