mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
6aa26c30e2
An Active Storage `Blob` must be identified before it can be reliably validated. For direct uploads, a `Blob` is identified when it is attached, rather than when it is created. Before this commit, the sequence of events when attaching a `Blob` was: 1. Find the `Blob`. 2. Assign the `Blob` to an `Attachment`. 3. Save the owner record. 4. Save the `Attachment`. 5. Identify the `Blob`'s true `content_type` from its file. 6. Save the `Blob`. This meant that the owner record's validations might not see the `Blob`'s true `content_type`. After this commit, the sequence of events will be: 1. Find the `Blob`. 2. Identify the `Blob`'s true `content_type` from its file. 3. Assign the `Blob` to an `Attachment`. 4. Save the owner record. 5. Save the `Attachment`. 6. Save the `Blob`. Thus the `Blob`'s true `content_type` will be available when running the owner record's validations.
58 lines
1.7 KiB
Ruby
58 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
||
|
||
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 they’re 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"
|
||
|
||
belongs_to :record, polymorphic: true, touch: true
|
||
belongs_to :blob, class_name: "ActiveStorage::Blob", autosave: true
|
||
|
||
delegate_missing_to :blob
|
||
delegate :signed_id, to: :blob
|
||
|
||
after_create_commit :mirror_blob_later, :analyze_blob_later
|
||
after_destroy_commit :purge_dependent_blob_later
|
||
|
||
# Synchronously deletes the attachment and {purges the blob}[rdoc-ref:ActiveStorage::Blob#purge].
|
||
def purge
|
||
transaction do
|
||
delete
|
||
record&.touch
|
||
end
|
||
blob&.purge
|
||
end
|
||
|
||
# Deletes the attachment and {enqueues a background job}[rdoc-ref:ActiveStorage::Blob#purge_later] to purge the blob.
|
||
def purge_later
|
||
transaction do
|
||
delete
|
||
record&.touch
|
||
end
|
||
blob&.purge_later
|
||
end
|
||
|
||
private
|
||
def analyze_blob_later
|
||
blob.analyze_later unless blob.analyzed?
|
||
end
|
||
|
||
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
|
||
end
|
||
|
||
ActiveSupport.run_load_hooks :active_storage_attachment, ActiveStorage::Attachment
|