Sha256: 098cd02c5116bad0427a493f578a1f4ed1f3d3e52346dfd19f3f9715f6fa3a85

Contents?: true

Size: 1.26 KB

Versions: 19

Compression:

Stored size: 1.26 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

      def initialize(attachment, styles: {})
        @attachment = attachment
        @styles = normalize_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)
        @attachment.variant(
          resize_to_limit: size,
          strip: true
        ).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 normalize_styles(styles)
        styles.transform_values { |v| v.split('x') }
      end

      def style_to_size(style)
        @styles.fetch(style&.to_sym) { [width, height] }
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
solidus_core-2.11.15 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.14 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.0.5 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.1.5 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.13 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.0.4 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.1.4 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.12 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.0.3 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.1.3 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.1.2 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.1.1 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.1.0 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.11 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.0.2 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.10 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.0.1 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-3.0.0 app/models/concerns/spree/active_storage_adapter/attachment.rb
solidus_core-2.11.9 app/models/concerns/spree/active_storage_adapter/attachment.rb