Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new-style csv preview links for attachments #3434

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions app/presenters/content_item/attachments.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
module ContentItem
module Attachments
def attachments
return [] unless content_item["details"]["attachments"]
content_item_attachments = content_item.dig("details", "attachments")
return [] unless content_item_attachments

docs = content_item["details"]["attachments"].select { |a| !a.key?("locale") || a["locale"] == locale }
docs = content_item_attachments.select { |a| !a.key?("locale") || a["locale"] == locale }
docs.each do |doc|
doc["type"] = "html" unless doc["content_type"]
doc["type"] = "external" if doc["attachment_type"] == "external"
doc["preview_url"] = "#{doc['url']}/preview" if doc["preview_url"]
doc["preview_url"] = preview_url(doc) if doc["preview_url"]
doc["alternative_format_contact_email"] = nil if doc["accessible"] == true
end
end

def attachments_from(attachment_id_list)
attachments.select { |doc| (attachment_id_list || []).include? doc["id"] }
end

private

def preview_url(doc)
case doc.symbolize_keys
in { preview_url: String, attachment_data_id:, filename: }
# We have attachment_data_id and filename, so we can redirect to a new style CSV preview
# Note: using a path rather than a full URL so the user doesn't flip from a draft frontend
# to a live frontend if previewing a live asset of a draft document
"/csv-preview/#{attachment_data_id}/#{filename}"
in { preview_url: String, url: }
# TODO: remove this branch once we're confident attachment_data_id is present everywhere
Rails.logger.warn("Attachment at #{url} using old style preview, because no attachment_data_id present")
# NOTE: we're using "#{url}/preview" here because the preview_url fields have the wrong ids in them
"#{url}/preview"
end
end
end
end
58 changes: 58 additions & 0 deletions test/presenters/content_item/attachments_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "test_helper"

class AttachmentsTest < ActiveSupport::TestCase
setup do
@subject = (Class.new do
attr_accessor :content_item

include ContentItem::Attachments
end).new
end

test "#attachments returns an empty array when there are no attachments" do
attachments = []
@subject.content_item = { "details" => { "attachments" => attachments } }
assert_equal [], @subject.attachments
end

test "#attachments returns attachments with type html by default" do
attachments = [
{ "any-key" => "any-value" },
]
@subject.content_item = { "details" => { "attachments" => attachments } }
assert_equal [{ "any-key" => "any-value", "type" => "html" }], @subject.attachments
end

test "#attachments returns attachments with preview_urls based on url" do
attachments = [
{ "preview_url" => "some-preview-url", "url" => "some-url" },
]
@subject.content_item = { "details" => { "attachments" => attachments } }
assert_equal [{ "preview_url" => "some-url/preview", "url" => "some-url", "type" => "html" }], @subject.attachments
end

test "#attachments returns attachments with preview_urls based on attachment_data_id and filename" do
attachments = [{
"preview_url" => "some-preview-url",
"url" => "some-url",
"attachment_data_id" => "some-attachment-data-id",
"filename" => "some-filename",
}]
@subject.content_item = { "details" => { "attachments" => attachments } }
assert_equal [{
"preview_url" => "/csv-preview/some-attachment-data-id/some-filename",
"url" => "some-url",
"attachment_data_id" => "some-attachment-data-id",
"filename" => "some-filename",
"type" => "html",
}], @subject.attachments
end

test "#attachments raises an error if preview_url is present, but both url and attachment_data_id / filename are absent" do
attachments = [{
"preview_url" => "some-preview-url",
}]
@subject.content_item = { "details" => { "attachments" => attachments } }
assert_raises(NoMatchingPatternError) { @subject.attachments }
end
end
21 changes: 21 additions & 0 deletions test/presenters/publication_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ def schema_name
assert presented_item("statistics_publication").national_statistics?
end

test "#attachments_for_components presents featured attachments" do
content_item = schema_item
content_item["details"]["featured_attachments"] = %w[some-id]
content_item["details"]["attachments"] = [{
"id" => "some-id",
"content_type" => "text/csv",
"preview_url" => "some-preview-url",
"url" => "some-url",
}]
presented = present_example(content_item)
expected = [{
"id" => "some-id",
"content_type" => "text/csv",
# NOTE: preview_url is the url with /preview appended, not the preview_url from above,
# because we're working around a bug with preview_url
"preview_url" => "some-url/preview",
"url" => "some-url",
}]
assert_equal expected, presented.attachments_for_components
end

test "presents withdrawn notices" do
example = schema_item("withdrawn_publication")
presented = presented_item("withdrawn_publication")
Expand Down
Loading