2017-08-12 08:32:15 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
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,
|
2018-10-23 21:25:15 -04:00
|
|
|
|
# but it is possible to associate many different records with the same blob. A foreign-key constraint
|
|
|
|
|
# on the attachments table prevents blobs from being purged if they’re still attached to any records.
|
2019-09-20 21:43:29 -04:00
|
|
|
|
#
|
|
|
|
|
# Attachments also have access to all methods from {ActiveStorage::Blob}[rdoc-ref:ActiveStorage::Blob].
|
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-08-31 20:10:56 -04:00
|
|
|
|
belongs_to :record, polymorphic: true, touch: 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
|
2020-05-28 09:20:22 -04:00
|
|
|
|
delegate :signed_id, to: :blob
|
2017-07-05 09:18:50 -04:00
|
|
|
|
|
2019-05-22 15:07:35 -04:00
|
|
|
|
after_create_commit :mirror_blob_later, :analyze_blob_later, :identify_blob
|
2018-07-07 23:25:33 -04:00
|
|
|
|
after_destroy_commit :purge_dependent_blob_later
|
2017-10-22 13:16:59 -04:00
|
|
|
|
|
2018-07-18 21:56:36 -04:00
|
|
|
|
# Synchronously deletes the attachment and {purges the blob}[rdoc-ref:ActiveStorage::Blob#purge].
|
2017-07-05 09:18:50 -04:00
|
|
|
|
def purge
|
2020-07-20 20:44:32 -04:00
|
|
|
|
transaction do
|
|
|
|
|
delete
|
|
|
|
|
record&.touch
|
|
|
|
|
end
|
2018-07-30 21:42:20 -04:00
|
|
|
|
blob&.purge
|
2017-07-05 09:18:50 -04:00
|
|
|
|
end
|
2017-07-05 12:31:49 -04:00
|
|
|
|
|
2018-07-18 21:56:36 -04:00
|
|
|
|
# Deletes the attachment and {enqueues a background job}[rdoc-ref:ActiveStorage::Blob#purge_later] to purge the blob.
|
2017-07-05 12:31:49 -04:00
|
|
|
|
def purge_later
|
2020-07-20 20:44:32 -04:00
|
|
|
|
transaction do
|
|
|
|
|
delete
|
|
|
|
|
record&.touch
|
|
|
|
|
end
|
2018-07-30 21:42:20 -04:00
|
|
|
|
blob&.purge_later
|
2017-07-05 12:31:49 -04:00
|
|
|
|
end
|
2017-10-22 13:16:59 -04:00
|
|
|
|
|
|
|
|
|
private
|
2018-01-15 13:06:17 -05:00
|
|
|
|
def identify_blob
|
|
|
|
|
blob.identify
|
|
|
|
|
end
|
|
|
|
|
|
2017-10-22 13:16:59 -04:00
|
|
|
|
def analyze_blob_later
|
|
|
|
|
blob.analyze_later unless blob.analyzed?
|
|
|
|
|
end
|
2018-07-07 23:25:33 -04:00
|
|
|
|
|
2019-05-22 15:07:35 -04:00
|
|
|
|
def mirror_blob_later
|
|
|
|
|
blob.mirror_later
|
|
|
|
|
end
|
|
|
|
|
|
2018-07-07 23:25:33 -04:00
|
|
|
|
def purge_dependent_blob_later
|
2018-07-30 21:42:20 -04:00
|
|
|
|
blob&.purge_later if dependent == :purge_later
|
2018-07-07 23:25:33 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def dependent
|
|
|
|
|
record.attachment_reflections[name]&.options[:dependent]
|
|
|
|
|
end
|
2017-07-05 09:18:50 -04:00
|
|
|
|
end
|
2019-03-28 18:52:02 -04:00
|
|
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks :active_storage_attachment, ActiveStorage::Attachment
|