Skip to content

Commit

Permalink
update Content Block Tools
Browse files Browse the repository at this point in the history
This requires passing an embed code to any Content
Block render methods. If this is for a preview, we
have to grab the code first from the HTML data
attribute.
  • Loading branch information
Harriethw committed Feb 13, 2025
1 parent 5bae452 commit 1bf996a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
content_block_tools (0.3.1)
content_block_tools (0.4.2)
actionview (>= 6)
crack (1.0.0)
bigdecimal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ def update_document_reference_to_latest_edition!
document.update!(latest_edition_id: id)
end

def render
def render(embed_code)
ContentBlockTools::ContentBlock.new(
document_type: "content_block_#{block_type}",
content_id: document.content_id,
title:,
details:,
embed_code:,
).render
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call
content_block = content_blocks.find { |c| c.document.content_id == reference.content_id }
next if content_block.nil?

html.gsub!(reference.embed_code, content_block.render)
html.gsub!(reference.embed_code, content_block.render(reference.embed_code))
end

html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def replace_existing_content_blocks(nokogiri_html)

def replace_blocks(nokogiri_html)
content_block_spans(nokogiri_html).each do |span|
span.replace content_block_edition.render
embed_code = span["data-embed-code"]
span.replace content_block_edition.render(embed_code)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class ContentBlockManager::ContentBlockEditionTest < ActiveSupport::TestCase
let(:rendered_response) { "RENDERED" }
let(:stub_block) { stub("ContentBlockTools::ContentBlock", render: rendered_response) }
let(:document) { content_block_edition.document }
let(:embed_code) { "embed_code" }

it "initializes and renders a content block" do
ContentBlockTools::ContentBlock.expects(:new)
Expand All @@ -186,9 +187,10 @@ class ContentBlockManager::ContentBlockEditionTest < ActiveSupport::TestCase
content_id: document.content_id,
title:,
details:,
embed_code:,
).returns(stub_block)

assert_equal content_block_edition.render, rendered_response
assert_equal content_block_edition.render(embed_code), rendered_response
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ContentBlockManager::FindAndReplaceEmbedCodesServiceTest < ActiveSupport::

expected = "
<p>Hello there</p>
<p>#{edition_2.render}</p>
<p>#{edition_1.render}</p>
<p>#{edition_2.render(edition_2.document.embed_code)}</p>
<p>#{edition_1.render(edition_1.document.embed_code)}</p>
"

result = ContentBlockManager::FindAndReplaceEmbedCodesService.call(html)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class ContentBlockManager::GeneratePreviewHtmlTest < ActiveSupport::TestCase
let(:host_base_path) { "/test" }
let(:uri_mock) { mock }
let(:fake_frontend_response) do
"<body class=\"govuk-body\"><p>test</p><span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\">[email protected]</span></body>"
"<body class=\"govuk-body\"><p>test</p><span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-embed-code=\"embed-code\" data-content-id=\"#{preview_content_id}\">[email protected]</span></body>"
end
let(:block_render) do
"<span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\">[email protected]</span>"
"<span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-embed-code=\"embed-code\" data-content-id=\"#{preview_content_id}\">[email protected]</span>"
end
let(:block_render_with_style) do
"<span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\" style=\"background-color: yellow;\">[email protected]</span>"
"<span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\" data-embed-code=\"embed-code\" style=\"background-color: yellow;\">[email protected]</span>"
end
let(:expected_html) do
"<body class=\"govuk-body draft\"><p>test</p>#{block_render_with_style}</body>"
Expand Down

0 comments on commit 1bf996a

Please sign in to comment.