Skip to content

Commit

Permalink
Merge pull request #8640 from alphagov/editionable-world-orgs-presenter
Browse files Browse the repository at this point in the history
Present basic EditionableWorldwideOrgs
  • Loading branch information
brucebolt authored Dec 22, 2023
2 parents 0aff83d + a1b03ec commit 3180198
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/editionable_worldwide_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ class EditionableWorldwideOrganisation < Edition
def display_type_key
"editionable_worldwide_organisation"
end

def publishing_api_presenter
PublishingApi::EditionableWorldwideOrganisationPresenter
end

def base_path
"/editionable-world/organisations/#{slug}"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module PublishingApi
class EditionableWorldwideOrganisationPresenter
include Rails.application.routes.url_helpers
include ActionView::Helpers::UrlHelper

attr_accessor :item, :update_type, :state

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

delegate :content_id, to: :item

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

content.merge!(
details: {
logo: {
crest: "single-identity",
},
},
document_type: item.class.name.underscore,
public_updated_at: item.updated_at,
rendering_app: Whitehall::RenderingApp::GOVERNMENT_FRONTEND,
schema_name: "worldwide_organisation",
)
content.merge!(PayloadBuilder::PolymorphicPath.for(item))
end

def links
{
sponsoring_organisations: item.organisations.map(&:content_id),
}
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "test_helper"

class PublishingApi::EditionableWorldwideOrganisationPresenterTest < ActiveSupport::TestCase
def present(...)
PublishingApi::EditionableWorldwideOrganisationPresenter.new(...)
end

test "presents a Worldwide Organisation ready for adding to the publishing API" do
worldwide_org = create(:editionable_worldwide_organisation)

public_path = worldwide_org.public_path

expected_hash = {
base_path: public_path,
title: worldwide_org.title,
schema_name: "worldwide_organisation",
document_type: "editionable_worldwide_organisation",
locale: "en",
publishing_app: Whitehall::PublishingApp::WHITEHALL,
rendering_app: Whitehall::RenderingApp::GOVERNMENT_FRONTEND,
public_updated_at: worldwide_org.updated_at,
routes: [{ path: public_path, type: "exact" }],
redirects: [],
details: {
logo: {
crest: "single-identity",
},
},
update_type: "major",
}

expected_links = {
sponsoring_organisations: worldwide_org.organisations.map(&:content_id),
}

presented_item = present(worldwide_org)

assert_equal expected_hash, presented_item.content
assert_equal "major", presented_item.update_type
assert_equal worldwide_org.content_id, presented_item.content_id

# TODO: uncomment the below assertion when the editionable_worldwide_organisation model is
# finished and all content can be added to this presenter.
# assert_valid_against_publisher_schema(presented_item.content, "worldwide_organisation")

assert_equal expected_links, presented_item.links
assert_valid_against_links_schema({ links: presented_item.links }, "worldwide_organisation")
end
end

0 comments on commit 3180198

Please sign in to comment.