lib/backgrounder/orm/base.rb in carrierwave_backgrounder-0.1.2 vs lib/backgrounder/orm/base.rb in carrierwave_backgrounder-0.1.3
- old
+ new
@@ -37,31 +37,21 @@
# def self.up
# add_column :users, :avatar_processing, :boolean
# end
#
def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
- send :attr_accessor, :"process_#{column}_upload"
+ attr_accessor :"process_#{column}_upload"
- send :before_save, :"set_#{column}_processing", :if => :"trigger_#{column}_background_processing?"
- callback = self.respond_to?(:after_commit) ? :after_commit : :after_save
- send callback, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_processing?"
-
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
-
+ mod = Module.new
+ include mod
+ mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def set_#{column}_processing
self.#{column}_processing = true if respond_to?(:#{column}_processing)
end
-
- def enqueue_#{column}_background_job
- CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}.mounted_as)
- end
-
- def trigger_#{column}_background_processing?
- process_#{column}_upload != true
- end
-
RUBY
+
+ _define_shared_backgrounder_methods(mod, column, worker)
end
##
# #store_in_background will process, version and store uploads in a background process.
#
@@ -83,33 +73,40 @@
# mount_uploader :avatar, AvatarUploader
# store_in_background :avatar, CustomWorker
# end
#
def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
- send :attr_accessor, :"process_#{column}_upload"
+ attr_accessor :"process_#{column}_upload"
- callback = self.respond_to?(:after_commit) ? :after_commit : :after_save
- send callback, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_storage?"
-
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
-
+ mod = Module.new
+ include mod
+ mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def write_#{column}_identifier
- super() and return if process_#{column}_upload
+ super and return if process_#{column}_upload
self.#{column}_tmp = _mounter(:#{column}).cache_name if _mounter(:#{column}).cache_name
end
def store_#{column}!
- super() if process_#{column}_upload
+ super if process_#{column}_upload
end
+ RUBY
- def enqueue_#{column}_background_job
- CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}.mounted_as)
- end
+ _define_shared_backgrounder_methods(mod, column, worker)
+ end
- def trigger_#{column}_background_storage?
- process_#{column}_upload != true
+ private
+
+ def _define_shared_backgrounder_methods(mod, column, worker)
+ mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{column}_updated?; true; end
+
+ def enqueue_#{column}_background_job?
+ !process_#{column}_upload && #{column}_updated?
end
+ def enqueue_#{column}_background_job
+ CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}.mounted_as)
+ end
RUBY
end
end # Base