提交 499a4164 编写于 作者: G George Claghorn

Introduce ActiveStorage::Attached::{One,Many}#detach

上级 82022bd2
......@@ -13,7 +13,6 @@ def attachments
end
# Associates one or several attachments with the current record, saving them to the database.
# Examples:
#
# document.images.attach(params[:images]) # Array of ActionDispatch::Http::UploadedFile objects
# document.images.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
......@@ -36,6 +35,11 @@ def attached?
attachments.any?
end
# Deletes associated attachments without purging them, leaving their respective blobs in place.
def detach
attachments.destroy_all if attached?
end
# Directly purges each associated attachment (i.e. destroys the blobs and
# attachments and deletes the files on the service).
def purge
......
......@@ -14,7 +14,6 @@ def attachment
end
# Associates a given attachment with the current record, saving it to the database.
# Examples:
#
# person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object
# person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
......@@ -39,6 +38,14 @@ def attached?
attachment.present?
end
# Deletes the attachment without purging it, leaving its blob in place.
def detach
if attached?
attachment.destroy
write_attachment nil
end
end
# Directly purges the attachment (i.e. destroys the blob and
# attachment and deletes the file on the service).
def purge
......@@ -59,16 +66,12 @@ def purge_later
def replace(attachable)
blob.tap do
transaction do
destroy_attachment
detach
write_attachment create_attachment_from(attachable)
end
end.purge_later
end
def destroy_attachment
attachment.destroy
end
def create_attachment_from(attachable)
ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
end
......
......@@ -98,6 +98,17 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
assert_equal "bar", blob.reload.metadata[:foo]
end
test "detach blob" do
@user.avatar.attach create_blob(filename: "funky.jpg")
avatar_blob_id = @user.avatar.blob.id
avatar_key = @user.avatar.key
@user.avatar.detach
assert_not @user.avatar.attached?
assert ActiveStorage::Blob.exists?(avatar_blob_id)
assert ActiveStorage::Blob.service.exist?(avatar_key)
end
test "purge attached blob" do
@user.avatar.attach create_blob(filename: "funky.jpg")
avatar_key = @user.avatar.key
......@@ -218,6 +229,21 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
end
test "detach blobs" do
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
highlight_blob_ids = @user.highlights.collect { |highlight| highlight.blob.id }
highlight_keys = @user.highlights.collect(&:key)
@user.highlights.detach
assert_not @user.highlights.attached?
assert ActiveStorage::Blob.exists?(highlight_blob_ids.first)
assert ActiveStorage::Blob.exists?(highlight_blob_ids.second)
assert ActiveStorage::Blob.service.exist?(highlight_keys.first)
assert ActiveStorage::Blob.service.exist?(highlight_keys.second)
end
test "purge attached blobs" do
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
highlight_keys = @user.highlights.collect(&:key)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册