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 %>