1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/app/models/active_storage/attachment.rb

57 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-07-05 09:18:50 -04:00
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. A foreign-key constraint
# on the attachments table prevents blobs from being purged if theyre still attached to any records.
#
# Attachments also have access to all methods from {ActiveStorage::Blob}[rdoc-ref:ActiveStorage::Blob].
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
belongs_to :blob, class_name: "ActiveStorage::Blob"
2017-07-05 09:18:50 -04:00
delegate_missing_to :blob
2019-05-22 15:07:35 -04:00
after_create_commit :mirror_blob_later, :analyze_blob_later, :identify_blob
after_destroy_commit :purge_dependent_blob_later
# Synchronously deletes the attachment and {purges the blob}[rdoc-ref:ActiveStorage::Blob#purge].
2017-07-05 09:18:50 -04:00
def purge
delete
blob&.purge
2017-07-05 09:18:50 -04:00
end
# Deletes the attachment and {enqueues a background job}[rdoc-ref:ActiveStorage::Blob#purge_later] to purge the blob.
def purge_later
delete
blob&.purge_later
end
private
2018-01-15 13:06:17 -05:00
def identify_blob
blob.identify
end
def analyze_blob_later
blob.analyze_later unless blob.analyzed?
end
2019-05-22 15:07:35 -04:00
def mirror_blob_later
blob.mirror_later
end
def purge_dependent_blob_later
blob&.purge_later if dependent == :purge_later
end
def dependent
record.attachment_reflections[name]&.options[:dependent]
end
2017-07-05 09:18:50 -04:00
end
ActiveSupport.run_load_hooks :active_storage_attachment, ActiveStorage::Attachment