Skip to content

Commit

Permalink
Merge pull request #8962 from alphagov/worldwide-organisation-pages-p…
Browse files Browse the repository at this point in the history
…ublishing

Worldwide organisation pages publishing
  • Loading branch information
jkempster34 authored Apr 18, 2024
2 parents 8157394 + 2f453ca commit b6dd9f3
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/models/editionable_worldwide_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@ def republish_dependent_documents
end

def associated_documents
(offices + offices.map(&:contact)).compact.flatten
[offices, offices.map(&:contact), pages].compact.flatten
end
end
47 changes: 46 additions & 1 deletion app/models/worldwide_organisation_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ class WorldwideOrganisationPage < ApplicationRecord
exclusion: { in: [CorporateInformationPageType::AboutUs.id], message: "Type cannot be `About us`" }
validate :unique_worldwide_organisation_and_page_type, on: :create, if: :edition

delegate :display_type_key, to: :corporate_information_page_type
delegate :slug, :display_type_key, to: :corporate_information_page_type

after_commit :republish_worldwide_organisation_draft
after_destroy :discard_draft

include HasContentId
include Attachable

def title(_locale = :en)
Expand All @@ -24,6 +28,23 @@ def corporate_information_page_type=(type)
self.corporate_information_page_type_id = type && type.id
end

def self.by_menu_heading(menu_heading)
type_ids = CorporateInformationPageType.by_menu_heading(menu_heading).map(&:id)
where(corporate_information_page_type_id: type_ids)
end

def self.for_slug(slug)
if (type = CorporateInformationPageType.find(slug))
find_by(corporate_information_page_type_id: type.id)
end
end

def self.for_slug!(slug)
if (type = CorporateInformationPageType.find(slug))
find_by!(corporate_information_page_type_id: type.id)
end
end

def publicly_visible?
true
end
Expand All @@ -32,8 +53,32 @@ def access_limited?
false
end

def publishing_api_presenter
PublishingApi::WorldwideOrganisationPagePresenter
end

def base_path
"#{edition.base_path}/about/#{slug}"
end

def public_path(options = {})
append_url_options(base_path, options)
end

def public_url(options = {})
Plek.website_root + public_path(options)
end

private

def republish_worldwide_organisation_draft
Whitehall.edition_services.draft_updater(edition).perform! if edition.present?
end

def discard_draft
PublishingApiDiscardDraftWorker.perform_async(content_id, I18n.default_locale.to_s)
end

def unique_worldwide_organisation_and_page_type
current_page_types = edition.pages.map(&:corporate_information_page_type_id).flatten
duplicate_page = current_page_types.include?(corporate_information_page_type_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def content
crest: "single-identity",
formatted_title: worldwide_organisation_logo_name(item),
},
ordered_corporate_information_pages:,
secondary_corporate_information_pages:,
office_contact_associations:,
people_role_associations:,
social_media_links:,
Expand Down Expand Up @@ -56,6 +58,7 @@ def edition_links
secondary_role_person:,
sponsoring_organisations: item.lead_organisations.map(&:content_id),
world_locations: item.world_locations.map(&:content_id),
corporate_information_pages: pages,
}
end

Expand Down Expand Up @@ -155,5 +158,78 @@ def world_location_names
}
end
end

def pages
return [] unless item.pages.any?

item.pages.map(&:content_id)
end

def ordered_corporate_information_pages
return [] unless item.pages

links = []

%i[our_information jobs_and_contracts].each do |page_type|
item.pages.by_menu_heading(page_type).each do |page|
links << {
content_id: page.content_id,
title: page.title,
}
end
end

links
end

def secondary_corporate_information_pages
return [] unless item.pages

sentences = []

publication_scheme = item.pages.find_by(corporate_information_page_type_id: CorporateInformationPageType::PublicationScheme.id)
if publication_scheme.present?
sentences << I18n.t(
"worldwide_organisation.corporate_information.publication_scheme_html",
link: t_corporate_information_page_link(item, "publication-scheme"),
)
end

welsh_language_scheme = item.pages.find_by(corporate_information_page_type_id: CorporateInformationPageType::WelshLanguageScheme.id)
if welsh_language_scheme.present?
sentences << I18n.t(
"worldwide_organisation.corporate_information.welsh_language_scheme_html",
link: t_corporate_information_page_link(item, "welsh-language-scheme"),
)
end

