Module: CarrierWave::Backgrounder::ORM::Base

Included in:
ActiveModel, DataMapper
Defined in:
lib/backgrounder/orm/base.rb

Overview

Base class for all things orm

Instance Method Summary collapse

Instance Method Details

#process_in_background(column, worker = ::CarrierWave::Workers::ProcessAsset) ⇒ Object

User#process_in_background will process and create versions in a background process.

class User < ActiveRecord::Base

mount_uploader :avatar, AvatarUploader
process_in_background :avatar

end

The above adds a User#process_upload method which can be used at times when you want to bypass background storage and processing.

@user.process_avatar = true
@user.save

You can also pass in your own workers using the second argument in case you need other things done during processing.

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
  process_in_background :avatar, CustomWorker
end

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
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/backgrounder/orm/base.rb', line 41

def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
  send :before_save, :"set_#{column}_processing", :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
  include mod

  mod.module_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
end

#store_in_background(column, worker = ::CarrierWave::Workers::StoreAsset) ⇒ Object

#store_in_background will process, version and store uploads in a background process.

class User < ActiveRecord::Base

mount_uploader :avatar, AvatarUploader
store_in_background :avatar

end

The above adds a User#process_<column>_upload method which can be used at times when you want to bypass background storage and processing.

@user.process_avatar_upload = true
@user.save

You can also pass in your own workers using the second argument in case you need other things done during processing.

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
  store_in_background :avatar, CustomWorker
end


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/backgrounder/orm/base.rb', line 92

def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
  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
  include mod
  mod.module_eval  <<-RUBY, __FILE__, __LINE__ + 1

    def write_#{column}_identifier
      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
    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_storage?
      process_#{column}_upload != true
    end

  RUBY
end