Sha256: 09970b1ccce2e6aa29e2affa40b49132ac0d0d04236b894d27f81ab66b30f328

Contents?: true

Size: 1.71 KB

Versions: 20

Compression:

Stored size: 1.71 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[:dependent]
    end
end

ActiveSupport.run_load_hooks :active_storage_attachment, ActiveStorage::Attachment

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
activestorage-6.1.4.7 app/models/active_storage/attachment.rb
activestorage-6.1.4.6 app/models/active_storage/attachment.rb
activestorage-6.1.4.5 app/models/active_storage/attachment.rb
activestorage-6.1.4.4 app/models/active_storage/attachment.rb
activestorage-6.1.4.3 app/models/active_storage/attachment.rb
activestorage-6.1.4.2 app/models/active_storage/attachment.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activestorage-6.1.4.1/app/models/active_storage/attachment.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activestorage-6.1.4.1/app/models/active_storage/attachment.rb
activestorage-6.1.4.1 app/models/active_storage/attachment.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activestorage-6.1.4/app/models/active_storage/attachment.rb
activestorage-6.1.4 app/models/active_storage/attachment.rb
activestorage-6.1.3.2 app/models/active_storage/attachment.rb
activestorage-6.1.3.1 app/models/active_storage/attachment.rb
activestorage-6.1.3 app/models/active_storage/attachment.rb
activestorage-6.1.2.1 app/models/active_storage/attachment.rb
activestorage-6.1.2 app/models/active_storage/attachment.rb
activestorage-6.1.1 app/models/active_storage/attachment.rb
activestorage-6.1.0 app/models/active_storage/attachment.rb
activestorage-6.1.0.rc2 app/models/active_storage/attachment.rb
activestorage-6.1.0.rc1 app/models/active_storage/attachment.rb