personal_information_charter = item.pages.find_by(corporate_information_page_type_id: CorporateInformationPageType::PersonalInformationCharter.id)
if personal_information_charter.present?
sentences << I18n.t(
"worldwide_organisation.corporate_information.personal_information_charter_html",
link: t_corporate_information_page_link(item, "personal-information-charter"),
)
end

sentences.join(" ")
end

def t_corporate_information_page_link(organisation, slug)
page = organisation.pages.for_slug(slug)
page.extend(UseSlugAsParam)
link_to(
t_corporate_information_page_type_link_text(page),
page.public_path,
class: "govuk-link",
)
end

def t_corporate_information_page_type_link_text(page)
if I18n.exists?("corporate_information_page.type.link_text.#{page.display_type_key}")
I18n.t("corporate_information_page.type.link_text.#{page.display_type_key}")
else
I18n.t("corporate_information_page.type.title.#{page.display_type_key}")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module PublishingApi
class WorldwideOrganisationPagePresenter
attr_accessor :item, :update_type

def initialize(item, update_type: nil)
self.item = item
self.update_type = update_type || "major"
end

delegate :content_id, to: :item

def content
content = BaseItemPresenter.new(
item,
title: item.title,
update_type:,
).base_attributes

content.merge!(
details: {
body: Whitehall::GovspeakRenderer.new.govspeak_with_attachments_to_html(item.body, item.attachments),
},
description: item.summary,
public_updated_at: item.updated_at,
rendering_app: Whitehall::RenderingApp::GOVERNMENT_FRONTEND,
schema_name: "worldwide_corporate_information_page",
document_type:,
links: edition_links,
)

content.merge!(PayloadBuilder::PolymorphicPath.for(item))
end

def edition_links
{
parent: [item.edition.content_id],
worldwide_organisation: [item.edition.content_id],
}
end

def links
{}
end

def document_type
item.display_type_key
end
end
end
16 changes: 14 additions & 2 deletions test/factories/editionable_worldwide_organisations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,21 @@
end
end

trait(:with_page) do
after :create do |organisation, _evaluator|
organisation.pages = [build(:worldwide_organisation_page)]
end
end

trait(:with_pages) do
after :create do |organisation|
create(:worldwide_organisation_page, edition: organisation)
after :create do |organisation, _evaluator|
organisation.pages = [
build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::ComplaintsProcedure, edition: organisation),
build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::PersonalInformationCharter, edition: organisation),
build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::PublicationScheme, edition: organisation),
build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::Recruitment, edition: organisation),
build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::WelshLanguageScheme, edition: organisation),
]
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/factories/worldwide_organisation_pages.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FactoryBot.define do
factory :worldwide_organisation_page do
content_id { SecureRandom.uuid }
summary { "Some summary" }
body { "Some body" }
corporate_information_page_type_id { CorporateInformationPageType::PublicationScheme.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class EditionableWorldwideOrganisationTest < ActiveSupport::TestCase
published_worldwide_organisation = create(
:editionable_worldwide_organisation,
:published,
:with_pages,
:with_page,
)

draft_worldwide_organisation = published_worldwide_organisation.create_draft(create(:writer))
Expand Down
28 changes: 28 additions & 0 deletions test/unit/app/models/worldwide_organisation_page_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
require "test_helper"

class WorldwideOrganisationPageTest < ActiveSupport::TestCase
test "creating a new page republishes the associated worldwide organisation" do
worldwide_organisation = create(:editionable_worldwide_organisation)
Whitehall::PublishingApi.expects(:save_draft).with(worldwide_organisation).once
create(:worldwide_organisation_page, edition: worldwide_organisation)
end

test "updating an existing page republishes the associated worldwide organisation" do
worldwide_organisation = create(:editionable_worldwide_organisation)
page = create(:worldwide_organisation_page, edition: worldwide_organisation)
page.body = "updated"
Whitehall::PublishingApi.expects(:save_draft).with(worldwide_organisation).once
page.save!
end

test "deleting a page republishes the associated worldwide organisation" do
worldwide_organisation = create(:editionable_worldwide_organisation)
page = create(:worldwide_organisation_page, edition: worldwide_organisation)
Whitehall::PublishingApi.expects(:save_draft).with(worldwide_organisation).once
page.destroy!
end

test "deleting a page discards the draft page" do
worldwide_organisation = create(:editionable_worldwide_organisation)
page = create(:worldwide_organisation_page, edition: worldwide_organisation)
PublishingApiDiscardDraftWorker.expects(:perform_async).with(page.content_id, "en").once
page.destroy!
end

%w[body corporate_information_page_type_id].each do |param|
test "should not be valid without a #{param}" do
assert_not build(:worldwide_organisation_page, param.to_sym => nil).valid?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def present(...)
:with_social_media_account,
:with_main_office,
:with_home_page_offices,
:with_pages,
analytics_identifier: "WO123")

