Sha256: 8f1794ca0d22091eba4764e4f75ced64cc852f011479feb574a8974caab8ecb5

Contents?: true

Size: 1.22 KB

Versions: 10

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'mini_magick'

module Spree
  module ActiveStorageAdapter
    # Decorares AtiveStorage attachment to add methods exptected by Solidus'
    # Paperclip-oriented attachment support.
    class Attachment
      delegate_missing_to :@attachment

      DEFAULT_SIZE = '100%'

      def initialize(attachment, styles: {})
        @attachment = attachment
        @styles = 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)
        size = style_to_size(style&.to_sym)
        @attachment.variant(
          resize: size,
          strip: true,
          'auto-orient': true,
          colorspace: 'sRGB',
        ).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 style_to_size(style)
        @styles.fetch(style) { DEFAULT_SIZE }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
solidus_core-2.11.8 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.7 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.6 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.0.0.rc2 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.5 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.4 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.3 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.2 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.1 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.0 app/models/concerns/spree/active_storage_adapter/attachment.rb