Skip to content

Commit

Permalink
Merge pull request #3095 from alphagov/use-ww-office-multipart
Browse files Browse the repository at this point in the history
Add support for worldwide offices embedded in worldwide organisations
  • Loading branch information
brucebolt authored Feb 20, 2024
2 parents 1e3f37e + 0c88e11 commit dd69bf5
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 20 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 @@ -177,6 +177,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
24 changes: 9 additions & 15 deletions app/presenters/worldwide_organisation_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ def people_in_non_primary_roles
end

def main_office
return unless (office_item = content_item.dig("links", "main_office")&.first)
return unless (office_item = content_item.dig("details", "main_office_parts")&.first)

office_contact_item = contact_for_office(office_item["content_id"])
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.dig("details", "access_and_opening_times").present?,
public_url: office_item.fetch("web_url"),
has_access_and_opening_times?: office_item["access_and_opening_times"].present?,
public_url: "#{content_item['base_path']}/#{office_item['slug']}",
)
end

WorldwideOffice = Struct.new(:contact, :has_access_and_opening_times?, :public_url, keyword_init: true)

def home_page_offices
return [] unless content_item.dig("links", "home_page_offices")
return [] unless content_item.dig("details", "home_page_office_parts")

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

WorldwideOrganisation::LinkedContactPresenter.new(contact)
Expand Down Expand Up @@ -131,15 +131,9 @@ def sponsoring_organisations

private

def contact_for_office(office_content_id)
contact_mapping = content_item.dig("details", "office_contact_associations").select { |office_contact_association|
office_contact_association["office_content_id"] == office_content_id
}.first

return unless contact_mapping

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

Expand Down
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 @@ -108,7 +108,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 @@ -117,15 +117,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
4 changes: 2 additions & 2 deletions test/presenters/worldwide_organisation_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def schema_name

test "#main_office returns nil when there is no main office" do
without_main_office = schema_item
without_main_office["links"].delete("main_office")
without_main_office["details"].delete("main_office_parts")

presented = create_presenter(WorldwideOrganisationPresenter, content_item: without_main_office)

Expand All @@ -166,7 +166,7 @@ def schema_name

test "#home_page_offices returns an empty array when there are no home page offices" do
without_home_page_offices = schema_item
without_home_page_offices["links"].delete("home_page_offices")
without_home_page_offices["details"].delete("home_page_office_parts")

presented = create_presenter(WorldwideOrganisationPresenter, content_item: without_home_page_offices)

Expand Down

0 comments on commit dd69bf5

Please sign in to comment.