Module: CarrierWave::Backgrounder::ORM::DataMapper

Includes:
Base
Defined in:
lib/backgrounder/orm/data_mapper.rb

Instance Method Summary collapse

Instance Method Details

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/backgrounder/orm/data_mapper.rb', line 8

def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
  before :save, :"set_#{column}_processing"
  after  :save, :"enqueue_#{column}_background_job"

  class_eval  <<-RUBY, __FILE__, __LINE__ + 1
    attr_accessor :process_#{column}_upload
    attr_reader :#{column}_changed

    def set_#{column}_processing
      @#{column}_changed = attribute_dirty?(:#{column})
      self.#{column}_processing = true if respond_to?(:#{column}_processing) 
    end

    def enqueue_#{column}_background_job
      if trigger_#{column}_background_processing?
        ::Delayed::Job.enqueue #{worker}.new(self.class, id, #{column}.mounted_as) 
        @#{column}_changed = false
      end
    end

    def trigger_#{column}_background_processing?
      process_#{column}_upload != true && #{column}_changed
    end

  RUBY
end

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



35
36
37
38
39
40
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/data_mapper.rb', line 35

def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
  before :save, :"set_#{column}_changed"
  after :save, :"enqueue_#{column}_background_job"

  class_eval  <<-RUBY, __FILE__, __LINE__ + 1
    attr_accessor :process_#{column}_upload
    attr_reader :#{column}_changed

    def set_#{column}_changed
      @#{column}_changed = attribute_dirty?(:#{column})
    end

    def write_#{column}_identifier
      super() and return if process_#{column}_upload
      self.#{column}_tmp = _mounter(:#{column}).cache_name
    end

    def store_#{column}!
      super() if process_#{column}_upload
    end

    def enqueue_#{column}_background_job
      if trigger_#{column}_background_storage?
        ::Delayed::Job.enqueue #{worker}.new(self.class, id, #{column}.mounted_as) 
        @#{column}_changed = false
      end
    end

    def trigger_#{column}_background_storage?
      process_#{column}_upload != true && #{column}_changed
    end

  RUBY
end