Skip to content

Commit

Permalink
Use ContentBlockTools when rendering
Browse files Browse the repository at this point in the history
This ensures there is one source of truth for how content blocks are
rendered, and it can be used in other applications easily.
  • Loading branch information
pezholio committed Nov 20, 2024
1 parent c8750f6 commit 523fc78
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 88 deletions.
13 changes: 6 additions & 7 deletions app/presenters/content_embed_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ def render_embedded_editions(content)
content
end

# This is a temporary solution to get email address content blocks working
# while we agree on a long-term approach that works for everything.
def get_content_for_edition(edition)
if edition.document_type == "content_block_email_address"
edition.details[:email_address]
else
edition.title
end
ContentBlockTools::ContentBlock.new(
document_type: edition.document_type,
content_id: edition.document.content_id,
title: edition.title,
details: edition.details,
).render
end
end
end
41 changes: 8 additions & 33 deletions spec/integration/put_content/content_with_embedded_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
it "should send transformed content to the content store" do
put "/v2/content/#{content_id}", params: payload.to_json

expect_content_store_to_have_received_details_including({ "body" => "#{first_contact.title} #{second_contact.title}" })
expect_content_store_to_have_received_details_including({ "body" => "#{presented_details_for(first_contact)} #{presented_details_for(second_contact)}" })
end
end

Expand Down Expand Up @@ -52,7 +52,7 @@
it "should send transformed content to the content store" do
put "/v2/content/#{content_id}", params: payload_for_multiple_field_embeds.to_json

expect_content_store_to_have_received_details_including({ "downtime_message" => first_contact.title.to_s })
expect_content_store_to_have_received_details_including({ "downtime_message" => presented_details_for(first_contact) })
end
end

Expand Down Expand Up @@ -128,11 +128,11 @@
"body" => [
{
"content_type" => "text/govspeak",
"content" => first_contact.title,
"content" => presented_details_for(first_contact),
},
{
"content_type" => "text/html",
"content" => "<p>#{first_contact.title}</p>",
"content" => "<p>#{presented_details_for(first_contact)}</p>",
},
],
},
Expand All @@ -142,11 +142,11 @@
"body" => [
{
"content_type" => "text/govspeak",
"content" => second_contact.title,
"content" => presented_details_for(second_contact),
},
{
"content_type" => "text/html",
"content" => "<p>#{second_contact.title}</p>",
"content" => "<p>#{presented_details_for(second_contact)}</p>",
},
],
},
Expand Down Expand Up @@ -176,32 +176,7 @@
it "should send transformed content to the content store" do
put "/v2/content/#{content_id}", params: payload.to_json

expect_content_store_to_have_received_details_including({ "body" => array_including({ "content_type" => "text/govspeak", "content" => "#{first_contact.title} #{second_contact.title}" }) })
end
end

context "with an embedded email address" do
let(:first_email_address) { create(:edition, state: "published", content_store: "live", document_type: "content_block_email_address", details: { email_address: "[email protected]" }) }
let(:second_email_address) { create(:edition, state: "published", content_store: "live", document_type: "content_block_email_address", details: { email_address: "[email protected]" }) }
let(:document) { create(:document, content_id:) }

before do
payload.merge!(document_type: "press_release", schema_name: "news_article", details: { body: "{{embed:content_block_email_address:#{first_email_address.document.content_id}}} {{embed:content_block_email_address:#{second_email_address.document.content_id}}}" })
end

it "should create links" do
expect {
put "/v2/content/#{content_id}", params: payload.to_json
}.to change(Link, :count).by(2)

expect(Link.find_by(target_content_id: first_email_address.content_id)).not_to be_nil
expect(Link.find_by(target_content_id: second_email_address.content_id)).not_to be_nil
end

it "should send transformed content to the content store" do
put "/v2/content/#{content_id}", params: payload.to_json

expect_content_store_to_have_received_details_including({ "body" => "#{first_email_address.details[:email_address]} #{second_email_address.details[:email_address]}" })
expect_content_store_to_have_received_details_including({ "body" => array_including({ "content_type" => "text/govspeak", "content" => "#{presented_details_for(first_contact)} #{presented_details_for(second_contact)}" }) })
end
end

Expand All @@ -226,7 +201,7 @@
it "should send transformed content to the content store" do
put "/v2/content/#{content_id}", params: payload.to_json

expect_content_store_to_have_received_details_including({ "body" => "#{email_address.details[:email_address]} #{contact.title}" })
expect_content_store_to_have_received_details_including({ "body" => "#{presented_details_for(email_address)} #{presented_details_for(contact)}" })
end
end

Expand Down
81 changes: 38 additions & 43 deletions spec/presenters/content_embed_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
end
let(:details) { {} }

