Skip to content

Commit 9618129

Browse files
unoduetrecatalinailie
authored andcommitted
Use asset_manager_id instead of attachment_data_id
When sending attachment information to publishing api, use asset_manager_id instead of attachment_data_id, as this is the id that the asset manager expects.
1 parent 2c9c400 commit 9618129

File tree

8 files changed

+43
-6
lines changed

8 files changed

+43
-6
lines changed

app/models/attachment_data.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class AttachmentData < ApplicationRecord
99
as: :assetable,
1010
inverse_of: :assetable
1111

12-
delegate :url, :path, to: :file, allow_nil: true
12+
delegate :url, :path, :asset_manager_id, to: :file, allow_nil: true
1313

1414
before_save :update_file_attributes
1515

app/models/file_attachment.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def publishing_api_details_for_format
5555
filename:,
5656
number_of_pages:,
5757
preview_url:,
58-
attachment_data_id:,
58+
asset_manager_id:,
5959
}
6060
end
6161

@@ -67,10 +67,10 @@ def alternative_format_contact_email
6767
nil
6868
end
6969

70-
def attachment_data_id
70+
def asset_manager_id
7171
return unless csv? && attachable.is_a?(Edition) && attachment_data.all_asset_variants_uploaded?
7272

73-
attachment_data.id
73+
attachment_data.asset_manager_id
7474
end
7575

7676
def preview_url

app/uploaders/attachment_uploader.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def extension_allowlist
2626
EXTENSION_ALLOW_LIST
2727
end
2828

29+
def asset_manager_id
30+
file.try(:asset_manager_id)
31+
end
32+
2933
class ZipFile
3034
class NonUTF8ContentsError < RuntimeError; end
3135

lib/whitehall/asset_manager_storage.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ def url
3434
end
3535
end
3636

37+
def asset_manager_id
38+
asset = get_asset
39+
if asset
40+
asset.asset_manager_id
41+
end
42+
end
43+
3744
def path
3845
# We keep this because carrierwave needs this in after commit hook
3946
# to look for previous files to delete

test/unit/app/models/attachment_data_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,10 @@ class AttachmentDataTest < ActiveSupport::TestCase
452452

453453
assert_not attachment_data.all_asset_variants_uploaded?
454454
end
455+
456+
test "#asset_manager_id returns asset manager id" do
457+
attachment_data = create(:attachment_data_for_csv, attachable: create(:draft_edition, id: 1))
458+
459+
assert_equal "asset_manager_id", attachment_data.asset_manager_id
460+
end
455461
end

test/unit/app/models/file_attachment_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def assert_delegated(attachment, method)
6969
assert_equal Plek.asset_root + "/media/#{attachment.attachment_data.id}/sample.csv/preview", attachment.publishing_api_details_for_format[:preview_url]
7070
end
7171

72-
test "return media attachment_data_id if all_asset_variants_uploaded?" do
72+
test "#asset_manager_id returns asset manager id if all_asset_variants_uploaded?" do
7373
attachment = create(:csv_attachment, attachable: create(:edition))
74-
assert_equal attachment.attachment_data.id, attachment.publishing_api_details_for_format[:attachment_data_id]
74+
assert_not_nil attachment.attachment_data.asset_manager_id
75+
assert_equal attachment.attachment_data.asset_manager_id, attachment.publishing_api_details_for_format[:asset_manager_id]
7576
end
7677
end

test/unit/app/uploaders/attachment_uploader_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ class AttachmentUploaderTest < ActiveSupport::TestCase
138138
end
139139
end
140140

141+
test "#asset_manager_id should return asset manager id" do
142+
uploader = AttachmentUploader.new(@attachment_data, "mounted-as")
143+
uploader.store!(file_fixture("sample_attachment.zip"))
144+
145+
assert_equal "asset_manager_id", uploader.asset_manager_id
146+
end
147+
141148
def required_arcgis_file_list
142149
%w[london.shp london.shx london.dbf]
143150
end

test/unit/lib/whitehall/asset_manager_storage_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,16 @@ class Whitehall::AssetManagerStorage::FileTest < ActiveSupport::TestCase
6868

6969
assert_equal model.file.path, model.file.url
7070
end
71+
72+
test "returns nil when asset is not available" do
73+
model = create(:attachment_data_with_no_assets, attachable: create(:draft_edition, id: 1))
74+
75+
assert_nil model.file.file.asset_manager_id
76+
end
77+
78+
test "returns asset_manager_id when asset is available" do
79+
model = create(:attachment_data_for_csv, attachable: create(:draft_edition, id: 1))
80+
81+
assert_equal "asset_manager_id", model.file.file.asset_manager_id
82+
end
7183
end

0 commit comments

Comments
 (0)