How to generate derivatives URL with download_endpoint plugin

Hello, I can’t figure out how to generate derivative URLs with the derivative plugin, but via the download_endpoint plugin.

In normal circumstances, one would do:

MyModel.find(1).image.image_url ==
  MyModel.find(1).image.image.url

However with the derivative param on must use

MyModel.find(1).image.image_url(:thumb) ==
  MyModel.find(1).image.image.storage.url(:thumb)

And with derivative + download_endpoint plugins:

MyModel.find(1).image.download_url ==
  Shrine::Plugins::DownloadEndpoint::FileUrl.new(MyModel.find(1).image).call

MyModel.find(1).image.download_url(:thumb) # not ok, no such param
  Shrine::Plugins::DownloadEndpoint::FileUrl.new(MyModel.find(1).image).call(no_such_param)

Is it possible to generate such a URL ?

The #download_url method is defined on a Shrine::UploadedFile object. You can retrieve a Shrine::UploadedFile object representing a derivative by passing the name to the attachment method:

photo.image(:thumb) #=> #<Shrine::UploadedFile>

Then you call #download_url on that object:

photo.image(:thumb).download_url

Calling photo.image_url(:thumb) is just a shorthand for photo.image(:thumb).url.

Thanks @janko, it works great :wink:

Have a great day!

1 Like