File tree Expand file tree Collapse file tree 4 files changed +34
-2
lines changed
lib/active_storage/attached Expand file tree Collapse file tree 4 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ def attached?
41
41
42
42
# Deletes associated attachments without purging them, leaving their respective blobs in place.
43
43
def detach
44
- attachments . destroy_all if attached?
44
+ attachments . delete_all if attached?
45
45
end
46
46
47
47
##
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ def attached?
45
45
# Deletes the attachment without purging it, leaving its blob in place.
46
46
def detach
47
47
if attached?
48
- attachment . destroy
48
+ attachment . delete
49
49
write_attachment nil
50
50
end
51
51
end
Original file line number Diff line number Diff line change @@ -315,6 +315,23 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
315
315
end
316
316
end
317
317
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
+
318
335
test "purging" do
319
336
[ create_blob ( filename : "funky.jpg" ) , create_blob ( filename : "town.jpg" ) ] . tap do |blobs |
320
337
@user . highlights . attach blobs
Original file line number Diff line number Diff line change @@ -317,6 +317,21 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase
317
317
end
318
318
end
319
319
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
+
320
335
test "purging" do
321
336
create_blob ( filename : "funky.jpg" ) . tap do |blob |
322
337
@user . avatar . attach blob
You can’t perform that action at this time.
0 commit comments