You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixes for Carrierwave and Mongoid nested documents
Usage
Fork off and clone!
Find and replace all MyApp occurences with YourAppName.
Also have a looksy in config/mongoid.yml and change the database names appropriately.
Enjoy :)
Carrierwave fix for embedded documents
I fixed some issues with Carrierwave and embedded documents, mostly with code from the community.
See below for an example uploader.
Embeds one example
With the fixes included in this template, you can easily use embedded uploaders, like so:
(in models/profile.rb)
classProfileincludeMongoid::DocumentincludeMongoid::Timestampsfield:name,:type=>Stringfield:about,:type=>Stringfield:user_id,:type=>Stringembeds_one:photoaccepts_nested_attributes_for:photo,:allow_destroy=>true,:reject_if=>:all_blank# Mount the embedded uploadermount_embedded_uploader:photo,:filebelongs_to:userend
Embeds many example
If you have a model that embeds a lot of photos, however, the above will not work. You will need something like this:
(in models/post.rb)
defPostincludeMongoid::DocumentincludeMongoid::Timestampsfield:titlefield:bodyembeds_many:photosaccepts_nested_attributes_for:photos,:allow_destroy=>true,:reject_if=>proc{ |attributes| attributes['file'].blank?}after_save:save_photos# forward validation to embedded documentsdefvalid?(*)_run_validation_callbacks{super}validate_photosend# Validate photos callbackdefvalidate_photosphotos.eachdo |photo|
photo.send(:_run_validation_callbacks)endend# Save photos callbackdefsave_photosphotos.eachdo |photo|
ifphoto.new_record?andphoto.file.present?photo.save!endendendend
classPhotoUploader < CarrierWave::Uploader::Base# Include RMagick or ImageScience support:# include CarrierWave::RMagickincludeCarrierWave::MiniMagick# include CarrierWave::ImageScience# Choose what kind of storage to use for this uploader:# storage :filestorage:fog# Override the directory where uploaded files will be stored.# This is a sensible default for uploaders that are meant to be mounted:defstore_dir"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"end# Provide a default URL as a default if there hasn't been a file uploaded:# def default_url# "/images/fallback/" + [version_name, "default.png"].compact.join('_')# end# Process files as they are uploaded:process:resize_to_limit=>[640,640]process:convert=>:png## def scale(width, height)# # do something# end# Create different versions of your uploaded files:version:thumbdoprocess:convert=>:pngprocess:resize_and_pad=>[40,40,:white]endversion:smalldoprocess:convert=>:pngprocess:resize_and_pad=>[100,100,:white]endversion:mediumdoprocess:convert=>:pngprocess:resize_and_pad=>[460,460,:white]endversion:largedoprocess:convert=>:pngprocess:resize_and_pad=>[640,640,:white]endversion:hugedoprocess:convert=>:pngprocess:resize_to_limit=>[960,960]end# Add a white list of extensions which are allowed to be uploaded.# For images you might use something like this:defextension_white_list%w(jpgjpeggifpng)enddeffilenameif not super.nil?super.chomp(File.extname(super)) + '.png'endendend
About
Rails template with Mongoid, Devise, Cancan, Carrierwave, Rspec, Cucumber, Oauth with Facebook and Google login.