Sha256: dbe4f949ba0c4076aa2db59c7cc73249a50db72613ac8758e99207726bf6b345

Contents?: true

Size: 971 Bytes

Versions: 2

Compression:

Stored size: 971 Bytes

Contents

# frozen_string_literal: true

# Ability to auto apply :default variant before uploading original image
module ActivetoragePreprocessDefaultVariation
  def self.prepended(base)
    base.extend(ClassMethods)
  end

  def upload_without_unfurling(io) # rubocop:disable Metrics/MethodLength
    variant = attachments.first.try(:send, :variants)
    default_variant = variant ? variant[:default] : nil
    if default_variant && self.class.enabled_default_variant?
      ActiveStorage::Variation.wrap(default_variant).transform(io) do |output|
        unfurl output, identify: identify
        save! if id.present? # update new unfurl information
        super(output)
      end
    else
      super(io)
    end
  end

  module ClassMethods
    # To improve testing performance, we don't want to preprocess images in test environment
    def enabled_default_variant?
      !Rails.env.test?
    end
  end
end

ActiveStorage::Blob.prepend ActivetoragePreprocessDefaultVariation

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activestorage-delayed-0.1.4 initializers/upload_default_variation.rb
activestorage-delayed-0.1.3 initializers/upload_default_variation.rb