Skip to content

Commit da83bf9

Browse files
committed
Use the ContentBlockDetector in the presenter
This allows us to send the content IDs of any content blocks that are used in a smart answer as links, so Content Block Manager knows that the blocks are used in a particular Smart Answer.
1 parent 098b3f4 commit da83bf9

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

app/presenters/content_item_presenter.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def payload
2525
routes: [
2626
{ type: "prefix", path: base_path },
2727
],
28-
}
28+
links:,
29+
}.compact
2930
end
3031

3132
private
@@ -37,4 +38,12 @@ def start_node_presenter
3738
def base_path
3839
"/#{flow.name}"
3940
end
41+
42+
def links
43+
embed_content_ids.any? ? { embed: embed_content_ids } : nil
44+
end
45+
46+
def embed_content_ids
47+
@embed_content_ids ||= ContentBlockDetector.new(flow).content_blocks.map(&:content_id).uniq
48+
end
4049
end

test/unit/content_item_presenter_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,35 @@ class ContentItemPresenterPresenterTest < ActiveSupport::TestCase
2828
assert_equal "The Gorge of Eternal Peril!!!", payload[:description]
2929
assert_match %r{He who would cross the Bridge of Death}, payload.dig(:details, :hidden_search_terms, 0)
3030
end
31+
32+
context "when the ContentBlockDetector finds associated content blocks" do
33+
setup do
34+
@content_id = SecureRandom.uuid
35+
content_block_1 = stub("content_block", content_id: @content_id)
36+
content_block_2 = stub("content_block", content_id: @content_id)
37+
38+
detector = stub("detector", content_blocks: [content_block_1, content_block_2])
39+
ContentBlockDetector.expects(:new).with(@flow).returns(detector)
40+
end
41+
42+
should "include an array of unique content IDs in the payload" do
43+
payload = ContentItemPresenter.new(@flow).payload
44+
45+
assert_equal [@content_id], payload[:links][:embed]
46+
end
47+
end
48+
49+
context "when the ContentBlockDetector does not find associated content blocks" do
50+
setup do
51+
detector = stub("detector", content_blocks: [])
52+
ContentBlockDetector.expects(:new).with(@flow).returns(detector)
53+
end
54+
55+
should "not include any links" do
56+
payload = ContentItemPresenter.new(@flow).payload
57+
58+
assert_nil payload[:links]
59+
end
60+
end
3161
end
3262
end

0 commit comments

Comments
 (0)