I've been looking into CarrierWave to replace our current approach to
file attachments, and found it good. However, I couldn't find a way to
store more information about the attachment than just the filename.
Let me explain a bit further.
Right now we use Paperclip and extended it to cache in the database
the image dimensions (w x h) so when rendered by Rails image_tag, the
HTML generated contains the size information and avoids displacement
of elements while images loads.
We also compute the size for the different versions, but using
ImageScience geometry computation to do it real-time.
Pardon my ignorance, but couldn't find an example or in the code how
to achieve this.
This has been implemented? and if not, from 1 to 10, how complicated
will be to extend CarrierWave to do it outside the official gem?
Asking this because the changes in Paperclip require monkeypatching...
Thank you,
--
Luis Lavena
Jonas
The tricky part was getting it to work across form submission errors,
too. Specifically, before :store callbacks seemed to get called
several times with different params and values for @file during the
course of saving the object.
Here's the code that worked for me in the end. It can probably be
improved upon.
Here's my entire uploader class, but the important parts are the
capture_size methods.
Enjoy!
//Lars
--
class HeaderUploader < CarrierWave::Uploader::Base
storage :right_s3
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def url
["http://#{s3_bucket}.s3.amazonaws.com/", path].compact.join
end
before :cache, :capture_size_before_cache
before :retrieve_from_cache, :capture_size_after_retrieve_from_cache
def capture_size_before_cache(new_file)
model.header_width, model.header_height = `identify -format "%wx
%h" #{new_file.path}`.split(/x/)
end
def capture_size_after_retrieve_from_cache(cache_name)
model.header_width, model.header_height = `identify -format "%wx
%h" #{@file.path}`.split(/x/)
end
def dimensions
"#{model.header_width}x#{model.header_height}"
end
end