Skip to content

Commit

Permalink
Add attachment arguments
Browse files Browse the repository at this point in the history
These are needed to render the new accessible form link from GOVUK
Publishing Components. This is dependant on the component being updated
first [1].

This change will apply to publications and consultations, which use the
attachments partial. Initially, the new flow of requesting accessible
formats will be a pilot and only available for documents, owned by a few
organisations. This code will not need to be changed when the change is
released to all orgs.

[1]: alphagov/govuk_publishing_components#2636
  • Loading branch information
1pretz1 committed Mar 3, 2022
1 parent add72c3 commit 711ce3c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
8 changes: 7 additions & 1 deletion app/presenters/content_item/attachments.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
module ContentItem
module Attachments
def attachment_details(attachment_id)
content_item.dig("details", "attachments")&.find do |attachment|
found_attachment = content_item.dig("details", "attachments")&.find do |attachment|
attachment["id"] == attachment_id
end

return unless found_attachment

found_attachment["attachment_id"] = found_attachment.delete("id")
found_attachment["owning_document_content_id"] = content_item["content_id"]
found_attachment
end
end
end
49 changes: 49 additions & 0 deletions test/presenters/content_item/attachments_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "test_helper"

class ContentItemAttachmentsTest < ActiveSupport::TestCase
class DummyContentItem
include ContentItem::Attachments
attr_reader :content_item, :attachment_id, :content_id

def initialize
@attachment_id = 1234
@content_id = SecureRandom.uuid
@content_item = {
"content_id" => content_id,
"details" => {
"attachments" => [
{
"id" => attachment_id,
"title" => "Some title",
"url" => "some/url",
"alternative_format_contact_email" => "[email protected]",
},
],
},
}
end
end

test "returns attachment details for found attachment" do
dummy = DummyContentItem.new

details = dummy.attachment_details(dummy.attachment_id)

assert_equal details, {
"title" => "Some title",
"url" => "some/url",
"alternative_format_contact_email" => "[email protected]",
"attachment_id" => dummy.attachment_id,
"owning_document_content_id" => dummy.content_id,
}
end

test "returns nil if no attachment found" do
attachment_id = 4321
dummy = DummyContentItem.new

details = dummy.attachment_details(attachment_id)

assert_equal details, nil
end
end
15 changes: 12 additions & 3 deletions test/views/content_items/attachments.html.erb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ class ContentItemsAttachmentsTest < ActionView::TestCase

test "can render attachments using their metadata" do
@content_item = PublicationPresenter.new(
{ "details" => { "attachments" => [{ "id" => "attachment_id",
"title" => "Some title",
"url" => "some/url" }] } },
{
"content_id" => "doc_content_id",
"details" => {
"attachments" => [
{ "id" => "attachment_id",
"title" => "Some title",
"url" => "some/url",
"alternative_format_contact_email" => "[email protected]" },
],
},
},
"/publication",
ApplicationController.new.view_context,
)
Expand All @@ -30,6 +38,7 @@ class ContentItemsAttachmentsTest < ActionView::TestCase

assert_includes rendered, "gem-c-attachment"
assert_includes rendered, "Some title"
assert_includes rendered, "href=\"/contact/govuk/request-accessible-format?content_id=doc_content_id&amp;attachment_id=attachment_id"
end

test "it prioritises pre-rendered attachments" do
Expand Down

0 comments on commit 711ce3c

Please sign in to comment.