diff --git a/app/presenters/publication_presenter.rb b/app/presenters/publication_presenter.rb
index 98c7e575f..84c9aadce 100644
--- a/app/presenters/publication_presenter.rb
+++ b/app/presenters/publication_presenter.rb
@@ -10,10 +10,14 @@ def details
content_item["details"]["body"]
end
+ def attachments_for_components
+ documents.select { |doc| featured_attachments.include? doc["id"] }
+ end
+
def documents
return [] unless content_item["details"]["attachments"]
- docs = content_item["details"]["attachments"].select { |a| a["locale"] == locale }
+ docs = content_item["details"]["attachments"].select { |a| !a.key?("locale") || a["locale"] == locale }
docs.each do |doc|
doc["type"] = "html" unless doc["content_type"]
doc["alternative_format_contact_email"] = nil if doc["accessible"] == true
diff --git a/app/views/content_items/_attachments_list.html.erb b/app/views/content_items/_attachments_list.html.erb
index 327bacba2..99f35a694 100644
--- a/app/views/content_items/_attachments_list.html.erb
+++ b/app/views/content_items/_attachments_list.html.erb
@@ -1,21 +1,16 @@
-<%
- attachments ||= nil
- featured_attachments ||= []
- attachment_details = featured_attachments.filter_map { |id| @content_item&.attachment_details(id) }
-%>
-
- <%= render 'govuk_publishing_components/components/heading',
- text: title,
- mobile_top_margin: true
- %>
- <% if attachment_details.length > 0 %>
+<% if attachments_for_components.any? %>
+
+ <%= render 'govuk_publishing_components/components/heading',
+ text: title,
+ mobile_top_margin: true
+ %>
<% add_gem_component_stylesheet("details") %>
- <% attachment_details.each do |details| %>
+ <% attachments_for_components.each do |details| %>
<%= render 'govuk_publishing_components/components/attachment', {
heading_level: 3,
attachment: details,
margin_bottom: 6
} %>
<% end %>
- <% end %>
-
+
+<% end %>
diff --git a/app/views/content_items/publication.html.erb b/app/views/content_items/publication.html.erb
index 322d5ea65..293c4ed7c 100644
--- a/app/views/content_items/publication.html.erb
+++ b/app/views/content_items/publication.html.erb
@@ -51,8 +51,7 @@
<%= render "attachments_list",
title: t("publication.documents", count: 5), # This should always be pluralised.
- attachments: @content_item.documents,
- featured_attachments: @content_item.featured_attachments
+ attachments_for_components: @content_item.attachments_for_components
%>
diff --git a/test/integration/publication_test.rb b/test/integration/publication_test.rb
index 48c43bf53..9ddafa5dd 100644
--- a/test/integration/publication_test.rb
+++ b/test/integration/publication_test.rb
@@ -58,9 +58,7 @@ class PublicationTest < ActionDispatch::IntegrationTest
}
setup_and_visit_content_item("publication", overrides)
- within "#documents" do
- assert page.has_no_text?("Permit: Veolia ES (UK) Limited")
- end
+ assert page.has_no_text?("Permit: Veolia ES (UK) Limited")
end
test "renders featured document attachments using components" do
@@ -71,6 +69,16 @@ class PublicationTest < ActionDispatch::IntegrationTest
end
end
+ test "doesn't render the documents section if no documents" do
+ overrides = {
+ "details" => {
+ "attachments" => [{}],
+ },
+ }
+ setup_and_visit_content_item("publication-with-featured-attachments", overrides)
+ assert page.has_no_text?("Documents")
+ end
+
test "renders accessible format option when accessible is false and email is supplied" do
overrides = {
"details" => {