app/uploaders/avatar_uploader.rb
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
SMALL_WIDTH = 64
SMALL_HEIGHT = 64
NORMAL_WIDTH = 160
NORMAL_HEIGHT = 160
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_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
email_address = model.email.downcase
hash = Digest::MD5.hexdigest(email_address)
unless version_name.nil?
size = AvatarUploader.const_get("#{version_name.upcase}_WIDTH")
size_param = "?s=#{size}"
end
"http://www.gravatar.com/avatar/#{hash}#{size_param}"
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :small do
process resize_to_fill: [SMALL_WIDTH, SMALL_HEIGHT]
end
version :normal do
process resize_to_fill: [NORMAL_WIDTH, NORMAL_HEIGHT]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end