Skip to content

Commit

Permalink
Remove assumptions of attachment existence
Browse files Browse the repository at this point in the history
This resolves a couple of scenarios where assumptions are made that
documents will have attachments and attachments with particular ids
exist. This is data that developers assume is the case (and most likely
is) but is beyond the guarantees that schemas make.

This mismatch can cause tests to fail when valid schemas are generated
which cannot be rendered.
  • Loading branch information
kevindew committed Mar 8, 2021
1 parent f023572 commit 37b8b4b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/presenters/content_item/attachments.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions app/views/content_items/_attachments.html.erb
Original file line number Diff line number Diff line change
@@ -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? %>
<section id="<%= title.parameterize %>">
<%= render 'govuk_publishing_components/components/heading',
text: title,
Expand All @@ -11,15 +13,14 @@
<%= raw(legacy_pre_rendered_documents) %>
<% end %>
<% else %>
<% attachments.each do |attachment_id| %>
<% attachment_details.each do |details| %>
<div class="attachment">
<%= render 'govuk_publishing_components/components/attachment', {
heading_level: 3,
attachment: @content_item.attachment_details(attachment_id)
attachment: details
} %>
</div>
<% end %>
<% end %>

</section>
<% end %>

0 comments on commit 37b8b4b

Please sign in to comment.