-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #355 from alphagov/content-modelling/647-support-e…
…mbeds-in-govspeak (647) Support Embeds in Govspeak
- Loading branch information
Showing
9 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
### 8.5.0 | ||
|
||
* Support embeds in Govspeak | ||
|
||
## 8.4.1 | ||
* Do not pin version of govuk_publishing_components | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Govspeak | ||
class EmbedExtractor | ||
def initialize(document) | ||
@document = document | ||
end | ||
|
||
def content_references | ||
@content_references ||= @document.scan(EmbeddedContent::EMBED_REGEX).map { |match| | ||
EmbeddedContent.new(document_type: match[1], content_id: match[2], embed_code: match[0]) | ||
}.uniq | ||
end | ||
|
||
def content_ids | ||
@content_ids ||= content_references.map(&:content_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Govspeak | ||
class EmbeddedContent | ||
SUPPORTED_DOCUMENT_TYPES = %w[contact content_block_email_address].freeze | ||
UUID_REGEX = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/ | ||
EMBED_REGEX = /({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):#{UUID_REGEX}}})/ | ||
|
||
attr_reader :document_type, :content_id, :embed_code | ||
|
||
def initialize(document_type:, content_id:, embed_code:) | ||
@document_type = document_type | ||
@content_id = content_id | ||
@embed_code = embed_code | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "action_view" | ||
require "htmlentities" | ||
|
||
module Govspeak | ||
class EmbedPresenter | ||
include ActionView::Helpers::TagHelper | ||
|
||
attr_reader :embed | ||
|
||
def initialize(embed) | ||
@embed = ActiveSupport::HashWithIndifferentAccess.new(embed) | ||
end | ||
|
||
def content_id | ||
embed[:content_id] | ||
end | ||
|
||
def document_type | ||
embed[:document_type] | ||
end | ||
|
||
def render | ||
body = if document_type == "content_block_email_address" | ||
embed.dig(:details, :email_address) | ||
else | ||
embed[:title] | ||
end | ||
|
||
content_tag(:span, body, class: "embed embed-#{document_type}", id: "embed_#{content_id}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Govspeak | ||
VERSION = "8.4.1".freeze | ||
VERSION = "8.5.0".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require "test_helper" | ||
|
||
class EmbedExtractorTest < Minitest::Test | ||
extend Minitest::Spec::DSL | ||
|
||
describe "EmbedExtractor" do | ||
subject { Govspeak::EmbedExtractor.new(document) } | ||
|
||
describe "when there is no embedded content" do | ||
let(:document) { "foo" } | ||
|
||
describe "#content_references" do | ||
it "returns an empty array" do | ||
assert_equal [], subject.content_references | ||
end | ||
end | ||
|
||
describe "#content_ids" do | ||
it "returns an empty array" do | ||
assert_equal [], subject.content_ids | ||
end | ||
end | ||
end | ||
|
||
describe "when there is embedded content" do | ||
let(:contact_uuid) { SecureRandom.uuid } | ||
let(:content_block_email_address_uuid) { SecureRandom.uuid } | ||
|
||
let(:document) do | ||
""" | ||
{{embed:contact:#{contact_uuid}}} | ||
{{embed:content_block_email_address:#{content_block_email_address_uuid}}} | ||
""" | ||
end | ||
|
||
describe "#content_references" do | ||
it "returns all references" do | ||
result = subject.content_references | ||
|
||
assert_equal 2, result.count | ||
|
||
assert_equal "contact", result[0].document_type | ||
assert_equal contact_uuid, result[0].content_id | ||
assert_equal "{{embed:contact:#{contact_uuid}}}", result[0].embed_code | ||
|
||
assert_equal "content_block_email_address", result[1].document_type | ||
assert_equal content_block_email_address_uuid, result[1].content_id | ||
assert_equal "{{embed:content_block_email_address:#{content_block_email_address_uuid}}}", result[1].embed_code | ||
end | ||
end | ||
|
||
describe "#content_ids" do | ||
it "returns all uuids as an array" do | ||
assert_equal [contact_uuid, content_block_email_address_uuid], subject.content_ids | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require "test_helper" | ||
|
||
class GovspeakEmbedsTest < Minitest::Test | ||
extend Minitest::Spec::DSL | ||
|
||
def compress_html(html) | ||
html.gsub(/[\n\r]+\s*/, "") | ||
end | ||
|
||
let(:content_id) { SecureRandom.uuid } | ||
|
||
it "renders an email address when present in options[:embeds]" do | ||
embed = { | ||
content_id:, | ||
document_type: "content_block_email_address", | ||
title: "foo", | ||
details: { | ||
email_address: "foo@example.com", | ||
}, | ||
} | ||
govspeak = "{{embed:content_block_email_address:#{content_id}}}" | ||
|
||
rendered = Govspeak::Document.new(govspeak, embeds: [embed]).to_html | ||
|
||
expected = "<p><span class=\"embed embed-content_block_email_address\" id=\"embed_#{content_id}\">#{embed[:details][:email_address]}</span></p>" | ||
|
||
assert_equal compress_html(expected), compress_html(rendered) | ||
end | ||
|
||
it "renders the title when the document type is a contact" do | ||
embed = { | ||
content_id:, | ||
document_type: "contact", | ||
title: "foo", | ||
} | ||
govspeak = "{{embed:contact:#{content_id}}}" | ||
|
||
rendered = Govspeak::Document.new(govspeak, embeds: [embed]).to_html | ||
|
||
expected = "<p><span class=\"embed embed-contact\" id=\"embed_#{content_id}\">#{embed[:title]}</span></p>" | ||
|
||
assert_equal compress_html(expected), compress_html(rendered) | ||
end | ||
|
||
it "ignores missing embeds" do | ||
govspeak = "{{embed:contact:#{content_id}}}" | ||
|
||
rendered = Govspeak::Document.new(govspeak, embeds: []).to_html | ||
|
||
assert_equal compress_html(""), compress_html(rendered) | ||
end | ||
|
||
it "supports multiple embeds" do | ||
embeds = [ | ||
{ | ||
content_id: SecureRandom.uuid, | ||
document_type: "contact", | ||
title: "foo", | ||
}, | ||
{ | ||
content_id: SecureRandom.uuid, | ||
document_type: "content_block_email_address", | ||
title: "foo", | ||
details: { | ||
email_address: "foo@example.com", | ||
}, | ||
}, | ||
] | ||
|
||
govspeak = %(Here is a contact: {{embed:contact:#{embeds[0][:content_id]}}} | ||
Here is an email address: {{embed:content_block_email_address:#{embeds[1][:content_id]}}} | ||
) | ||
|
||
rendered = Govspeak::Document.new(govspeak, embeds:).to_html | ||
|
||
expected = """ | ||
<p>Here is a contact: <span class=\"embed embed-contact\" id=\"embed_#{embeds[0][:content_id]}\">#{embeds[0][:title]}</span></p> | ||
<p>Here is an email address: <span class=\"embed embed-content_block_email_address\" id=\"embed_#{embeds[1][:content_id]}\">#{embeds[1][:details][:email_address]}</span></p> | ||
""" | ||
|
||
assert_equal compress_html(expected), compress_html(rendered) | ||
end | ||
end |