Created
April 22, 2016 06:34
-
-
Save jasper502/b3daff11631f62607fb758fbebeb2bec to your computer and use it in GitHub Desktop.
conditional_carrierwave
This file contains hidden or 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
# encoding: utf-8 | |
class DocumentUploader < CarrierWave::Uploader::Base | |
# http://stackoverflow.com/questions/29624223/papertrail-doesnt-play-nice-with-carrierwave-and-remove-previously-stored-file | |
#configure do |config| | |
# config.remove_previously_stored_files_after_update = false | |
#end | |
include CarrierWave::MiniMagick | |
include CarrierWave::MimeTypes | |
# 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 | |
#"#{model.class.to_s.underscore}/#{model.id}/" | |
"#{model.uploadable_type.downcase.pluralize.underscore}/#{model.parent_asset.id}/uploads/#{model.id}/" | |
end | |
def filename | |
"#{original_filename}" if original_filename.present? | |
end | |
# Create different versions of your uploaded files: | |
version :version do | |
def store_dir | |
"#{model.uploadable_type.downcase.pluralize.underscore}/#{model.parent_asset.id}/uploads/#{model.id}/versions/#{model.versions.last.id}/" | |
end | |
def full_filename (for_file = model.file.file) | |
"#{original_filename}" if original_filename.present? | |
end | |
end | |
version :thumb, :if => :image? do | |
process :resize_to_limit => [20, 20] | |
process :convert => 'jpg' | |
def full_filename (for_file = model.file.file) | |
"preview_thumb.jpg" | |
end | |
end | |
version :small, :if => :image? do | |
process :resize_to_limit => [200, 100] | |
process :convert => 'jpg' | |
def full_filename (for_file = model.file.file) | |
"preview_small.jpg" | |
end | |
end | |
version :large, :if => :image? do | |
process :resize_to_limit => [500, 700] | |
process :convert => 'jpg' | |
def full_filename (for_file = model.file.file) | |
"preview_large.jpg" | |
end | |
end | |
protected | |
def image?(new_file) | |
new_file.content_type.start_with? 'image' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment