With Carrierwave and Rails 3, is it possible to beautifully manage image and image files without the same bootloader?

In my Rails application, I want to allow users to upload image or image files without using Carrierwave. Currently, Carrierwave works great with processing and processing image files, but unfortunately it completely discards files without images. Is there a clean way for a single Carrierwave loader to process both image files and non-image files?

I will include my current bootloader below:

class AssetUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick storage :file def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end version :thumb do process :resize_to_fill => [300, 300] end version :icon do process :resize_to_fill => [48, 48] end def extension_white_list %w(jpg jpeg gif png pdf doc xls docx xlsx ppt) end end 
+7
source share
2 answers

I had this exact problem. I solved it with the hoary comp sci solution for a single-level direction: the trampoline / thunk method, which dynamically decides whether to process based on the file extension.

You can find the implementation here: https://gist.github.com/995663

(the naive approach of introducing logic into the version block does not actually work due to how DSL CarrierWave works - the logic should be delayed until the call)

I hope this helps.

+2
source

I know this was answered, but Aaronโ€™s answer does not completely solve the problem. You can use what he suggested at https://gist.github.com/995663 if you only need to call process , but not if you want to selectively process using version .

For the version, see the carrier wiki page: https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing

Now you can do this in your code, and it will only process the version block in images

 version :thumb, :if => :image? do process ... end protected def image?(new_file) new_file.content_type.include? 'image' end 
+1
source

All Articles