Shrine 2.5.0
New features
- The
add_metadataplugin has been extended to enable extracting and saving multiple metadata values at once. This is useful when you have tools which extract various metadata in a single command.
plugin :add_metadata
add_metadata do |io, context|
movie = FFMPEG::Movie.new(io.path)
{ "duration" => movie.duration,
"bitrate" => movie.bitrate,
"resolution" => movie.resolution,
"frame_rate" => movie.frame_rate }
end
# You can also optionally add metadata reader methods to the UploadedFile
metadata_method :duration, :bitrate, :resolution, :frame_rate- The
UploadedFile#dimensionsmethod will now be added when loading thestore_dimensionsplugin, which returns a two-element array of width and height.
plugin :store_dimensionsuploaded_file = uploader.upload(image)
uploaded_file.width #=> 500
uploaded_file.height #=> 400
uploaded_file.dimensions #=> [500, 400] (new)- The
:fallback_original_optionhas been added toversionsplugin, allowing you to disable automatic fallback to the cached file when using backgrounding, so that you can use a default URL instead.
plugin :versions, fallback_to_original: false- The
rack_fileplugin now enables the uploaders themselves to accept Rack uploaded file hashes for upload, in addition to attachers.
plugin :rack_fileparams[:file] #=> {:tempfile=>#<Tempfile>, :name=>"file", ...}
uploaded_file = uploader.upload(params[:file])
uploaded_file.original_filename #=> "nature.jpg"
uploaded_file.mime_type #=> "image/jpeg"Other improvements
- The validation helper methods now also return whether the validation suceeded, which makes it much easier to do conditional validation.
plugin :validation_helpers
Attacher.validate do
if validate_extension_inclusion %w[jpg jpeg png gif]
validate_mime_type_inclusion %w[image/jpeg image/png image/gif]
end
end- A
Attacher.default_urlmethod has been added todefault_urlplugin, as a new idiomatic way of declaring the default URL. This API better communicates that the default URL functionality is tied only to the attacher. This block is now also evaluated in the context of aShrine::Attacherinstance.
plugin :default_url
Attacher.default_url do |options|
self #=> #<Shrine::Attacher>
name #=> :image
record #=> #<Photo>
"/images/missing/#{name}.jpg"
endThe
validation_helpersplugin will now match the file extension in a case insensitive way, so if you dovalidate_extension_inclusion ["jpg"], that will now also pass for a file namednature.JPG.The
backgroundingplugin now also works for plain models, without an ORM. Specifically, it doesn't depend onAttacher.find_recordbeing defined anymore.The presign endpoint in the
direct_uploadplugin now allows theextensionquery parameter to be passed without a dot.If the data hash used for instantiating a
Shrine::UploadedFileis not valid, an explanatoryShrine::Errorwill now be raised. Previously Shrine would raise aKeyErrorfor a key that was missing.Shrine now detects whether two versions point to the same IO object, and raises a
Shrine::Error. Previously the upload would fail with anIOErrorwhen the same IO object would attempt to be uploaded the second time, which wasn't very descriptive.When
remove_invalidplugin removes an invalid cached file, it now also removes the dirty state from the attacher.
Backwards compatibility
- Passing a block when loading the
default_urlplugin is now deprecated over the newAttacher.default_url.
# deprecated
plugin :default_url do |context|
"/images/missing/#{context[:name]}.jpg"
endDocumentation
- Added the "Multiple Files" guide describing how to set up multiple file uploads using nested association attributes.