Last Update: 2023-07-30 18:26:06 +0200

title: Shrine 3.2.0

New features

  • The type_predicates plugin has been added, which adds convenient predicate methods to Shrine::UploadedFile based on the MIME type.

rb # Gemfile gem "mini_mime" # default dependency of type_predicates rb Shrine.plugin :type_predicates

The plugin adds four predicate methods based on the general type of the file:

rb file.image? # returns true for any "image/*" MIME type file.video? # returns true for any "video/*" MIME type file.audio? # returns true for any "audio/*" MIME type file.text? # returns true for any "text/*" MIME type

You can also check for specific MIME type using the extension name:

rb file.type?(:jpg) # returns true if MIME type is "image/jpeg" file.type?(:svg) # returns true if MIME type is "image/svg+xml" file.type?(:mov) # returns true if MIME type is "video/quicktime" file.type?(:ppt) # returns true if MIME type is "application/vnd.ms-powerpoint" ...

For convenience, you can create predicate methods for specific file types:

rb Shrine.plugin :type_predicates, methods: %i[jpg svg mov ppt] rb file.jpg? # returns true if MIME type is "image/jpeg" file.svg? # returns true if MIME type is "image/svg+xml" file.mov? # returns true if MIME type is "video/quicktime" file.ppt? # returns true if MIME type is "application/vnd.ms-powerpoint"

  • The #add_metadata method has been added to the add_metadata plugin for adding new metadata to an existing file/attachment.

rb attacher.file.metadata #=> { ... } attacher.add_metadata("foo" => "bar") attacher.file.metadata #=> { ..., "foo" => "bar" }

Other improvements

  • The remove_invalid plugin now works correctly with derivatives plugin.

  • The remove_invalid plugin is now also activated when Attacher#validate is called manually.

  • The current attached file data can now be assigned back to the attachment attribute, and this operation will be a no-op.

rb photo.image #=> #<Shrine::UploadedFile id="foo" storage=:store metadata={...}> photo.image = { "id" => "foo", "storage" => "store", "metadata" => { ... } } # no-op

This allows treating the attachment attribute as a persistent attribute, where the current value can be assigned back on record updates.

  • When promoting derivatives, the :derivative parameter value was being passed to the uploader as an array. This has been fixed, and the value is now the same as when uploading derivatives directly to permanent storage.

  • The derivatives plugin now includes additional :io and :attacher values in the instrumentation event payload.

Backwards compatibility

  • The validation plugin now runs validations on Attacher#attach and Attacher#attach_cached. If you were using Attacher#change directly and expecting the validations to be run automatically, you will need to update your code.

  • If you were updating the cached file metadata via file data assignment, this will no longer work.

rb photo.image #=> #<Shrine::UploadedFile id="foo" storage=:cache metadata={...}> photo.image = { "id" => "foo", "storage" => "cache", "metadata" => { ... } } # no-op