Skip to content

Commit

Permalink
Replace embed tags in body when expanding links
Browse files Browse the repository at this point in the history
When expanding links, any embedded content within the body of the linked
item should also be expanded to include the full value.
  • Loading branch information
brucebolt committed Aug 7, 2024
1 parent ab39844 commit 0cf721c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 32 deletions.
7 changes: 6 additions & 1 deletion app/presenters/queries/expanded_link_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def present_expanded_link(link_hash)
end

if hash[:details]
hash[:details] = Presenters::DetailsPresenter.new(hash[:details], nil, nil).details
hash[:details] = Presenters::DetailsPresenter.new(hash[:details], nil, content_embed_presenter(hash[:content_id], hash[:locale])).details
end
end
end
Expand All @@ -77,6 +77,11 @@ def available_translations
def translations
available_translations.translations
end

def content_embed_presenter(content_id, locale)
edition = Document.find_by(content_id:, locale:).live
Presenters::ContentEmbedPresenter.new(edition) if edition
end
end
end
end
107 changes: 76 additions & 31 deletions spec/presenters/queries/expanded_link_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,85 @@
describe "details" do
let(:c) { create_link_set }

before do
create_edition(a, "/a", document_type: "person")
create_edition(
b,
"/b",
document_type: "ministerial_role",
details: {
body: [
{
content_type: "text/govspeak",
content: "Body",
},
],
},
)
create_edition(c, "/c", document_type: "role_appointment")
context "without embedded content in the body" do
before do
create_edition(a, "/a", document_type: "person")
create_edition(
b,
"/b",
document_type: "ministerial_role",
details: {
body: [
{
content_type: "text/govspeak",
content: "Body",
},
],
},
)
create_edition(c, "/c", document_type: "role_appointment")

create_link(c, a, "person")
create_link(c, b, "role")
end

create_link(c, a, "person")
create_link(c, b, "role")
it "recursively calls the details presenter and renders govspeak inside expanded links" do
b = expanded_links[:role_appointments].first
c = b[:links][:role].first
expect(c[:details][:body]).to match([
{
content_type: "text/govspeak",
content: "Body",
},
{
content_type: "text/html",
content: "<p>Body</p>\n",
},
])
end
end

it "recursively calls the details presenter and renders govspeak inside expanded links" do
b = expanded_links[:role_appointments].first
c = b[:links][:role].first
expect(c[:details][:body]).to match([
{
content_type: "text/govspeak",
content: "Body",
},
{
content_type: "text/html",
content: "<p>Body</p>\n",
},
])
context "with embedded content in the body" do
let(:contact) do
create(:edition, state: "published", content_store: "live", document_type: "contact", title: "Some contact")
end

before do
create_edition(a, "/a", document_type: "person")
create_edition(
b,
"/b",
document_type: "ministerial_role",
details: {
body: [
{
content_type: "text/govspeak",
content: "{{embed:contact:#{contact.document.content_id}}}",
},
],
},
links_hash: { embed: [contact.document.content_id] },
)
create_edition(c, "/c", document_type: "role_appointment")

create_link(c, a, "person")
create_link(c, b, "role")
end

it "recursively calls the details presenter and embeds content inside expanded links" do
b = expanded_links[:role_appointments].first
c = b[:links][:role].first
expect(c[:details][:body]).to match([
{
content_type: "text/govspeak",
content: "Some contact",
},
{
content_type: "text/html",
content: "<p>Some contact</p>\n",
},
])
end
end
end
end

0 comments on commit 0cf721c

Please sign in to comment.