lib/backgrounder/orm/base.rb in carrierwave_backgrounder-0.4.3 vs lib/backgrounder/orm/base.rb in carrierwave_backgrounder-1.0.0.beta
- old
+ new
@@ -33,20 +33,21 @@
#
# In addition you can also add a column to the database appended by _processing with a type of boolean
# which can be used to check if processing is complete.
#
# def self.up
- # add_column :users, :avatar_processing, :boolean
+ # add_column :users, :avatar_processing, :boolean, null: false, default: false
# end
#
- def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
+ def process_in_background(column, worker_klass=nil)
attr_accessor :"process_#{column}_upload"
+ worker = worker_klass || "#{CarrierWave::Backgrounder.worker_klass}::ProcessAsset"
mod = Module.new
include mod
- _define_shared_backgrounder_methods(mod, column, worker)
+ _define_shared_backgrounder_methods(mod, column, worker )
end
##
# #store_in_background will process, version and store uploads in a background process.
#
@@ -67,23 +68,20 @@
# class User < ActiveRecord::Base
# mount_uploader :avatar, AvatarUploader
# store_in_background :avatar, CustomWorker
# end
#
- def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
+ def store_in_background(column, worker_klass=nil)
attr_accessor :"process_#{column}_upload"
+ worker = worker_klass || "#{CarrierWave::Backgrounder.worker_klass}::StoreAsset"
+
mod = Module.new
include mod
mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
- def remove_#{column}=(value)
- super
- self.process_#{column}_upload = true
- end
-
def write_#{column}_identifier
- super and return if process_#{column}_upload
+ super and return if process_#{column}_upload || remove_#{column}
self.#{column}_tmp = #{column}_cache if #{column}_cache
end
def store_#{column}!
super if process_#{column}_upload
@@ -103,14 +101,22 @@
def set_#{column}_processing
self.#{column}_processing = true if respond_to?(:#{column}_processing)
end
def enqueue_#{column}_background_job?
- !remove_#{column}? && !process_#{column}_upload && #{column}_updated?
+ !remove_#{column}? && !process_#{column}_upload && #{column}_present? && #{column}_updated?
end
+ def #{column}_mounted_as
+ #{column}.is_a?(Array) ? #{column}.first.mounted_as : #{column}.mounted_as
+ end
+
+ def #{column}_present?
+ #{column}.is_a?(Array) ? #{column}.present? : #{column}.file.present?
+ end
+
def enqueue_#{column}_background_job
- CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}.mounted_as)
+ CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}_mounted_as)
end
RUBY
end
end # Base