Skip to content

Commit

Permalink
Add support for worldwide offices embedded in worldwide organisations
Browse files Browse the repository at this point in the history
As part of the switch to Worldwide Organisation content items containing
the content for their offices, we need to switch the rendering of the
office pages to use these parts.

This makes that switch in a backward compatible way, so we can continue
rendering office pages under the existing content items until the office
routes are updated to point to the organisation's content item.

In a later PR, we will remove `WorldwideOfficePresenter` and it's
associated view.
  • Loading branch information
brucebolt committed Feb 19, 2024
1 parent 89e7756 commit 0c88e11
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 3 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 @@ -178,6 +178,7 @@ def content_item_template
return "guide_single" if @content_item.render_guide_as_single_page?
return "manual_updates" if @content_item.manual_updates?
return "hmrc_manual_updates" if @content_item.hmrc_manual_updates?
return "worldwide_organisation_office" if @content_item.worldwide_organisation_office?

@content_item.schema_name
end
Expand Down
4 changes: 4 additions & 0 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def hmrc_manual_updates?
view_context.request.path =~ /^\/hmrc-internal-manuals\/.*\/updates$/ && content_item["schema_name"] == "hmrc_manual"
end

def worldwide_organisation_office?
view_context.request.path =~ /^\/world\/.*\/office\/.*$/ && content_item["schema_name"] == "worldwide_organisation"
end

def show_default_breadcrumbs?
true
end
Expand Down
46 changes: 46 additions & 0 deletions app/presenters/worldwide_organisation_office_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class WorldwideOrganisationOfficePresenter < ContentItemPresenter
include ContentItem::ContentsList
include WorldwideOrganisation::Branding

def formatted_title
worldwide_organisation&.formatted_title
end

def body
office["access_and_opening_times"]
end

def contact
associated_contact = content_item.dig("links", "contacts").select { |contact|
contact["content_id"] == office["contact_content_id"]
}.first

WorldwideOrganisation::LinkedContactPresenter.new(associated_contact)
end

def show_default_breadcrumbs?
false
end

def office
all_offices = content_item.dig("details", "main_office_parts") + content_item.dig("details", "home_page_office_parts")

all_offices.select { |office|
office["slug"] == requested_path.gsub("#{content_item['base_path']}/", "")
}.first
end

def worldwide_organisation
WorldwideOrganisationPresenter.new(content_item, requested_path, view_context)
end

def sponsoring_organisations
worldwide_organisation&.sponsoring_organisations
end

private

def show_contents_list?
true
end
end
5 changes: 5 additions & 0 deletions app/services/presenter_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def presenter_name
return service_sign_in_presenter_name if service_sign_in_format?
return "ManualUpdatesPresenter" if manual_updates?
return "HmrcManualUpdatesPresenter" if hmrc_manual_updates?
return "WorldwideOrganisationOfficePresenter" if worldwide_organisation_office?

"#{content_item['schema_name'].classify}Presenter"
end
Expand All @@ -49,6 +50,10 @@ def hmrc_manual_updates?
view_context.request.path =~ /^\/hmrc-internal-manuals\/.*\/updates$/ && content_item["schema_name"] == "hmrc_manual"
end

def worldwide_organisation_office?
view_context.request.path =~ /^\/world\/.*\/office\/.*$/ && content_item["schema_name"] == "worldwide_organisation"
end

def service_sign_in_format?
content_item["schema_name"] == "service_sign_in"
end
Expand Down
35 changes: 35 additions & 0 deletions app/views/content_items/worldwide_organisation_office.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%= render partial: "content_items/worldwide_organisation/header", locals: {
worldwide_organisation: @content_item.worldwide_organisation,
} %>

<article class="govuk-grid-row">
<div class="govuk-grid-column-one-third">
<%= render "govuk_publishing_components/components/contents_list",
contents: @content_item.contents,
underline_links: true
%>
</div>

<div class="govuk-grid-column-two-thirds">
<div class="body">
<%= render "govuk_publishing_components/components/heading", {
text: sanitize("<span class='govuk-visually-hidden'>About </span>#{@content_item.contact.title}"),
heading_level: 2,
font_size: "xl",
margin_bottom: 4,
} %>

<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: @content_item.contact,
hide_title: true,
} %>
</div>

