Sha256: b488dca04882547875056c355f2dad92533aa42247211be019cf8d309850340f

Contents?: true

Size: 1009 Bytes

Versions: 3

Compression:

Stored size: 1009 Bytes

Contents

# frozen_string_literal: true

module Spree
  module ActiveStorageAdapter
    # Contains normalization methods to make objects compliant with
    # ActiveStorage API.
    module Normalization
      # Normalizes an attachable
      def normalize_attachable(attachable)
        case attachable
        when ActiveStorage::Blob, ActionDispatch::Http::UploadedFile,
          Rack::Test::UploadedFile, Hash, String
          attachable
        when Attachment, ActiveStorage::Attached
          attachable_blob(attachable)
        else # assume it's an IO
          attachable_io(attachable)
        end
      end

      private

      def attachable_blob(attachable)
        attachable.blob
      end

      def attachable_io(attachable)
        filename = if attachable.respond_to?(:to_path)
                     attachable.to_path
                   else
                     SecureRandom.uuid
                   end
        attachable.rewind

        { io: attachable, filename: }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_core-4.4.2 app/models/concerns/spree/active_storage_adapter/normalization.rb
solidus_core-4.4.1 app/models/concerns/spree/active_storage_adapter/normalization.rb
solidus_core-4.4.0 app/models/concerns/spree/active_storage_adapter/normalization.rb