2017-07-05 09:18:50 -04:00
|
|
|
require "active_support/core_ext/module/delegation"
|
|
|
|
|
2017-08-03 14:41:23 -04:00
|
|
|
# Attachments associate records with blobs. Usually that's a one record-many blobs relationship,
|
2017-07-24 13:05:15 -04:00
|
|
|
# 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).
|
2017-07-06 05:33:29 -04:00
|
|
|
class ActiveStorage::Attachment < ActiveRecord::Base
|
|
|
|
self.table_name = "active_storage_attachments"
|
2017-07-05 09:18:50 -04:00
|
|
|
|
2017-07-09 12:48:26 -04:00
|
|
|
belongs_to :record, polymorphic: true
|
2017-07-06 05:33:29 -04:00
|
|
|
belongs_to :blob, class_name: "ActiveStorage::Blob"
|
2017-07-05 09:18:50 -04:00
|
|
|
|
|
|
|
delegate_missing_to :blob
|
|
|
|
|
2017-07-24 13:05:15 -04:00
|
|
|
# Purging an attachment will purge the blob (delete the file on the service, then destroy the record)
|
|
|
|
# and then destroy the attachment itself.
|
2017-07-05 09:18:50 -04:00
|
|
|
def purge
|
|
|
|
blob.purge
|
|
|
|
destroy
|
|
|
|
end
|
2017-07-05 12:31:49 -04:00
|
|
|
|
2017-07-24 13:05:15 -04:00
|
|
|
# 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,
|
2017-08-10 23:36:07 -04:00
|
|
|
# so it doesn't hold up other operations. That's what +#purge_later+ provides.
|
2017-07-05 12:31:49 -04:00
|
|
|
def purge_later
|
2017-07-06 05:33:29 -04:00
|
|
|
ActiveStorage::PurgeJob.perform_later(self)
|
2017-07-05 12:31:49 -04:00
|
|
|
end
|
2017-07-05 09:18:50 -04:00
|
|
|
end
|