Sha256: 2374610bee7077801ce49c975946eb28848705e506d80edfb59fe02782835f6f

Contents?: true

Size: 1.05 KB

Versions: 8

Compression:

Stored size: 1.05 KB

Contents

require 'shrine/storage/url'

class Shrine
  module Plugins
    # Set file location to "asset/#{asset_uuid_id}/#{unique_file_id}" -- regardless of
    # asset sub-class, since they all have unique ids, just all under asset/.
    #
    # If no Asset pk is available (direct upload or unsaved Asset), will be stored just
    # under "asset/#{unique_file_id}.#{suffix}"
    #
    # We are choosing to store under Asset UUID PK instead of friendlier_id, friendlier_id
    # is good for public URLs and UI, but actual PK is more reliable/immutable.
    module KitheStorageLocation
      module InstanceMethods
        def generate_location(io, context)
          # If it doesn't have a id, we're probably storing in cache, possibly as part
          # of direct upload endpoint. A better path will be created on store.
          id = context[:record].id if context[:record].respond_to?(:id)

          basename = super

          ["asset", id, basename].compact.join("/")
        end
      end
    end
    register_plugin(:kithe_storage_location, KitheStorageLocation)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
kithe-2.0.0.pre.alpha2 lib/shrine/plugins/kithe_storage_location.rb
kithe-2.0.0.pre.alpha1 lib/shrine/plugins/kithe_storage_location.rb
kithe-1.1.2 lib/shrine/plugins/kithe_storage_location.rb
kithe-1.1.1 lib/shrine/plugins/kithe_storage_location.rb
kithe-1.1.0 lib/shrine/plugins/kithe_storage_location.rb
kithe-1.0.0 lib/shrine/plugins/kithe_storage_location.rb
kithe-0.3.0 lib/shrine/plugins/kithe_storage_location.rb
kithe-0.2.0 lib/shrine/plugins/kithe_storage_location.rb