Skip to content

Commit

Permalink
Add a .for_content_id method to PreviewContent
Browse files Browse the repository at this point in the history
This matches the approach we’ve agreed to take to limit
the number of service objects.
  • Loading branch information
pezholio committed Jan 10, 2025
1 parent 0e09a2f commit e730288
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
module ContentBlockManager
class PreviewContent < Data.define(:title, :html, :instances_count)
class << self
def for_content_id(content_id:, content_block_edition:, base_path: nil)
content_item = Services.publishing_api.get_content(content_id).parsed_content
metadata = Services.publishing_api.get_host_content_item_for_content_id(
content_block_edition.document.content_id,
content_id,
).parsed_content
html = ContentBlockManager::GeneratePreviewHtml.new(
content_id:,
content_block_edition:,
base_path: base_path || content_item["base_path"],
).call

ContentBlockManager::PreviewContent.new(
title: content_item["title"],
html:,
instances_count: metadata["instances"],
)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,62 @@ class ContentBlockManager::PreviewContentTest < ActiveSupport::TestCase
assert_equal preview_content.html, html
assert_equal preview_content.instances_count, instances_count
end

describe ".for_content_id" do
let(:host_content_id) { SecureRandom.uuid }
let(:preview_content_id) { SecureRandom.uuid }
let(:host_title) { "Test" }
let(:host_base_path) { "/test" }
let(:document) do
build(:content_block_document, :email_address, content_id: preview_content_id)
end
let(:block_to_preview) do
build(:content_block_edition, :email_address, document:, details: { "email_address" => "[email protected]" }, id: 1)
end
let(:metadata_response) do
stub(:response, parsed_content: { "instances" => 2 })
end
let(:preview_response) { stub(:preview_response, call: html) }
let(:html) { "SOME_HTML" }

setup do
stub_publishing_api_has_item(content_id: host_content_id, title: host_title, base_path: host_base_path)
Services.publishing_api.expects(:get_host_content_item_for_content_id)
.with(block_to_preview.document.content_id, host_content_id)
.returns(metadata_response)
end

it "returns the title of host document" do
ContentBlockManager::GeneratePreviewHtml.expects(:new)
.with(content_id: host_content_id,
content_block_edition: block_to_preview,
base_path: host_base_path)
.returns(preview_response)

preview_content = ContentBlockManager::PreviewContent.for_content_id(
content_id: host_content_id,
content_block_edition: block_to_preview,
)

assert_equal host_title, preview_content.title
assert_equal 2, preview_content.instances_count
assert_equal html, preview_content.html
end

it "allows a base_path to be provided" do
base_path = "/something/different"

ContentBlockManager::GeneratePreviewHtml.expects(:new)
.with(content_id: host_content_id,
content_block_edition: block_to_preview,
base_path:)
.returns(preview_response)

ContentBlockManager::PreviewContent.for_content_id(
content_id: host_content_id,
content_block_edition: block_to_preview,
base_path:,
)
end
end
end

0 comments on commit e730288

Please sign in to comment.