primary_role = create(:ambassador_role)
Expand Down Expand Up @@ -47,6 +48,17 @@ def present(...)
crest: "single-identity",
formatted_title: "Editionable<br/>worldwide<br/>organisation<br/>title",
},
ordered_corporate_information_pages: [
{
content_id: worldwide_org.pages[0].content_id,
title: "Complaints procedure",
},
{
content_id: worldwide_org.pages[3].content_id,
title: "Working for Editionable worldwide organisation title",
},
],
secondary_corporate_information_pages: "Read about the types of information we routinely publish in our <a class=\"govuk-link\" href=\"/editionable-world/organisations/editionable-worldwide-organisation-title/about/publication-scheme\">Publication scheme</a>. Find out about our commitment to <a class=\"govuk-link\" href=\"/editionable-world/organisations/editionable-worldwide-organisation-title/about/welsh-language-scheme\">publishing in Welsh</a>. Our <a class=\"govuk-link\" href=\"/editionable-world/organisations/editionable-worldwide-organisation-title/about/personal-information-charter\">Personal information charter</a> explains how we treat your personal information.",
office_contact_associations: [
{
office_content_id: worldwide_org.reload.main_office.content_id,
Expand Down Expand Up @@ -115,6 +127,7 @@ def present(...)
],
sponsoring_organisations: worldwide_org.organisations.map(&:content_id),
world_locations: worldwide_org.world_locations.map(&:content_id),
corporate_information_pages: worldwide_org.pages.map(&:content_id),
},
analytics_identifier: "WO123",
update_type: "major",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "test_helper"

module PublishingApi::WorldwideOrganisationPagePresenterTest
class TestCase < ActiveSupport::TestCase
attr_accessor :page, :update_type

def presented_page
PublishingApi::WorldwideOrganisationPagePresenter.new(
page,
update_type:,
)
end

class BasicWorldwideOrganisationPageTest < TestCase
setup do
self.page = create(:worldwide_organisation_page)
end

test "presents a Worldwide Organisation Page ready for adding to the publishing API" do
public_path = page.public_path

expected_hash = {
base_path: public_path,
title: page.title,
schema_name: "worldwide_corporate_information_page",
document_type: page.display_type_key,
locale: "en",
publishing_app: Whitehall::PublishingApp::WHITEHALL,
rendering_app: Whitehall::RenderingApp::GOVERNMENT_FRONTEND,
public_updated_at: page.updated_at,
routes: [{ path: public_path, type: "exact" }],
redirects: [],
description: "Some summary",
details: {
body: "<div class=\"govspeak\"><p>Some body</p>\n</div>",
},
update_type: "major",
links: {
parent: [page.edition.content_id],
worldwide_organisation: [page.edition.content_id],
},
}

expected_links = {
parent: [
page.edition.content_id,
],
worldwide_organisation: [
page.edition.content_id,
],
}

presented_item = presented_page

assert_equal expected_hash, presented_item.content
assert_hash_includes presented_item.edition_links, expected_links
assert_equal "major", presented_item.update_type
assert_equal page.content_id, presented_item.content_id

assert_valid_against_publisher_schema(presented_item.content, "worldwide_corporate_information_page")
assert_valid_against_links_schema({ links: presented_item.edition_links }, "worldwide_corporate_information_page")
end
end
end
end
Loading

0 comments on commit b6dd9f3

Please sign in to comment.