aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/app/models/active_storage/attachment.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-08-04 18:05:13 -0500
committerGitHub <noreply@github.com>2017-08-04 18:05:13 -0500
commit552840660389e39f3ba8e47dcf35ab817c01cb48 (patch)
tree5013b2d5a1691ac1f675935eea38848639ac54bc /activestorage/app/models/active_storage/attachment.rb
parent978b3d604ab082ac0be071245646b0803b8ff382 (diff)
parent3179f089be4f631b9c0f8b431567992164f2bdb4 (diff)
downloadrails-552840660389e39f3ba8e47dcf35ab817c01cb48.tar.gz
rails-552840660389e39f3ba8e47dcf35ab817c01cb48.tar.bz2
rails-552840660389e39f3ba8e47dcf35ab817c01cb48.zip
Merge pull request #30020 from rails/active-storage-import
Add Active Storage to Rails
Diffstat (limited to 'activestorage/app/models/active_storage/attachment.rb')
-rw-r--r--activestorage/app/models/active_storage/attachment.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/activestorage/app/models/active_storage/attachment.rb b/activestorage/app/models/active_storage/attachment.rb
new file mode 100644
index 0000000000..e94fc69bba
--- /dev/null
+++ b/activestorage/app/models/active_storage/attachment.rb
@@ -0,0 +1,28 @@
+require "active_support/core_ext/module/delegation"
+
+# Attachments associate records with blobs. Usually that's a one record-many blobs relationship,
+# but it is possible to associate many different records with the same blob. If you're doing that,
+# you'll want to declare with `has_one/many_attached :thingy, dependent: false`, so that destroying
+# any one record won't destroy the blob as well. (Then you'll need to do your own garbage collecting, though).
+class ActiveStorage::Attachment < ActiveRecord::Base
+ self.table_name = "active_storage_attachments"
+
+ belongs_to :record, polymorphic: true
+ belongs_to :blob, class_name: "ActiveStorage::Blob"
+
+ 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.
+ 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.
+ def purge_later
+ ActiveStorage::PurgeJob.perform_later(self)
+ end
+end