module Shrine::Plugins::KitheStorageLocation::InstanceMethods

Public Instance Methods

generate_location(io, derivative: nil, **context) click to toggle source
Calls superclass method
# File lib/shrine/plugins/kithe_storage_location.rb, line 35
def generate_location(io, derivative: nil, **context)
  original = super

  if derivative
    _kithe_generate_derivative_location(io, original: original, derivative: derivative, **context)
  else
    _kithe_generate_main_location(io, original: original, **context)
  end
end

Private Instance Methods

_kithe_generate_derivative_location(io, original:, derivative:, record:, **context) click to toggle source

Usually NOT in the same bucket/prefix as the originals/main attachments. You can set a prefix yourself in your shrine storage config if you want them on the same bucket, and probably should.

# File lib/shrine/plugins/kithe_storage_location.rb, line 60
def _kithe_generate_derivative_location(io, original:, derivative:, record:, **context)
  # for now to be save, insist the record exist and have an id so we can get the
  # correct derivative location. This is consistent with kithe 1.x behavior. We can
  # enhance later maybe.
  unless record && record.id
    raise TypeError.new("Can't determine correct derivative location without a persisted record. Record: #{record}")
  end
  unless derivative && original
    raise ArgumentError.new("Missing required argument")
  end

  [record.id, derivative, original].join("/")
end
_kithe_generate_main_location(io, original:, **context) click to toggle source
# File lib/shrine/plugins/kithe_storage_location.rb, line 47
def _kithe_generate_main_location(io, original:, **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 = original

  ["asset", id, basename].compact.join("/")
end