Shrine 3.x: custom location with derivatives plugin

Hello,

When migrating to 3.x, how to use the derivatives plugin to build customized paths for images ?
In Shrine 2.x, I was using:

def generate_location(io, context)
# build final path
if context[:record].respond_to?(:id) and context[:version]
# id path
id_path = ("%09d".freeze % context[:record].id).scan(/\d{3}/).join("/".freeze)
# return full path
return [type, id_path, “file”, context[:version], name].compact.join("/")
# build temp path
else
return [type, “file”, name].compact.join("/")
end
end

However, context[:version] is empty due to the new derivatives plugin. What can replace it ?

Thanks,

With derivatives plugin the :derivative option is now passed to #generate_location (and you can also use kwargs now):

def generate_location(io, record: nil, name: nil, derivative: nil, **)
  derivative #=> :small, :medium, :large, ...
  record #=> #<Photo>, #<User>, ...
  name #=> :image, :avatar, ...
  # ...
end

Well, so, this is what I did. It works well.

def generate_location(io, record:nil, name:nil, derivative:nil, metadata:nil, **)
		
    # generate custom filename and store it in metadata["filename"]
    generate_filename(record, metadata)
		
    # get class name
    type = record.class.name.downcase
		
    # build final path
    if record.id
      # id path
      id_path = ("%09d".freeze % record.id).scan(/\d{3}/).join("/".freeze)   
      # return full path   
      return [type, id_path, name, (derivative ? derivative : "original"), metadata["filename"]].compact.join("/")
    # build temp path
    else
      return [type, name, metadata["filename"]].compact.join("/")
    end
end

The only tip I used is storing my new custom file name in metadata[‘filename’] before using it in location. Not sure it’s the best way but it works.