-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add presenter for worldwide corporate information pages
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
1 parent
b6b1827
commit 3eb7594
Showing
5 changed files
with
171 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/presenters/publishing_api/payload_builder/corporate_information_page.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
37 changes: 37 additions & 0 deletions
37
app/presenters/publishing_api/worldwide_corporate_information_page_presenter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
88 changes: 88 additions & 0 deletions
88
test/unit/presenters/publishing_api/worldwide_corporate_information_page_presenter_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters