Skip to content

Commit

Permalink
Dry up Recruitment Banner logic
Browse files Browse the repository at this point in the history
  • Loading branch information
georges1996 committed Jan 3, 2024
1 parent c1949ec commit d99cbbb
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 33 deletions.
1 change: 1 addition & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ContentItemsController < ApplicationController
include GovukPersonalisation::ControllerConcern
include Slimmer::Headers
include Slimmer::Template
include RecruitmentBannerHelper

rescue_from GdsApi::HTTPForbidden, with: :error_403
rescue_from GdsApi::HTTPNotFound, with: :error_notfound
Expand Down
27 changes: 27 additions & 0 deletions app/helpers/recruitment_banner_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module RecruitmentBannerHelper
def recruitment_banner
return false if recruitment_banners.nil?

current_path = request.path

recruitment_banners.find do |banner|
next unless valid?(banner)

banner["page_paths"]&.include?(current_path)
end
end

def recruitment_banners
recruitment_banners_urls_file_path = Rails.root.join("lib/data/recruitment_banners.yml")
recruitment_banners_data = YAML.load_file(recruitment_banners_urls_file_path)
recruitment_banners_data["banners"]
end

def valid?(banner)
required_fields.select { |field| banner[field].present? } == required_fields
end

def required_fields
%w[survey_url suggestion_text suggestion_link_text page_paths]
end
end
22 changes: 0 additions & 22 deletions app/presenters/content_item/recruitment_banner.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class ContentItemPresenter
include ContentItem::Withdrawable
include ContentItem::RecruitmentBanner

attr_reader :content_item,
:requested_path,
Expand Down
11 changes: 1 addition & 10 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@
<% end %>
<% end %>

<% if @content_item.recruitment_survey_url %>
<div class="govuk-!-static-margin-top-4">
<%= render "govuk_publishing_components/components/intervention", {
suggestion_text: "Help improve GOV.UK",
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)",
suggestion_link_url: @content_item.recruitment_survey_url,
new_tab: true,
} %>
</div>
<% end %>
<%= render partial: 'shared/intervention_banner' %>

<%= yield :header %>

Expand Down
10 changes: 10 additions & 0 deletions app/views/shared/_intervention_banner.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% if recruitment_banner.present? %>
<div class="govuk-width-container govuk-!-margin-top-4">
<%= render "govuk_publishing_components/components/intervention", {
new_tab: true,
suggestion_text: recruitment_banner["suggestion_text"],
suggestion_link_text: recruitment_banner["suggestion_link_text"],
suggestion_link_url: recruitment_banner["survey_url"],
} %>
</div>
<% end %>
80 changes: 80 additions & 0 deletions lib/data/recruitment_banners.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Example usage of adding a banner to the banners list:

# - name: Banner 1
# suggestion_text: "Help improve GOV.UK"
# suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
# survey_url: https://google.com
# page_paths:
# - /
# - /foreign-travel-advice

banners:
- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_CEST_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=577
page_paths:
- /guidance/check-employment-status-for-tax

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_Corp_Tax_trading_and_non_trading_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=578
page_paths:
- /guidance/corporation-tax-trading-and-non-trading

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_Fill_and_submit_VAT_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=579
page_paths:
- /guidance/how-to-fill-in-and-submit-your-vat-return-vat-notice-70012

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_Sign_in_BTA_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=580
page_paths:
- /guidance/sign-in-to-your-hmrc-business-tax-account

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_VAT_on_goods_and_services_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=581
page_paths:
- /guidance/rates-of-vat-on-different-goods-and-services

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_CIS_refund_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=582
page_paths:
- /guidance/claim-a-refund-of-construction-industry-scheme-deductions-if-youre-a-limited-company

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_IR35_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=583
page_paths:
- /guidance/understanding-off-payroll-working-ir35

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_Tax_reliefs_and_allowances_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=584
page_paths:
- /guidance/tax-reliefs-and-allowances-for-businesses-employers-and-the-self-employed

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_Corp_Tax_selling_or_closing_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=585
page_paths:
- /guidance/corporation-tax-selling-or-closing-your-company

- name: HMRC Team Banner
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
survey_url: https://signup.take-part-in-research.service.gov.uk/?utm_campaign=List_Expect_a_reply_TAD&utm_source=Other&utm_medium=gov.uk&t=HMRC&id=586
page_paths:
- /guidance/check-when-you-can-expect-a-reply-from-hmrc
14 changes: 14 additions & 0 deletions test/fixtures/recruitment_banners.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
banners:
- name: Banner 1
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Take part in user research"
survey_url: https://google.com
page_paths:
- /

- name: Banner 2
suggestion_text: "Help improve GOV.UK"
suggestion_link_text: "Take part in user research"
survey_url: https://google.com
page_paths:
- /some_path
51 changes: 51 additions & 0 deletions test/unit/helpers/recruitment_banner_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "test_helper"

class RecruitmentBannerHelperTest < ActionView::TestCase
include RecruitmentBannerHelper

def setup
@recruitment_banners_data = YAML.load_file(Rails.root.join("test/fixtures/recruitment_banners.yml"))
end

def request
OpenStruct.new(path: "/")
end

def recruitment_banners
@recruitment_banners_data["banners"]
end

test "recruitment_banner returns banners that include the current url" do
actual_banners = recruitment_banner

expected_banners =
{
"name" => "Banner 1",
"suggestion_text" => "Help improve GOV.UK",
"suggestion_link_text" => "Take part in user research",
"survey_url" => "https://google.com",
"page_paths" => ["/"],
}
assert_equal expected_banners, actual_banners
end

test "recruitment_banners yaml structure is valid" do
@recruitment_banners_data = YAML.load_file(Rails.root.join("lib/data/recruitment_banners.yml"))

if @recruitment_banners_data["banners"].present?
recruitment_banners.each do |banner|
assert banner.key?("suggestion_text"), "Banner is missing 'suggestion_text' key"
assert_not banner["suggestion_text"].blank?, "'suggestion_text' key should not be blank"

assert banner.key?("suggestion_link_text"), "Banner is missing 'suggestion_link_text' key"
assert_not banner["suggestion_link_text"].blank?, "'suggestion_link_text' key should not be blank"

assert banner.key?("survey_url"), "Banner is missing 'survey_url' key"
assert_not banner["survey_url"].blank?, "'survey_url' key should not be blank"

assert banner.key?("page_paths"), "Banner is missing 'page_paths' key"
assert_not banner["page_paths"].blank?, "'page_paths' key should not be blank"
end
end
end
end

0 comments on commit d99cbbb

Please sign in to comment.