Created
July 29, 2014 06:25
-
-
Save anonymous/40b118d120eae265648c to your computer and use it in GitHub Desktop.
Carrierwave example file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# User Avatar Uploader | |
class UserAvatarUploader < CarrierWave::Uploader::Base | |
include CarrierWave::MiniMagick | |
storage :file | |
def move_to_cache | |
true | |
end | |
def move_to_store | |
true | |
end | |
def cache_dir | |
if Rails.env.test? || Rails.env.cucumber? | |
"#{Rails.root}/spec/support/uploads/tmp" | |
else | |
"#{Rails.root}/public/uploads/tmp" | |
end | |
end | |
def store_dir | |
dir = '/public/uploads/' | |
dir = '/spec/support/uploads/' if Rails.env.test? || Rails.env.cucumber? | |
"#{Rails.root}#{dir}#{model.class.to_s.underscore}/#{model.id}" | |
end | |
def filename | |
fname = "#{model.class.to_s.underscore}" | |
tstamp = "#{Time.now.strftime('%Y%m%d%H%M%S')}" | |
orig_fname = original_filename | |
"#{fname}#{tstamp}#{model.avatar.file.extension}" if orig_fname | |
end | |
version :large do | |
process resize_to_fill: [600, 600] | |
end | |
version :medium do | |
process resize_to_fill: [300, 300] | |
end | |
version :small do | |
process resize_to_fill: [150, 150] | |
end | |
version :thumb do | |
process resize_to_fill: [64, 64] | |
end | |
# whitelisting | |
def extension_white_list | |
%w(jpg jpeg gif png) | |
end | |
# set a default image | |
def default_url | |
image_name = [version_name, 'default-user.png'].compact.join('_') | |
ActionController::Base.helpers.asset_path "fallback/#{image_name}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment