-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render featured attachments directly
This extends the attachments partial to render attachments directly using the new 'attachments' metadata in the payload (see RFC 116) [1]. Since both kinds of data may be present in the payload, I've added a view test to ensure we don't change the way attachments are rendered until the publishing app stops sending pre-rendered attachments. Publications have one set of featured attachments, while consultations can have up to three. For the tests, I decided to test both the legacy and the new behaviours in the same test block, noting that there's no other way to group these related tests together. The new 'attachment' CSS class replicates the 30px bottom margin that legacy pre-rendered attachments have in Govspeak [2]. [1] https://github.com/alphagov/govuk-rfcs/blob/master/rfc-116-store-attachment-data-in-content-items.md [2] https://github.com/alphagov/govuk_publishing_components/blob/6ee40d5f1c2b3d81d98db0923ab96cf454d34ff0/app/assets/stylesheets/govuk_publishing_components/components/govspeak/_attachment.scss#L22
- Loading branch information
Ben Thorner
committed
Mar 26, 2020
1 parent
c6ed5d8
commit 999f56c
Showing
11 changed files
with
126 additions
and
13 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
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,3 @@ | ||
.attachment { | ||
margin-bottom: govuk-spacing(6); | ||
} |
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,9 @@ | ||
module ContentItem | ||
module Attachments | ||
def attachment_details(attachment_id) | ||
content_item["details"]["attachments"].find do |attachment| | ||
attachment["id"] == attachment_id | ||
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
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,14 +1,23 @@ | ||
<% attachment_title_id = "#{title.parameterize}-title" %> | ||
|
||
<% if legacy_pre_rendered_documents.present? %> | ||
<% if legacy_pre_rendered_documents.present? || attachments.any? %> | ||
<%= render 'govuk_publishing_components/components/heading', | ||
id: attachment_title_id, | ||
text: title, | ||
mobile_top_margin: true %> | ||
|
||
<div aria-labelledby="<%= attachment_title_id %>"> | ||
<%= render 'govuk_publishing_components/components/govspeak', | ||
content: legacy_pre_rendered_documents.html_safe, | ||
direction: page_text_direction %> | ||
<% if legacy_pre_rendered_documents.present? %> | ||
<%= render 'govuk_publishing_components/components/govspeak', | ||
content: legacy_pre_rendered_documents.html_safe, | ||
direction: page_text_direction %> | ||
<% else %> | ||
<% attachments.each do |attachment_id| %> | ||
<div class="attachment"> | ||
<%= render 'govuk_publishing_components/components/attachment', | ||
attachment: @content_item.attachment_details(attachment_id) %> | ||
</div> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
<% 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
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
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,40 @@ | ||
require "test_helper" | ||
|
||
class ContentItemsAttachmentsTest < ActionView::TestCase | ||
test "it shows pre-rendered attachments by default" do | ||
render(partial: "content_items/attachments", | ||
locals: { title: "Documents", | ||
legacy_pre_rendered_documents: "some html" }) | ||
|
||
assert_includes rendered, "gem-c-govspeak" | ||
assert_includes rendered, "some html" | ||
end | ||
|
||
test "can render attachments using their metadata" do | ||
@content_item = PublicationPresenter.new( | ||
"details" => { | ||
"attachments" => [{ "id" => "attachment_id", | ||
"title" => "Some title", | ||
"url" => "some/url" }] | ||
} | ||
) | ||
|
||
render(partial: "content_items/attachments", | ||
locals: { title: "Documents", | ||
legacy_pre_rendered_documents: "", | ||
attachments: %w(attachment_id) }) | ||
|
||
assert_includes rendered, "gem-c-attachment" | ||
assert_includes rendered, "Some title" | ||
end | ||
|
||
test "it prioritises pre-rendered attachments" do | ||
render(partial: "content_items/attachments", | ||
locals: { title: "Documents", | ||
legacy_pre_rendered_documents: "some html", | ||
attachments: %w(attachment_id) }) | ||
|
||
assert_includes rendered, "gem-c-govspeak" | ||
assert_includes rendered, "some html" | ||
end | ||
end |