Skip to content

Instantly share code, notes, and snippets.

@SteveDeWald
Created February 26, 2011 22:47
Show Gist options
  • Save SteveDeWald/845706 to your computer and use it in GitHub Desktop.
Save SteveDeWald/845706 to your computer and use it in GitHub Desktop.
Better way to handle background processing w/ uploads to s3
# lib/carrier_wave/delayed_job.rb
module CarrierWave
module DelayedJob
module ActiveRecordInterface
def delay_carrierwave
@delay_carrierwave ||= true
end
def delay_carrierwave=(delay)
@delay_carrierwave = delay
end
def perform
asset_name = self.class.uploader_options.keys.first
self.send(asset_name).recreate_versions!
end
private
def enqueue
::Delayed::Job.enqueue self
end
end
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def self.extended(base)
base.send(:include, InstanceMethods)
base.alias_method_chain :store_versions!, :delay
::ActiveRecord::Base.send(:include, CarrierWave::DelayedJob::ActiveRecordInterface)
end
module InstanceMethods
def store_versions_with_delay!(new_file)
store_versions_without_delay!(new_file) unless model.delay_carrierwave
end
end
end
end
end
@monteirobrena
Copy link

How can I use this in my uploader?

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