Sha256: 5ba047611029fe6ca3543124ab6ae17f584d2889e2d2b180717088b91236d658

Contents?: true

Size: 1.72 KB

Versions: 11

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require 'mini_magick'

module Spree
  module ActiveStorageAdapter
    # Decorates ActiveStorage attachment to add methods expected by Solidus'
    # Paperclip-oriented attachment support.
    class Attachment
      delegate_missing_to :@attachment

      attr_reader :attachment

      def initialize(attachment, styles: {})
        @attachment = attachment
        @transformations = styles_to_transformations(styles)
      end

      def exists?
        attached?
      end

      def filename
        blob&.filename.to_s
      end

      def url(style = nil)
        variant(style)&.url
      end

      def variant(style = nil)
        transformation = @transformations[style] || default_transformation(width, height)

        @attachment.variant({
          saver: {
            strip: true
          }
        }.merge(transformation)).processed
      end

      def height
        metadata[:height]
      end

      def width
        metadata[:width]
      end

      def destroy
        return false unless attached?

        purge
        true
      end

      private

      def metadata
        analyze unless analyzed?

        @attachment.metadata
      end

      def styles_to_transformations(styles)
        styles.transform_values(&method(:imagemagick_to_image_processing_definition))
      end

      def imagemagick_to_image_processing_definition(definition)
        width_height = definition.split('x').map(&:to_i)

        case definition[-1].to_sym
        when :^
          { resize_to_fill: width_height }
        else
          default_transformation(*width_height)
        end
      end

      def default_transformation(width, height)
        { resize_to_limit: [width, height] }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
solidus_core-3.2.9 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.8 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.7 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.6 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.5 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.4 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.3 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.2 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.1 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.0 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.2.0.alpha app/models/concerns/spree/active_storage_adapter/attachment.rb