未验证 提交 b9ed1fa4 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #31970 from fatkodima/active_storage-unattached-scope

Add ActiveStorage::Blob.unattached scope
......@@ -27,6 +27,8 @@ class ActiveStorage::Blob < ActiveRecord::Base
has_many :attachments
scope :unattached, -> { left_joins(:attachments).where(ActiveStorage::Attachment.table_name => { blob_id: nil }) }
class << self
# You can used the signed ID of a blob to refer to it on the client side without fear of tampering.
# This is particularly helpful for direct uploads where the client-side needs to refer to the blob
......
......@@ -7,6 +7,23 @@
class ActiveStorage::BlobTest < ActiveSupport::TestCase
include ActiveSupport::Testing::MethodCallAssertions
test ".unattached scope returns not attached blobs" do
class UserWithHasOneAttachedDependentFalse < User
has_one_attached :avatar, dependent: false
end
ActiveStorage::Blob.delete_all
blob_1 = create_blob filename: "funky.jpg"
blob_2 = create_blob filename: "town.jpg"
user = UserWithHasOneAttachedDependentFalse.create!
user.avatar.attach blob_1
assert_equal [blob_2], ActiveStorage::Blob.unattached
user.destroy
assert_equal [blob_1, blob_2].map(&:id).sort, ActiveStorage::Blob.unattached.pluck(:id).sort
end
test "create after upload sets byte size and checksum" do
data = "Hello world!"
blob = create_blob data: data
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册