diff options
author | Nicholas Shirley <nicholas@reallymy.email> | 2018-02-12 12:29:18 +0100 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-03-06 13:03:02 -0500 |
commit | f9a5839083e697f0d5aad6d9304cd5f26e2a7a11 (patch) | |
tree | 5d69dc69db87cda7040b61d11aeb4e1787cb32c7 /activestorage/lib/active_storage/attached | |
parent | 40d3fa5dfe01d0e3bd0f6530aba3b4cddfda3969 (diff) | |
download | rails-f9a5839083e697f0d5aad6d9304cd5f26e2a7a11.tar.gz rails-f9a5839083e697f0d5aad6d9304cd5f26e2a7a11.tar.bz2 rails-f9a5839083e697f0d5aad6d9304cd5f26e2a7a11.zip |
Allow selectively purging attached blobs
Diffstat (limited to 'activestorage/lib/active_storage/attached')
-rw-r--r-- | activestorage/lib/active_storage/attached/macros.rb | 12 | ||||
-rw-r--r-- | activestorage/lib/active_storage/attached/many.rb | 18 |
2 files changed, 18 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 |