Skip to content

Commit d20d6c7

Browse files
Fix that detaching could purge
1 parent 6395107 commit d20d6c7

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

activestorage/lib/active_storage/attached/many.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def attached?
4141

4242
# Deletes associated attachments without purging them, leaving their respective blobs in place.
4343
def detach
44-
attachments.destroy_all if attached?
44+
attachments.delete_all if attached?
4545
end
4646

4747
##

activestorage/lib/active_storage/attached/one.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def attached?
4545
# Deletes the attachment without purging it, leaving its blob in place.
4646
def detach
4747
if attached?
48-
attachment.destroy
48+
attachment.delete
4949
write_attachment nil
5050
end
5151
end

activestorage/test/models/attached/many_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
315315
end
316316
end
317317

318+
test "detaching" do
319+
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
320+
@user.highlights.attach blobs
321+
assert @user.highlights.attached?
322+
323+
perform_enqueued_jobs do
324+
@user.highlights.detach
325+
end
326+
327+
assert_not @user.highlights.attached?
328+
assert ActiveStorage::Blob.exists?(blobs.first.id)
329+
assert ActiveStorage::Blob.exists?(blobs.second.id)
330+
assert ActiveStorage::Blob.service.exist?(blobs.first.key)
331+
assert ActiveStorage::Blob.service.exist?(blobs.second.key)
332+
end
333+
end
334+
318335
test "purging" do
319336
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
320337
@user.highlights.attach blobs

activestorage/test/models/attached/one_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase
317317
end
318318
end
319319

320+
test "detaching" do
321+
create_blob(filename: "funky.jpg").tap do |blob|
322+
@user.avatar.attach blob
323+
assert @user.avatar.attached?
324+
325+
perform_enqueued_jobs do
326+
@user.avatar.detach
327+
end
328+
329+
assert_not @user.avatar.attached?
330+
assert ActiveStorage::Blob.exists?(blob.id)
331+
assert ActiveStorage::Blob.service.exist?(blob.key)
332+
end
333+
end
334+
320335
test "purging" do
321336
create_blob(filename: "funky.jpg").tap do |blob|
322337
@user.avatar.attach blob

0 commit comments

Comments
 (0)