Skip to content

Instantly share code, notes, and snippets.

@rogercampos
Created January 10, 2013 08:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogercampos/4500405 to your computer and use it in GitHub Desktop.
Save rogercampos/4500405 to your computer and use it in GitHub Desktop.
carrierwave delayed version processing

All the versions declared as 'delayed' won't be processed synchronously. Instead they will be processed when you call ClientFile#process_delayed_versions!. It's up to you to decide when and from where to call this.

module Carrierwave
module DelayedVersions
extend ActiveSupport::Concern
module ClassMethods
def delayed_versions
@delayed_versions ||= Set.new
end
def delayed_version(name, opts = {}, &block)
delayed_versions.add(name)
version(name, opts.merge(:if => :processing_async?), &block)
end
end
def processing_async?(img = nil)
!!model.processing_async
end
module ModelMethods
extend ActiveSupport::Concern
included { attr_accessor :processing_async }
def with_async_processing
self.processing_async = true
yield
ensure
self.processing_async = false
end
def process_delayed_versions!
self.class.uploaders.each do |mounted_as, uploader|
next unless uploader.included_modules.include?(Carrierwave::DelayedVersions) &&
!uploader.delayed_versions.empty?
with_async_processing do
send(mounted_as).send(:"recreate_versions!", *uploader.delayed_versions.to_a)
end
end
end
end
end
en# #
class ClientFile < ActiveRecord::Base
include Carrierwave::DelayedVersions::ModelMethods
mount_uploader :preview, UploaderExample
end
class ClientFilePreviewUploader < BaseUploader
@fog_public = true
include Carrierwave::DelayedVersions
delayed_version :large do
process :resize_to_fit => [768, 768]
process :pad_to_exact_dimensions => [768, 768]
process :apply_pngquant
end
delayed_version :medium do
process :resize_to_fit => [384, 384]
process :pad_to_exact_dimensions => [384, 384]
process :apply_pngquant
end
delayed_version :small do
process :resize_to_fit => [192, 192]
process :pad_to_exact_dimensions => [192, 192]
process :apply_pngquant
end
version :thumb do
process :resize_to_fit => [100, 100]
process :pad_to_exact_dimensions => [100, 100]
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg png)
end
end
@kenzo2013
Copy link

Hello @rogercampos...thank you for this. But it can work with Mongoid?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment