diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2018-02-12 20:55:23 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-12 20:55:23 -0500 |
commit | b9ed1fa4442633e1328dcde5a37a472b22003a6f (patch) | |
tree | 25c63154b5c693c516dba05e6369609a1072b09e | |
parent | 2c4e9c678bcfae23fcee28c105577403ebb2aa00 (diff) | |
parent | 0c463f50eaf939042ea9561eed85146612daab5e (diff) | |
download | rails-b9ed1fa4442633e1328dcde5a37a472b22003a6f.tar.gz rails-b9ed1fa4442633e1328dcde5a37a472b22003a6f.tar.bz2 rails-b9ed1fa4442633e1328dcde5a37a472b22003a6f.zip |
Merge pull request #31970 from fatkodima/active_storage-unattached-scope
Add ActiveStorage::Blob.unattached scope
-rw-r--r-- | activestorage/app/models/active_storage/blob.rb | 2 | ||||
-rw-r--r-- | activestorage/test/models/blob_test.rb | 17 |
2 files changed, 19 insertions, 0 deletions
diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index bca9f4c590..3bcd7599d0 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -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 diff --git a/activestorage/test/models/blob_test.rb b/activestorage/test/models/blob_test.rb index 9b555f9a1d..779e47ffb6 100644 --- a/activestorage/test/models/blob_test.rb +++ b/activestorage/test/models/blob_test.rb @@ -7,6 +7,23 @@ require "active_support/testing/method_call_assertions" 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 |