Sha256: c80a0311ab6e06d852b581c1a59f415e7763ea715961f5afa6746cb56719b16d

Contents?: true

Size: 1.56 KB

Versions: 14

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

# 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
  attr_reader :blob, :variation
  delegate :service, to: :blob

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

  def processed
    process
    self
  end

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

  def processed?
    record.present?
  end

  def image
    record&.image
  end

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

  private
    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

14 entries across 14 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.2.3/app/models/active_storage/variant_with_record.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activestorage-7.0.2.3/app/models/active_storage/variant_with_record.rb
activestorage-7.0.2.4 app/models/active_storage/variant_with_record.rb
activestorage-7.0.2.3 app/models/active_storage/variant_with_record.rb
activestorage-7.0.2.2 app/models/active_storage/variant_with_record.rb
activestorage-7.0.2.1 app/models/active_storage/variant_with_record.rb
activestorage-7.0.2 app/models/active_storage/variant_with_record.rb
activestorage-7.0.1 app/models/active_storage/variant_with_record.rb
activestorage-7.0.0 app/models/active_storage/variant_with_record.rb
activestorage-7.0.0.rc3 app/models/active_storage/variant_with_record.rb
activestorage-7.0.0.rc2 app/models/active_storage/variant_with_record.rb
activestorage-7.0.0.rc1 app/models/active_storage/variant_with_record.rb
activestorage-7.0.0.alpha2 app/models/active_storage/variant_with_record.rb
activestorage-7.0.0.alpha1 app/models/active_storage/variant_with_record.rb