From fd61bae02cd9609677fc24b5c02f509f6e20232e Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Thu, 28 May 2020 09:20:22 -0400 Subject: [PATCH] ActiveStorage::Attachment#signed_id must return a signed *blob* ID Fixes that calling e.g. `url_for @user.avatar` would return an incorrect URL resulting in a 404. The Active Storage URL helper ends up calling ActiveStorage::Attachment#signed_id, which previously delegated to ActiveStorage::Blob#signed_id but changed to return a signed *attachment* ID in 1a3dc42. The Active Storage controllers pass signed IDs to ActiveStorage::Blob.find_signed, so we need signed blob IDs. --- activestorage/app/models/active_storage/attachment.rb | 1 + activestorage/test/models/attachment_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/activestorage/app/models/active_storage/attachment.rb b/activestorage/app/models/active_storage/attachment.rb index 8938eed825..d0af5265b3 100644 --- a/activestorage/app/models/active_storage/attachment.rb +++ b/activestorage/app/models/active_storage/attachment.rb @@ -14,6 +14,7 @@ class ActiveStorage::Attachment < ActiveRecord::Base belongs_to :blob, class_name: "ActiveStorage::Blob" delegate_missing_to :blob + delegate :signed_id, to: :blob after_create_commit :mirror_blob_later, :analyze_blob_later, :identify_blob after_destroy_commit :purge_dependent_blob_later diff --git a/activestorage/test/models/attachment_test.rb b/activestorage/test/models/attachment_test.rb index 814584126d..88e83c32ab 100644 --- a/activestorage/test/models/attachment_test.rb +++ b/activestorage/test/models/attachment_test.rb @@ -49,4 +49,12 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase assert ActiveStorage::Blob.service.mirrors.second.exist?(blob.key) end end + + test "getting a signed blob ID from an attachment" do + blob = create_blob + @user.avatar.attach(blob) + + signed_id = @user.avatar.signed_id + assert_equal blob, ActiveStorage::Blob.find_signed(signed_id) + end end -- GitLab