mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Synchronously destroy attachments
This commit is contained in:
parent
99f75820bb
commit
d3e7dc6f16
2 changed files with 7 additions and 9 deletions
|
@ -1,11 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Provides delayed purging of attachments or blobs using their +purge_later+ method.
|
||||
# Provides delayed purging of ActiveStorage::Blob records via ActiveStorage::Blob#purge_later.
|
||||
class ActiveStorage::PurgeJob < ActiveJob::Base
|
||||
# FIXME: Limit this to a custom ActiveStorage error
|
||||
retry_on StandardError
|
||||
|
||||
def perform(attachment_or_blob)
|
||||
attachment_or_blob.purge
|
||||
def perform(blob)
|
||||
blob.purge
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,17 +14,15 @@ class ActiveStorage::Attachment < ActiveRecord::Base
|
|||
|
||||
delegate_missing_to :blob
|
||||
|
||||
# Purging an attachment will purge the blob (delete the file on the service, then destroy the record)
|
||||
# and then destroy the attachment itself.
|
||||
# Synchronously purges the blob (deletes it from the configured service) and destroys the attachment.
|
||||
def purge
|
||||
blob.purge
|
||||
destroy
|
||||
end
|
||||
|
||||
# Purging an attachment means purging the blob, which means talking to the service, which means
|
||||
# talking over the Internet. Whenever you're doing that, it's a good idea to put that work in a job,
|
||||
# so it doesn't hold up other operations. That's what +purge_later+ provides.
|
||||
# Destroys the attachment and asynchronously purges the blob (deletes it from the configured service).
|
||||
def purge_later
|
||||
ActiveStorage::PurgeJob.perform_later(self)
|
||||
blob.purge_later
|
||||
destroy
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue