Permalink
Browse files

Use after_commit if it is available to support reactive workers.

  • Loading branch information...
1 parent d48a28e commit 1a309ca7e3a9721dbe8fccfa117474e4d783aa3a @saulius saulius committed Oct 20, 2012
Showing with 13 additions and 4 deletions.
  1. +2 −2 lib/backgrounder/orm/activemodel.rb
  2. +10 −2 lib/backgrounder/orm/base.rb
  3. +1 −0 spec/backgrounder/orm/activemodel_spec.rb
View
4 lib/backgrounder/orm/activemodel.rb
@@ -14,7 +14,7 @@ def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
include mod
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
def trigger_#{column}_background_processing?
- super && (#{column}_changed? || remote_#{column}_url.present?)
+ super && (#{column}_changed? || previous_changes.has_key?(:#{column}) || remote_#{column}_url.present?)
end
RUBY
end
@@ -26,7 +26,7 @@ def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
include mod
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
def trigger_#{column}_background_storage?
- super && (#{column}_changed? || remote_#{column}_url.present?)
+ super && (#{column}_changed? || previous_changes.has_key?(:#{column}) || remote_#{column}_url.present?)
end
RUBY
end
View
12 lib/backgrounder/orm/base.rb
@@ -40,7 +40,11 @@ module Base
#
def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
send :before_save, :"set_#{column}_processing", :if => :"trigger_#{column}_background_processing?"
- send :after_commit, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_processing?"
+ if self.respond_to?(:after_commit)
+ send :after_commit, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_processing?"
+ else
+ send :after_save, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_processing?"
+ end
send :attr_accessor, :"process_#{column}_upload"
mod = Module.new
@@ -86,7 +90,11 @@ def trigger_#{column}_background_processing?
# end
#
def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
- send :after_commit, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_storage?"
+ if self.respond_to?(:after_commit)
+ send :after_commit, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_storage?"
+ else
+ send :after_save, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_storage?"
+ end
send :attr_accessor, :"process_#{column}_upload"
mod = Module.new
View
1 spec/backgrounder/orm/activemodel_spec.rb
@@ -9,6 +9,7 @@ def self.before_save(method, opts); nil; end
def self.after_commit(method, opts); nil; end
def avatar_changed?; nil; end
def remote_avatar_url; OpenStruct.new(:present? => true); end
+ def previous_changes; {}; end
end
@mock_class.extend CarrierWave::Backgrounder::ORM::ActiveModel

0 comments on commit 1a309ca

Please sign in to comment.