Created
February 26, 2011 22:47
-
-
Save SteveDeWald/845706 to your computer and use it in GitHub Desktop.
Better way to handle background processing w/ uploads to s3
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I use this in my uploader?