Sha256: 3b583d6fc1d989930606fa3ff4b5e62dd304174ca3226840c736a95ea123e815

Contents?: true

Size: 1.88 KB

Versions: 34

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

# = Active Storage \Variant With Record
#
# Like an ActiveStorage::Variant, but keeps detail about the variant in the database as an
# ActiveStorage::VariantRecord. This is only used if +ActiveStorage.track_variants+ is enabled.
class ActiveStorage::VariantWithRecord
  include ActiveStorage::Blob::Servable

  attr_reader :blob, :variation
  delegate :service, to: :blob
  delegate :content_type, to: :variation

  def initialize(blob, variation)
    @blob, @variation = blob, ActiveStorage::Variation.wrap(variation)
  end

  def processed
    process unless processed?
    self
  end

  def image
    record&.image
  end

  def filename
    ActiveStorage::Filename.new "#{blob.filename.base}.#{variation.format.downcase}"
  end

  # Destroys record and deletes file from service.
  def destroy
    record&.destroy
  end

  delegate :key, :url, :download, to: :image, allow_nil: true

  private
    def processed?
      record.present?
    end

    def process
      transform_blob { |image| create_or_find_record(image: image) }
    end

    def transform_blob
      blob.open do |input|
        variation.transform(input) do |output|
          yield io: output, filename: "#{blob.filename.base}.#{variation.format.downcase}",
            content_type: variation.content_type, service_name: blob.service.name
        end
      end
    end

    def create_or_find_record(image:)
      @record =
        ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role) do
          blob.variant_records.create_or_find_by!(variation_digest: variation.digest) do |record|
            record.image.attach(image)
          end
        end
    end

    def record
      @record ||= if blob.variant_records.loaded?
        blob.variant_records.find { |v| v.variation_digest == variation.digest }
      else
        blob.variant_records.find_by(variation_digest: variation.digest)
      end
    end
end

Version data entries

34 entries across 34 versions & 5 rubygems

Version Path
activestorage-8.0.2 app/models/active_storage/variant_with_record.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activestorage-7.1.3.4/app/models/active_storage/variant_with_record.rb
activestorage-8.0.1 app/models/active_storage/variant_with_record.rb
activestorage-8.0.0.1 app/models/active_storage/variant_with_record.rb
activestorage-7.2.2.1 app/models/active_storage/variant_with_record.rb
activestorage-7.1.5.1 app/models/active_storage/variant_with_record.rb
activestorage-8.0.0 app/models/active_storage/variant_with_record.rb
activestorage-7.2.2 app/models/active_storage/variant_with_record.rb
activestorage-7.1.5 app/models/active_storage/variant_with_record.rb
activestorage-8.0.0.rc2 app/models/active_storage/variant_with_record.rb
activestorage-7.2.1.2 app/models/active_storage/variant_with_record.rb
activestorage-7.1.4.2 app/models/active_storage/variant_with_record.rb
activestorage-8.0.0.rc1 app/models/active_storage/variant_with_record.rb
activestorage-7.2.1.1 app/models/active_storage/variant_with_record.rb
activestorage-7.1.4.1 app/models/active_storage/variant_with_record.rb
activestorage-8.0.0.beta1 app/models/active_storage/variant_with_record.rb
omg-activestorage-8.0.0.alpha9 app/models/active_storage/variant_with_record.rb
omg-activestorage-8.0.0.alpha8 app/models/active_storage/variant_with_record.rb
omg-activestorage-8.0.0.alpha7 app/models/active_storage/variant_with_record.rb
omg-activestorage-8.0.0.alpha4 app/models/active_storage/variant_with_record.rb