Skip to content

Commit

Permalink
Add attachment_data_id to file attachments
Browse files Browse the repository at this point in the history
This needs to follow on from alphagov/publishing-api#2994

As described there, the lack of attachment_data_id in the data for file asset attachments in content store makes it hard to request the attachments on the server side. Instead, we have to redirect users to the assets, which has led to ugly workarounds like CSV previews being rendered by frontend but served on assets.publishing.service.gov.uk.

Adding attachment_data_id should allow the frontends to request attachments directly, using the API client:

    GdsApi.asset_manager.media(attachment_data_id, filename)

This should make it much more convenient to preview assets, or use them for other rendering purposes (e.g. showing a CSV as a line graph).
  • Loading branch information
richardTowers committed Nov 27, 2024
1 parent 4ebd734 commit 4e6b4e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/file_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def publishing_api_details_for_format
filename:,
number_of_pages:,
preview_url:,
attachment_data_id:,
}
end

Expand All @@ -66,6 +67,12 @@ def alternative_format_contact_email
nil
end

def attachment_data_id
return unless csv? && attachable.is_a?(Edition) && attachment_data.all_asset_variants_uploaded?

attachment_data.id
end

def preview_url
return unless csv? && attachable.is_a?(Edition) && attachment_data.all_asset_variants_uploaded?

Expand Down
5 changes: 5 additions & 0 deletions test/unit/app/models/file_attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ def assert_delegated(attachment, method)
attachment = create(:csv_attachment, attachable: create(:edition))
assert_equal Plek.asset_root + "/media/#{attachment.attachment_data.id}/sample.csv/preview", attachment.publishing_api_details_for_format[:preview_url]
end

test "return media attachment_data_id if all_asset_variants_uploaded?" do
attachment = create(:csv_attachment, attachable: create(:edition))
assert_equal attachment.attachment_data.id, attachment.publishing_api_details_for_format[:attachment_data_id]
end
end

0 comments on commit 4e6b4e6

Please sign in to comment.