Sha256: dc3a1df597b947101c7a97a984d4e836e47a23567743e0168c3730713bbd0339

Contents?: true

Size: 1.27 KB

Versions: 28

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

class ActiveStorage::VariantWithRecord
  attr_reader :blob, :variation

  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

  alias_method :service_url, :url
  deprecate service_url: :url

  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::Base.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 ||= blob.variant_records.find_by(variation_digest: variation.digest)
    end
end

Version data entries

28 entries across 28 versions & 4 rubygems

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