aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicholas Shirley <nicholas@reallymy.email>2018-02-12 12:29:18 +0100
committerGeorge Claghorn <george@basecamp.com>2018-03-06 13:03:02 -0500
commitf9a5839083e697f0d5aad6d9304cd5f26e2a7a11 (patch)
tree5d69dc69db87cda7040b61d11aeb4e1787cb32c7
parent40d3fa5dfe01d0e3bd0f6530aba3b4cddfda3969 (diff)
downloadrails-f9a5839083e697f0d5aad6d9304cd5f26e2a7a11.tar.gz
rails-f9a5839083e697f0d5aad6d9304cd5f26e2a7a11.tar.bz2
rails-f9a5839083e697f0d5aad6d9304cd5f26e2a7a11.zip
Allow selectively purging attached blobs
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb12
-rw-r--r--activestorage/lib/active_storage/attached/many.rb18
-rw-r--r--activestorage/test/models/attachments_test.rb28
3 files changed, 46 insertions, 12 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index 1bda86369e..819f00cc06 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -85,7 +85,17 @@ module ActiveStorage
end
CODE
- has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: false
+ has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: false do
+ def purge
+ each(&:purge)
+ reset
+ end
+
+ def purge_later
+ each(&:purge_later)
+ reset
+ end
+ end
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob
scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) }
diff --git a/activestorage/lib/active_storage/attached/many.rb b/activestorage/lib/active_storage/attached/many.rb
index 6eace65b79..d61acb6fad 100644
--- a/activestorage/lib/active_storage/attached/many.rb
+++ b/activestorage/lib/active_storage/attached/many.rb
@@ -44,20 +44,16 @@ module ActiveStorage
attachments.destroy_all if attached?
end
+ ##
+ # :method: purge
+ #
# Directly purges each associated attachment (i.e. destroys the blobs and
# attachments and deletes the files on the service).
- def purge
- if attached?
- attachments.each(&:purge)
- attachments.reload
- end
- end
+
+ ##
+ # :method: purge_later
+ #
# Purges each associated attachment through the queuing system.
- def purge_later
- if attached?
- attachments.each(&:purge_later)
- end
- end
end
end
diff --git a/activestorage/test/models/attachments_test.rb b/activestorage/test/models/attachments_test.rb
index 36e76f2845..29b83eb841 100644
--- a/activestorage/test/models/attachments_test.rb
+++ b/activestorage/test/models/attachments_test.rb
@@ -418,4 +418,32 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
@user.destroy
assert_not ActiveStorage::Attachment.exists?(record: @user, name: "vlogs")
end
+
+ test "selectively purge one attached blob of many" do
+ first_blob = create_blob(filename: "funky.jpg")
+ second_blob = create_blob(filename: "wonky.jpg")
+ attachments = @user.highlights.attach(first_blob, second_blob)
+
+ assert_difference -> { ActiveStorage::Blob.count }, -1 do
+ @user.highlights.where(id: attachments.first.id).purge
+ end
+
+ assert_not ActiveStorage::Blob.exists?(key: first_blob.key)
+ assert ActiveStorage::Blob.exists?(key: second_blob.key)
+ end
+
+ test "selectively purge one attached blob of many later" do
+ first_blob = create_blob(filename: "funky.jpg")
+ second_blob = create_blob(filename: "wonky.jpg")
+ attachments = @user.highlights.attach(first_blob, second_blob)
+
+ perform_enqueued_jobs do
+ assert_difference -> { ActiveStorage::Blob.count }, -1 do
+ @user.highlights.where(id: attachments.first.id).purge_later
+ end
+ end
+
+ assert_not ActiveStorage::Blob.exists?(key: first_blob.key)
+ assert ActiveStorage::Blob.exists?(key: second_blob.key)
+ end
end