diff options
-rw-r--r-- | lib/active_storage/attached.rb | 4 | ||||
-rw-r--r-- | test/attachments_test.rb | 5 |
2 files changed, 9 insertions, 0 deletions
diff --git a/lib/active_storage/attached.rb b/lib/active_storage/attached.rb index 7475c38999..d5ded51e2b 100644 --- a/lib/active_storage/attached.rb +++ b/lib/active_storage/attached.rb @@ -4,6 +4,8 @@ require "active_storage/attachment" require "action_dispatch/http/upload" require "active_support/core_ext/module/delegation" +require "global_id/locator" + class ActiveStorage::Attached attr_reader :name, :record @@ -23,6 +25,8 @@ class ActiveStorage::Attached content_type: attachable.content_type when Hash ActiveStorage::Blob.create_after_upload!(attachable) + when String + GlobalID::Locator.locate_signed(attachable) else nil end diff --git a/test/attachments_test.rb b/test/attachments_test.rb index 33bbff716d..ec7e9fcd5b 100644 --- a/test/attachments_test.rb +++ b/test/attachments_test.rb @@ -25,6 +25,11 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase assert_equal "funky.jpg", @user.avatar.filename.to_s end + test "attach existing sgid blob" do + @user.avatar.attach create_blob(filename: "funky.jpg").to_sgid.to_s + assert_equal "funky.jpg", @user.avatar.filename.to_s + end + test "attach new blob" do @user.avatar.attach io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" assert_equal "town.jpg", @user.avatar.filename.to_s |