Skip to content

Commit

Permalink
MAYBE?: Move config query inside model
Browse files Browse the repository at this point in the history
  • Loading branch information
KludgeKML committed Dec 10, 2024
1 parent 466a81b commit 9350b01
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/views/govuk_web_banners/_emergency_banner.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% emergency_banner = GovukWebBanners::EmergencyBanner.new(redis_client: Rails.application.config.emergency_banner_redis_client) %>
<% emergency_banner = GovukWebBanners::EmergencyBanner.new %>
<% if emergency_banner.active? %>
<%= render "govuk_publishing_components/components/emergency_banner", {
campaign_class: emergency_banner.campaign_class,
Expand Down
12 changes: 8 additions & 4 deletions lib/govuk_web_banners/emergency_banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module GovukWebBanners
class EmergencyBanner
attr_reader :campaign_class, :heading, :short_description, :link, :link_text

def initialize(redis_client:)
content = content_from_redis(redis_client)
def initialize
content = content_from_redis

@campaign_class = content[:campaign_class].presence
@heading = content[:heading].presence
Expand All @@ -21,9 +21,13 @@ def active?

private

def content_from_redis(client)
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
client.hgetall("emergency_banner").try(:symbolize_keys)
redis_client.hgetall("emergency_banner").try(:symbolize_keys)
end
rescue StandardError => e
GovukError.notify(e, extra: { context: "Emergency Banner Redis" })
Expand Down
23 changes: 15 additions & 8 deletions spec/units/emergency_banner_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
RSpec.describe GovukWebBanners::EmergencyBanner do
let(:redis_client) { double(hgetall: {}) }
subject(:emergency_banner) { GovukWebBanners::EmergencyBanner.new(redis_client:) }
subject(:emergency_banner) { GovukWebBanners::EmergencyBanner.new }

before do
Rails.application.config.emergency_banner_redis_client = double(hgetall: {})
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

GovukWebBanners::EmergencyBanner.new(redis_client:)
GovukWebBanners::EmergencyBanner.new(redis_client:)
GovukWebBanners::EmergencyBanner.new
GovukWebBanners::EmergencyBanner.new

expect(redis_client).to have_received(:hgetall).once
expect(Rails.application.config.emergency_banner_redis_client).to have_received(:hgetall).once

travel_to(Time.now + 61.seconds)

GovukWebBanners::EmergencyBanner.new(redis_client:)
expect(redis_client).to have_received(:hgetall).twice
GovukWebBanners::EmergencyBanner.new
expect(Rails.application.config.emergency_banner_redis_client).to have_received(:hgetall).twice

travel_back
end
Expand All @@ -31,7 +34,9 @@
end

context "with the emergency banner active" do
let(:redis_client) { double(hgetall: { heading: "Emergency!", campaign_class: "notable-death" }) }
before do
Rails.application.config.emergency_banner_redis_client = double(hgetall: { heading: "Emergency!", campaign_class: "notable-death" })
end

it "returns true" do
expect(emergency_banner.active?).to be true
Expand All @@ -40,7 +45,9 @@

context "if the call to Redis fails" do
before do
redis_client = double
allow(redis_client).to receive(:hgetall).with("emergency_banner").and_raise(StandardError)
Rails.application.config.emergency_banner_redis_client = redis_client
end

it "returns false" do
Expand Down

0 comments on commit 9350b01

Please sign in to comment.