Skip to content

Commit

Permalink
Update blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
wvengen committed Nov 27, 2023
1 parent e33139c commit bff2994
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions _posts/2023-11-14-active-storage-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,39 @@ class DirectUploadController {

And so we have server-generated thumbnails, including for movies and PDFs
(which the browser can't do).


## Later addition: using named variants

Since Rails 7.0, Active Storage allows using named variants. This has the
benefit of a single place where to configure the specific settings for an image
variant, and allows generating these variants in a worker (offloading the web
process).

This works great, except it breaks when we try to use the named variant in
the `DirectUploadsController` above. It seems like named variants work only
work after the file has been uploaded and/or variant has been generated. To
still have the convenience of named variants, we can just get the processing
parameters directly:


```ruby
# app/controllers/direct_uploads_controller.rb
class DirectUploadsController < ActiveStorage::DirectUploadsController

private

# add thumb_url to response
def direct_upload_json(blob)
json = super(blob)
variant_tf = MyModel.attachment_reflections["files"].named_variants[:thumb].transformations
json[:thumb_url] = url_for(blob.representation(variant_tf))
json
end
end
```

This code block contains a direct reference to the model (`MyModel`) and the
attribute (`files`), so it is not a generic solution. This can probably be
improved, e.g. by getting the attachment from the blob and, which contains the
model name and attribute. But for us, it works well as it is.

0 comments on commit bff2994

Please sign in to comment.