Skip to content

Commit

Permalink
Merge pull request #176 from alphagov/publication-attachments
Browse files Browse the repository at this point in the history
Turn `attachments` into `parts`
  • Loading branch information
csutter authored Jan 15, 2024
2 parents 628a051 + b54fa3d commit 8b48fd9
Show file tree
Hide file tree
Showing 4 changed files with 1,078 additions and 2 deletions.
28 changes: 28 additions & 0 deletions app/models/concerns/publishing_api/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def manual
end

def parts
parts_from_parts || parts_from_attachments
end

def parts_from_parts
document_hash
.dig(:details, :parts)
&.map do
Expand All @@ -124,5 +128,29 @@ def parts
}
end
end

def parts_from_attachments
document_hash
.dig(:details, :attachments)
&.map {
# Skip any attachments that aren't directly nested underneath this document
#
# This is replicated from v1 search-api behaviour, and is a workaround for the fact that
# the consumers always expect a slug rather than a URL, so parts cannot have a URL that
# doesn't match the document's base path.
next unless _1[:url].start_with?(document_hash[:base_path])

# The slug for a part from an attachment is the part of the URL that comes after the
# parent document's base path.
slug = _1[:url].sub(document_hash[:base_path], "").sub(%r{^/}, "").sub(%r{/$}, "")
{
slug:,
title: _1[:title],
# TODO: We don't receive any body content for attachments as part of the message from
# publishing-api. This needs to be fetched from publishing-api separately.
body: "",
}
}&.compact_blank
end
end
end
Loading

0 comments on commit 8b48fd9

Please sign in to comment.