Sha256: b2f190cd1b43c99bd0a47b10ccf33aa5515e129ad3e2e15e870d81deb63bf8ae

Contents?: true

Size: 1.72 KB

Versions: 17

Compression:

Stored size: 1.72 KB

Contents

# 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 < ActiveStorage::Record
  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&.fetch(:dependent, nil)
    end
end

ActiveSupport.run_load_hooks :active_storage_attachment, ActiveStorage::Attachment

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
activestorage-6.1.7.10 app/models/active_storage/attachment.rb
activestorage-6.1.7.9 app/models/active_storage/attachment.rb
activestorage-6.1.7.8 app/models/active_storage/attachment.rb
activestorage-6.1.7.7 app/models/active_storage/attachment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-6.1.6.1/app/models/active_storage/attachment.rb
activestorage-6.1.7.6 app/models/active_storage/attachment.rb
activestorage-6.1.7.5 app/models/active_storage/attachment.rb
activestorage-6.1.7.4 app/models/active_storage/attachment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activestorage-6.1.6.1/app/models/active_storage/attachment.rb
activestorage-6.1.7.3 app/models/active_storage/attachment.rb
activestorage-6.1.7.2 app/models/active_storage/attachment.rb
activestorage-6.1.7.1 app/models/active_storage/attachment.rb
activestorage-6.1.7 app/models/active_storage/attachment.rb
activestorage-6.1.6.1 app/models/active_storage/attachment.rb
activestorage-6.1.6 app/models/active_storage/attachment.rb
activestorage-6.1.5.1 app/models/active_storage/attachment.rb
activestorage-6.1.5 app/models/active_storage/attachment.rb