Skip to content

Commit

Permalink
Link to individual offices from worldwide organisation
Browse files Browse the repository at this point in the history
Home page offices should link to their individual worldwide office pages if the
office has access and opening times.
  • Loading branch information
jkempster34 committed Feb 22, 2024
1 parent ba9910c commit 4edc831
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
20 changes: 12 additions & 8 deletions app/presenters/worldwide_organisation_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,23 +84,17 @@ 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")

content_item.dig("details", "home_page_office_parts").map { |office|
contact = linked_contact(office["contact_content_id"])
next unless contact

WorldwideOrganisation::LinkedContactPresenter.new(contact)
office(office, contact)
}.compact
end

Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions app/views/content_items/worldwide_organisation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@
<% end %>
</div>

<% @content_item.home_page_offices.each do |contact| %>
<% @content_item.home_page_offices.each do |office| %>
<hr class="govuk-section-break govuk-section-break--visible">

<div class="govuk-!-padding-top-6 govuk-!-padding-bottom-6 govuk-clearfix">
<%= 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 %>
</div>
<% end %>
</section>
Expand Down
51 changes: 50 additions & 1 deletion test/integration/worldwide_organisation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4edc831

Please sign in to comment.