Shrine 3.2.0
New features
The
type_predicatesplugin has been added, which adds convenient predicate methods toShrine::UploadedFilebased on the MIME type.# Gemfile gem "mini_mime" # default dependency of type_predicatesShrine.plugin :type_predicatesThe plugin adds four predicate methods based on the general type of the file:
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 typeYou can also check for specific MIME type using the extension name:
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:
Shrine.plugin :type_predicates, methods: %i[jpg svg mov ppt]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_metadatamethod has been added to theadd_metadataplugin for adding new metadata to an existing file/attachment.attacher.file.metadata #=> { ... } attacher.add_metadata("foo" => "bar") attacher.file.metadata #=> { ..., "foo" => "bar" }
Other improvements
The
remove_invalidplugin now works correctly withderivativesplugin.The
remove_invalidplugin is now also activated whenAttacher#validateis called manually.The current attached file data can now be assigned back to the attachment attribute, and this operation will be a no-op.
photo.image #=> #<Shrine::UploadedFile id="foo" storage=:store metadata={...}> photo.image = { "id" => "foo", "storage" => "store", "metadata" => { ... } } # no-opThis allows treating the attachment attribute as a persistent attribute, where the current value can be assigned back on record updates.
When promoting derivatives, the
:derivativeparameter 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
derivativesplugin now includes additional:ioand:attachervalues in the instrumentation event payload.
Backwards compatibility
The
validationplugin now runs validations onAttacher#attachandAttacher#attach_cached. If you were usingAttacher#changedirectly 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.
photo.image #=> #<Shrine::UploadedFile id="foo" storage=:cache metadata={...}> photo.image = { "id" => "foo", "storage" => "cache", "metadata" => { ... } } # no-op