Skip to content

Commit

Permalink
Add presenter for worldwide corporate information pages
Browse files Browse the repository at this point in the history
We have decided to publish the Corporate Information Pages associated with
Worldwide Organisations as a separate content item
(alphagov/publishing-api#2399) for the following
reasons:
- We require a link to the associated worldwide_organisation. Having a new
  content item means that we don't have to attach multiple optional links to the
  corporate information page content item.
- We will need a new template for this page in government-frontend as the
  worldwide organisation corporate information pages are quite different when
  compared to the organisation corporate information pages. Because
  government_frontend maps content items to templates through their
  schema_name (except for a few exceptions), it would make sense to follow this
  pattern.
- Many of the attributes of the corporate information page schema are not
  needed in order to render the worldwide organisation corporate informaiton
  pages.

This adds a presenter for the new content item. Any shared attributes are DRYed
up through a shared PayloadBuilder.
  • Loading branch information
jkempster34 authored and syed-ali-tw committed Jun 28, 2023
1 parent b6b1827 commit 3eb7594
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ def content
.new(corporate_information_page, update_type:)
.base_attributes
.merge(PayloadBuilder::PublicDocumentPath.for(corporate_information_page))
.merge(
description: corporate_information_page.summary,
.merge(PayloadBuilder::CorporateInformationPage.for(corporate_information_page))
.deep_merge(
details:,
document_type:,
public_updated_at:,
rendering_app: corporate_information_page.rendering_app,
schema_name: SCHEMA_NAME,
links: edition_links,
auth_bypass_ids: [corporate_information_page.auth_bypass_id],
Expand Down Expand Up @@ -59,21 +57,8 @@ def document_type

delegate :display_type_key, to: :corporate_information_page

def base_details
{
body:,
}
end

def body
Whitehall::GovspeakRenderer
.new
.govspeak_edition_to_html(corporate_information_page)
end

def details
base_details
.merge(change_history)
change_history
.merge(CorporateInformationGroups.for(corporate_information_page))
.merge(Organisation.for(corporate_information_page))
.merge(PayloadBuilder::TagDetails.for(corporate_information_page))
Expand All @@ -84,13 +69,6 @@ def links_presenter
@links_presenter ||= LinksPresenter.new(corporate_information_page)
end

def public_updated_at
public_updated_at = corporate_information_page.public_timestamp ||
corporate_information_page.updated_at

public_updated_at.rfc3339
end

def change_history
return {} if corporate_information_page.change_history.blank?

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module PublishingApi
module PayloadBuilder
class CorporateInformationPage
attr_reader :item

def self.for(item)
new(item).call
end

def initialize(item)
@item = item
end

def call
{
description: item.summary,
rendering_app: item.rendering_app,
public_updated_at:,
details: {
body:,
},
}
end

private

def public_updated_at
public_updated_at = item.public_timestamp ||
item.updated_at

public_updated_at.rfc3339
end

def body
Whitehall::GovspeakRenderer
.new
.govspeak_edition_to_html(item)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module PublishingApi
class WorldwideCorporateInformationPagePresenter
include UpdateTypeHelper

attr_accessor :item, :update_type

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

delegate :content_id, to: :item

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

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

content.merge!(
document_type: item.display_type_key,
schema_name: "worldwide_corporate_information_page",
)
end

def links
{
parent: [item.worldwide_organisation.content_id],
worldwide_organisation: [item.worldwide_organisation.content_id],
}
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require "test_helper"

module PublishingApi::WorldwideCorporateInformationPagePresenterTest
class TestCase < ActiveSupport::TestCase
attr_accessor :corporate_information_page, :update_type

def presented_corporate_information_page
PublishingApi::WorldwideCorporateInformationPagePresenter.new(
corporate_information_page,
update_type:,
)
end

class BasicCorporateInformationPageTest < TestCase
setup do
worldwide_organisation = create(:worldwide_organisation)
self.corporate_information_page = create(:corporate_information_page, worldwide_organisation:, organisation: nil)
end

test "presents a Worldwide Corporate Information Page ready for adding to the publishing API" do
public_path = corporate_information_page.public_path

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

expected_links = {
parent: [
corporate_information_page.owning_organisation.content_id,
],
worldwide_organisation: [
corporate_information_page.owning_organisation.content_id,
],
}

presented_item = presented_corporate_information_page

assert_equal expected_hash, presented_item.content
assert_hash_includes presented_item.links, expected_links
assert_equal "major", presented_item.update_type
assert_equal corporate_information_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.links }, "worldwide_corporate_information_page")
end
end

class CorporateInformationPageWithMinorChange < TestCase
setup do
self.corporate_information_page = create(
:corporate_information_page,
minor_change: true,
)
end

test "update type" do
assert_equal "minor", presented_corporate_information_page.update_type
end
end

class CorporateInformationPageWithoutMinorChange < TestCase
setup do
self.corporate_information_page = create(
:corporate_information_page,
minor_change: false,
)
end

test "update type" do
assert_equal "major", presented_corporate_information_page.update_type
end
end
end
end
4 changes: 2 additions & 2 deletions test/unit/presenters/publishing_api_presenters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class PublishingApiPresentersTest < ActiveSupport::TestCase
)
end

test ".presenter_for returns a GenericEditionPresenter for a " \
test ".presenter_for returns a GenericEditionPresenter for an " \
"CorporateInformationPage belonging to an WorldwideOrganisation" do
presenter = PublishingApiPresenters
.presenter_for(
Expand All @@ -155,7 +155,7 @@ class PublishingApiPresentersTest < ActiveSupport::TestCase
)

assert_equal(
PublishingApi::GenericEditionPresenter,
PublishingApi::WorldwideCorporateInformationPagePresenter,
presenter.class,
)
end
Expand Down

0 comments on commit 3eb7594

Please sign in to comment.