before do
let!(:embedded_edition) do
embedded_document = create(:document, content_id: embedded_content_id)
create(
:edition,
Expand All @@ -29,12 +29,24 @@
end

describe "#render_embedded_content" do
let(:expected_value) { "VALUE" }
let(:stub_block) { double(ContentBlockTools::ContentBlock, render: expected_value) }

before do
expect(ContentBlockTools::ContentBlock).to receive(:new).with(
document_type: embedded_edition.document_type,
content_id: embedded_edition.document.content_id,
title: embedded_edition.title,
details: embedded_edition.details,
).at_least(:once).and_return(stub_block)
end

context "when body is a string" do
let(:details) { { body: "some string with a reference: {{embed:contact:#{embedded_content_id}}}" } }

it "returns embedded content references with values from their editions" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: "some string with a reference: VALUE",
body: "some string with a reference: #{expected_value}",
})
end
end
Expand All @@ -50,8 +62,8 @@
it "returns embedded content references with values from their editions" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: [
{ content_type: "text/govspeak", content: "some string with a reference: VALUE" },
{ content_type: "text/html", content: "some string with a reference: VALUE" },
{ content_type: "text/govspeak", content: "some string with a reference: #{expected_value}" },
{ content_type: "text/html", content: "some string with a reference: #{expected_value}" },
],
})
end
Expand All @@ -64,7 +76,7 @@

it "returns embedded content references with values from their editions" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: { title: "some string with a reference: VALUE" },
body: { title: "some string with a reference: #{expected_value}" },
})
end
end
Expand All @@ -91,7 +103,7 @@
parts: [
body: [
{
content: "some string with a reference: VALUE",
content: "some string with a reference: #{expected_value}",
content_type: "text/govspeak",
},
],
Expand All @@ -106,7 +118,7 @@
context "when the embedded content is available in multiple locales" do
let(:details) { { body: "some string with a reference: {{embed:contact:#{embedded_content_id}}}" } }

before do
let!(:welsh_edition) do
embedded_document = create(:document, content_id: embedded_content_id, locale: "cy")
create(
:edition,
Expand All @@ -121,17 +133,18 @@
context "when the document is in the default language" do
it "returns embedded content references with values from the same language" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: "some string with a reference: VALUE",
body: "some string with a reference: #{expected_value}",
})
end
end

context "when the document is in an available locale" do
let(:document) { create(:document, locale: "cy") }
let(:embedded_edition) { welsh_edition }

it "returns embedded content references with values from the same language" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: "some string with a reference: WELSH",
body: "some string with a reference: #{expected_value}",
})
end
end
Expand All @@ -141,46 +154,16 @@

it "returns embedded content references with values from the default language" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: "some string with a reference: VALUE",
body: "some string with a reference: #{expected_value}",
})
end
end
end

context "when the document is an email address" do
let(:embedded_document) { create(:document) }
let(:links_hash) do
{
embed: [embedded_document.content_id],
}
end

before do
create(
:edition,
document: embedded_document,
state: "published",
content_store: "live",
document_type: "content_block_email_address",
details: {
email_address: "[email protected]",
},
)
end

let(:details) { { body: "some string with a reference: {{embed:content_block_email_address:#{embedded_document.content_id}}}" } }

it "returns an email address" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: "some string with a reference: [email protected]",
})
end
end

context "when multiple documents are embedded in different parts of the document" do
let(:other_embedded_content_id) { SecureRandom.uuid }

before do
let!(:other_embedded_edition) do
embedded_document = create(:document, content_id: other_embedded_content_id)
create(
:edition,
Expand All @@ -192,6 +175,18 @@
)
end

let(:other_expected_value) { "VALUE2" }
let(:other_stub_block) { double(ContentBlockTools::ContentBlock, render: other_expected_value) }

before do
expect(ContentBlockTools::ContentBlock).to receive(:new).with(
document_type: other_embedded_edition.document_type,
content_id: other_embedded_edition.document.content_id,
title: other_embedded_edition.title,
details: other_embedded_edition.details,
).at_least(:once).and_return(other_stub_block)
end

let(:links_hash) do
{
embed: [embedded_content_id, other_embedded_content_id],
Expand All @@ -207,8 +202,8 @@

it "returns embedded content references with values from their editions" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
title: "title string with reference: VALUE2",
body: "some string with a reference: VALUE",
title: "title string with reference: #{other_expected_value}",
body: "some string with a reference: #{expected_value}",
})
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/presenters/details_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
let(:edition_details) { edition.details }
let(:expected_details) do
{
field_name => embeddable_content.title,
field_name => presented_details_for(embeddable_content),
}.symbolize_keys
end

Expand All @@ -219,8 +219,8 @@
let(:expected_details) do
{
field_name => [
{ content_type: "text/html", content: embeddable_content.title },
{ content_type: "text/govspeak", content: embeddable_content.title },
{ content_type: "text/html", content: presented_details_for(embeddable_content) },
{ content_type: "text/govspeak", content: presented_details_for(embeddable_content) },
],
}.symbolize_keys
end
Expand Down
4 changes: 2 additions & 2 deletions spec/presenters/queries/expanded_link_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@
expect(c[:details][:body]).to match([
{
content_type: "text/govspeak",
content: "Some contact",
content: presented_details_for(contact),
},
{
content_type: "text/html",
content: "<p>Some contact</p>\n",
content: "<p>#{presented_details_for(contact)}</p>\n",
},
])
end
Expand Down
14 changes: 14 additions & 0 deletions spec/support/content_block_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ContentBlockHelpers
def presented_details_for(edition)
ContentBlockTools::ContentBlock.new(
document_type: edition.document_type,
content_id: edition.document.content_id,
title: edition.title,
details: edition.details,
).render
end
end

RSpec.configure do |c|
c.include ContentBlockHelpers
end

0 comments on commit 523fc78

Please sign in to comment.