<%= render "govuk_publishing_components/components/govspeak", {} do
raw(@content_item.body)
end %>
</div>
</div>
</article>
6 changes: 3 additions & 3 deletions test/integration/worldwide_organisation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class WorldwideOrganisationTest < ActionDispatch::IntegrationTest
within("#contact-us") do
assert page.has_text?("Contact us")
assert page.has_content?("Torre Emperador Castellana")
assert page.has_link?(I18n.t("contact.access_and_opening_times"), href: "https://www.integration.publishing.service.gov.uk/world/organisations/british-embassy-madrid/office/british-embassy")
assert page.has_link?(I18n.t("contact.access_and_opening_times"), href: "/world/uk-embassy-in-country/office/british-embassy")
end
end

Expand All @@ -116,15 +116,15 @@ class WorldwideOrganisationTest < ActionDispatch::IntegrationTest

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: "https://www.integration.publishing.service.gov.uk/world/organisations/department-for-business-and-trade-germany/office/uk-trade-investment-duesseldorf")
assert_not page.has_link?(I18n.t("contact.access_and_opening_times"), href: "/world/uk-embassy-in-country/office/uk-trade-investment-duesseldorf")
end
end

test "does not render the contacts section if there is no main office" do
setup_and_visit_content_item(
"worldwide_organisation",
{
"links" => { "main_office" => nil },
"details" => { "main_office_parts" => nil },
},
)
assert_not page.has_text?("Contact us")
Expand Down
70 changes: 70 additions & 0 deletions test/presenters/worldwide_organisation_office_presenter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require "presenter_test_helper"

class WorldwideOrganisationOfficePresenterTest < PresenterTestCase
def schema_name
"worldwide_organisation"
end

def requested_path
"#{schema_item['base_path']}/#{schema_item.dig('details', 'main_office_parts', 0, 'slug')}"
end

def present_example(example)
create_presenter(
"WorldwideOrganisationOfficePresenter".safe_constantize,
content_item: example,
requested_path:,
)
end

test "#title returns the title of the schema item" do
assert_equal schema_item["title"], presented_item.title
end

test "#body returns the access and opening times of the schema item" do
assert_equal schema_item.dig("details", "main_office_parts", 0, "access_and_opening_times"), presented_item.body
end

test "#contact returns the contact as an instance of #{WorldwideOrganisation::LinkedContactPresenter}" do
assert presented_item.contact.is_a?(WorldwideOrganisation::LinkedContactPresenter)
end

test "#sponsoring_organisation_logo returns the logo details of the item" do
with_non_default_crest = schema_item
sponsoring_organisation = first_sponsoring_organisation(with_non_default_crest)
sponsoring_organisation["details"]["logo"]["crest"] = "dbt"

presented = present_example(with_non_default_crest)

expected = { name: "British Deputy High Commission<br/>Hyderabad", url: "/world/uk-embassy-in-country", crest: "dbt", brand: "foreign-commonwealth-development-office" }
assert_equal expected, presented.organisation_logo
end

test "#sponsoring_organisation_logo returns default values when the crest and brand of the first sponsoring organisation are blank" do
with_empty_logo = schema_item
sponsoring_organisation = first_sponsoring_organisation(with_empty_logo)
sponsoring_organisation["details"]["logo"]["crest"] = nil
sponsoring_organisation["details"]["brand"] = nil

presented = present_example(with_empty_logo)

expected = { name: "British Deputy High Commission<br/>Hyderabad", url: "/world/uk-embassy-in-country", crest: "single-identity", brand: "single-identity" }
assert_equal expected, presented.organisation_logo
end

test "#sponsoring_organisation_logo returns default values when the sponsoring organisations are nil" do
without_sponsoring_organisations = schema_item
without_sponsoring_organisations["links"].delete("sponsoring_organisations")

presented = present_example(without_sponsoring_organisations)

expected = { name: "British Deputy High Commission<br/>Hyderabad", url: "/world/uk-embassy-in-country", crest: "single-identity", brand: "single-identity" }
assert_equal expected, presented.organisation_logo
end

private

def first_sponsoring_organisation(item)
item["links"]["sponsoring_organisations"][0]
end
end

0 comments on commit 0c88e11

Please sign in to comment.