From 4edc83106b769e206ab321c2345555b8d57be60c Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Thu, 22 Feb 2024 17:20:44 +0000 Subject: [PATCH] Link to individual offices from worldwide organisation Home page offices should link to their individual worldwide office pages if the office has access and opening times. --- .../worldwide_organisation_presenter.rb | 20 +++++--- .../worldwide_organisation.html.erb | 8 ++- .../worldwide_organisation_test.rb | 51 ++++++++++++++++++- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/app/presenters/worldwide_organisation_presenter.rb b/app/presenters/worldwide_organisation_presenter.rb index 3cec48a3d..102451428 100644 --- a/app/presenters/worldwide_organisation_presenter.rb +++ b/app/presenters/worldwide_organisation_presenter.rb @@ -3,6 +3,8 @@ class WorldwideOrganisationPresenter < ContentItemPresenter include WorldwideOrganisation::Branding include ActionView::Helpers::UrlHelper + WorldwideOffice = Struct.new(:contact, :has_access_and_opening_times?, :public_url, keyword_init: true) + def formatted_title content_item.dig("details", "logo", "formatted_title") end @@ -82,15 +84,9 @@ def main_office office_contact_item = linked_contact(office_item["contact_content_id"]) return unless office_contact_item - WorldwideOffice.new( - contact: WorldwideOrganisation::LinkedContactPresenter.new(office_contact_item), - has_access_and_opening_times?: office_item["access_and_opening_times"].present?, - public_url: "#{content_item['base_path'].gsub(/\..*?$/, '')}/#{office_item['slug']}", - ) + office(office_item, office_contact_item) end - WorldwideOffice = Struct.new(:contact, :has_access_and_opening_times?, :public_url, keyword_init: true) - def home_page_offices return [] unless content_item.dig("details", "home_page_office_parts") @@ -98,7 +94,7 @@ def home_page_offices contact = linked_contact(office["contact_content_id"]) next unless contact - WorldwideOrganisation::LinkedContactPresenter.new(contact) + office(office, contact) }.compact end @@ -131,6 +127,14 @@ def sponsoring_organisations private + def office(office, contact) + WorldwideOffice.new( + contact: WorldwideOrganisation::LinkedContactPresenter.new(contact), + has_access_and_opening_times?: office["access_and_opening_times"].present?, + public_url: "#{content_item['base_path'].gsub(/\..*?$/, '')}/#{office['slug']}", + ) + end + def linked_contact(contact_content_id) content_item.dig("links", "contacts").select { |contact| contact["content_id"] == contact_content_id diff --git a/app/views/content_items/worldwide_organisation.html.erb b/app/views/content_items/worldwide_organisation.html.erb index c12b0ecb4..405ca9215 100644 --- a/app/views/content_items/worldwide_organisation.html.erb +++ b/app/views/content_items/worldwide_organisation.html.erb @@ -91,13 +91,17 @@ <% end %> - <% @content_item.home_page_offices.each do |contact| %> + <% @content_item.home_page_offices.each do |office| %>
<%= render partial: "content_items/worldwide_organisation/contact", locals: { - contact: contact, + contact: office.contact, } %> + + <% if office.has_access_and_opening_times? %> + <%= link_to t("contact.access_and_opening_times"), office.public_url, class: "govuk-link", lang: @content_item.locale %> + <% end %>
<% end %> diff --git a/test/integration/worldwide_organisation_test.rb b/test/integration/worldwide_organisation_test.rb index 4c271babd..c181bfd3a 100644 --- a/test/integration/worldwide_organisation_test.rb +++ b/test/integration/worldwide_organisation_test.rb @@ -112,9 +112,58 @@ class WorldwideOrganisationTest < ActionDispatch::IntegrationTest end end - test "renders the home page offices without a link to the office page" do + test "renders the main office contact without a link to the office page when the office has no access details" do + setup_and_visit_content_item( + "worldwide_organisation", + { + "details" => + { "main_office_parts" => + [ + { + "access_and_opening_times": nil, + "contact_content_id": "410c4c3b-5c1c-4617-b603-4356bedcc85e", + "slug": "office/british-embassy", + "title": "British Embassy", + "type": "Embassy", + }, + ] }, + }, + ) + + within("#contact-us") do + assert page.has_text?("Contact us") + assert page.has_content?("Torre Emperador Castellana") + assert_not page.has_link?(I18n.t("contact.access_and_opening_times"), href: "/world/uk-embassy-in-country/office/british-embassy") + end + end + + test "renders the home page offices with a link to the office page" do setup_and_visit_content_item("worldwide_organisation") + within("#contact-us") do + assert page.has_content?("Department for Business and Trade Dusseldorf") + assert page.has_link?(I18n.t("contact.access_and_opening_times"), href: "/world/uk-embassy-in-country/office/uk-trade-investment-duesseldorf") + end + end + + test "renders the home page offices without a link to the office page when the office has no access details" do + setup_and_visit_content_item( + "worldwide_organisation", + { + "details" => + { "home_page_office_parts" => + [ + { + "access_and_opening_times": nil, + "contact_content_id": "53df7197-901c-48fc-b9b4-ed649903f1f0", + "slug": "office/uk-trade-investment-duesseldorf", + "title": "Department for Business and Trade Dusseldorf", + "type": "Department for Business and Trade Office", + }, + ] }, + }, + ) + within("#contact-us") do assert page.has_content?("Department for Business and Trade Dusseldorf") assert_not page.has_link?(I18n.t("contact.access_and_opening_times"), href: "/world/uk-embassy-in-country/office/uk-trade-investment-duesseldorf")