diff --git a/app/presenters/content_item/attachments.rb b/app/presenters/content_item/attachments.rb index 628e8d625..ca26c41cc 100644 --- a/app/presenters/content_item/attachments.rb +++ b/app/presenters/content_item/attachments.rb @@ -1,7 +1,7 @@ module ContentItem module Attachments def attachment_details(attachment_id) - content_item["details"]["attachments"].find do |attachment| + content_item.dig("details", "attachments")&.find do |attachment| attachment["id"] == attachment_id end end diff --git a/app/views/content_items/_attachments.html.erb b/app/views/content_items/_attachments.html.erb index f16792a73..7ffcd8e65 100644 --- a/app/views/content_items/_attachments.html.erb +++ b/app/views/content_items/_attachments.html.erb @@ -1,4 +1,6 @@ -<% if legacy_pre_rendered_documents.present? || attachments.any? %> +<% attachments ||= [] %> +<% attachment_details = attachments.filter_map { |id| @content_item&.attachment_details(id) } %> +<% if legacy_pre_rendered_documents.present? || attachment_details.any? %>
<%= render 'govuk_publishing_components/components/heading', text: title, @@ -11,15 +13,14 @@ <%= raw(legacy_pre_rendered_documents) %> <% end %> <% else %> - <% attachments.each do |attachment_id| %> + <% attachment_details.each do |details| %>
<%= render 'govuk_publishing_components/components/attachment', { heading_level: 3, - attachment: @content_item.attachment_details(attachment_id) + attachment: details } %>
<% end %> <% end %> -
<% end %> diff --git a/test/test_helper.rb b/test/test_helper.rb index 4753e3b94..c224d90af 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -173,7 +173,7 @@ def setup_and_visit_content_item_with_taxons(name, taxons) def setup_and_visit_random_content_item(document_type: nil) content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: schema_type) do |payload| - payload.merge("document_type" => document_type) unless document_type.nil? + payload.merge!("document_type" => document_type) unless document_type.nil? payload end