carrierwave_backgrounder 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/carrierwave_backgrounder.gemspec +1 -1
- data/lib/backgrounder/orm/{activerecord.rb → activemodel.rb} +10 -8
- data/lib/backgrounder/orm/base.rb +20 -7
- data/lib/backgrounder/orm/data_mapper.rb +2 -2
- data/lib/backgrounder/version.rb +1 -1
- data/lib/backgrounder/workers/process_asset.rb +6 -1
- data/lib/backgrounder/workers/store_asset.rb +8 -3
- data/lib/carrierwave_backgrounder.rb +6 -2
- data/spec/backgrounder/orm/activemodel_spec.rb +26 -0
- data/spec/backgrounder/orm/base_spec.rb +103 -0
- data/spec/support/backend_constants.rb +8 -0
- metadata +11 -8
- data/lib/backgrounder/orm/mongoid.rb +0 -33
@@ -21,6 +21,6 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.add_dependency "carrierwave", ["~> 0.5"]
|
22
22
|
|
23
23
|
s.add_development_dependency "rspec", ["2.5.0"]
|
24
|
-
s.add_development_dependency "mocha", ["~> 0.
|
24
|
+
s.add_development_dependency "mocha", ["~> 0.12.0"]
|
25
25
|
s.add_development_dependency "rake"
|
26
26
|
end
|
@@ -1,19 +1,20 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'active_record'
|
3
2
|
|
4
3
|
module CarrierWave
|
5
4
|
module Backgrounder
|
6
5
|
module ORM
|
7
6
|
|
8
|
-
module
|
7
|
+
module ActiveModel
|
9
8
|
include CarrierWave::Backgrounder::ORM::Base
|
10
9
|
|
11
10
|
def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
|
12
11
|
super
|
13
12
|
|
14
|
-
|
13
|
+
mod = Module.new
|
14
|
+
include mod
|
15
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
15
16
|
def trigger_#{column}_background_processing?
|
16
|
-
|
17
|
+
super && (#{column}_changed? || previous_changes.has_key?(:#{column}) || remote_#{column}_url.present?)
|
17
18
|
end
|
18
19
|
RUBY
|
19
20
|
end
|
@@ -21,16 +22,17 @@ module CarrierWave
|
|
21
22
|
def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
|
22
23
|
super
|
23
24
|
|
24
|
-
|
25
|
+
mod = Module.new
|
26
|
+
include mod
|
27
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
25
28
|
def trigger_#{column}_background_storage?
|
26
|
-
|
29
|
+
super && (#{column}_changed? || previous_changes.has_key?(:#{column}) || remote_#{column}_url.present?)
|
27
30
|
end
|
28
31
|
RUBY
|
29
32
|
end
|
30
|
-
end #
|
33
|
+
end # ActiveModel
|
31
34
|
|
32
35
|
end # ORM
|
33
36
|
end # Backgrounder
|
34
37
|
end # CarrierWave
|
35
38
|
|
36
|
-
ActiveRecord::Base.extend CarrierWave::Backgrounder::ORM::ActiveRecord
|
@@ -40,10 +40,17 @@ module CarrierWave
|
|
40
40
|
#
|
41
41
|
def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
|
42
42
|
send :before_save, :"set_#{column}_processing", :if => :"trigger_#{column}_background_processing?"
|
43
|
-
|
43
|
+
if self.respond_to?(:after_commit)
|
44
|
+
send :after_commit, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_processing?"
|
45
|
+
else
|
46
|
+
send :after_save, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_processing?"
|
47
|
+
end
|
48
|
+
send :attr_accessor, :"process_#{column}_upload"
|
44
49
|
|
45
|
-
|
46
|
-
|
50
|
+
mod = Module.new
|
51
|
+
include mod
|
52
|
+
|
53
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
47
54
|
|
48
55
|
def set_#{column}_processing
|
49
56
|
self.#{column}_processing = true if respond_to?(:#{column}_processing)
|
@@ -83,10 +90,16 @@ module CarrierWave
|
|
83
90
|
# end
|
84
91
|
#
|
85
92
|
def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
93
|
+
if self.respond_to?(:after_commit)
|
94
|
+
send :after_commit, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_storage?"
|
95
|
+
else
|
96
|
+
send :after_save, :"enqueue_#{column}_background_job", :if => :"trigger_#{column}_background_storage?"
|
97
|
+
end
|
98
|
+
send :attr_accessor, :"process_#{column}_upload"
|
99
|
+
|
100
|
+
mod = Module.new
|
101
|
+
include mod
|
102
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
90
103
|
|
91
104
|
def write_#{column}_identifier
|
92
105
|
super() and return if process_#{column}_upload
|
@@ -20,7 +20,7 @@ module CarrierWave
|
|
20
20
|
|
21
21
|
def enqueue_#{column}_background_job
|
22
22
|
if trigger_#{column}_background_processing?
|
23
|
-
::
|
23
|
+
CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id, #{column}.mounted_as)
|
24
24
|
@#{column}_changed = false
|
25
25
|
end
|
26
26
|
end
|
@@ -55,7 +55,7 @@ module CarrierWave
|
|
55
55
|
|
56
56
|
def enqueue_#{column}_background_job
|
57
57
|
if trigger_#{column}_background_storage?
|
58
|
-
::
|
58
|
+
CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id, #{column}.mounted_as)
|
59
59
|
@#{column}_changed = false
|
60
60
|
end
|
61
61
|
end
|
data/lib/backgrounder/version.rb
CHANGED
@@ -3,9 +3,14 @@ module CarrierWave
|
|
3
3
|
module Workers
|
4
4
|
|
5
5
|
class ProcessAsset < Struct.new(:klass, :id, :column)
|
6
|
-
include ::Sidekiq::Worker if defined?(::Sidekiq)
|
7
6
|
@queue = :process_asset
|
8
7
|
|
8
|
+
if defined?(::Sidekiq)
|
9
|
+
include ::Sidekiq::Worker
|
10
|
+
|
11
|
+
sidekiq_options :queue => @queue
|
12
|
+
end
|
13
|
+
|
9
14
|
def self.perform(*args)
|
10
15
|
new(*args).perform
|
11
16
|
end
|
@@ -3,9 +3,14 @@ module CarrierWave
|
|
3
3
|
module Workers
|
4
4
|
|
5
5
|
class StoreAsset < Struct.new(:klass, :id, :column)
|
6
|
-
include ::Sidekiq::Worker if defined?(::Sidekiq)
|
7
6
|
@queue = :store_asset
|
8
7
|
|
8
|
+
if defined?(::Sidekiq)
|
9
|
+
include ::Sidekiq::Worker
|
10
|
+
|
11
|
+
sidekiq_options :queue => @queue
|
12
|
+
end
|
13
|
+
|
9
14
|
def self.perform(*args)
|
10
15
|
new(*args).perform
|
11
16
|
end
|
@@ -27,12 +32,12 @@ module CarrierWave
|
|
27
32
|
end
|
28
33
|
end
|
29
34
|
end
|
30
|
-
|
35
|
+
|
31
36
|
def set_args(klass, id, column)
|
32
37
|
self.klass, self.id, self.column = klass, id, column
|
33
38
|
end
|
34
39
|
|
35
40
|
end # StoreAsset
|
36
|
-
|
41
|
+
|
37
42
|
end # Workers
|
38
43
|
end # Backgrounder
|
@@ -83,7 +83,8 @@ if defined?(Rails)
|
|
83
83
|
|
84
84
|
initializer "carrierwave_backgrounder.active_record" do
|
85
85
|
ActiveSupport.on_load :active_record do
|
86
|
-
require 'backgrounder/orm/
|
86
|
+
require 'backgrounder/orm/activemodel'
|
87
|
+
::ActiveRecord::Base.extend CarrierWave::Backgrounder::ORM::ActiveModel
|
87
88
|
end
|
88
89
|
end
|
89
90
|
|
@@ -92,7 +93,10 @@ if defined?(Rails)
|
|
92
93
|
end
|
93
94
|
|
94
95
|
initializer "carrierwave_backgrounder.mongoid" do
|
95
|
-
|
96
|
+
if defined?(Mongoid)
|
97
|
+
require 'backgrounder/orm/activemodel'
|
98
|
+
::Mongoid::Document::ClassMethods.send(:include, ::CarrierWave::Backgrounder::ORM::ActiveModel)
|
99
|
+
end
|
96
100
|
end
|
97
101
|
|
98
102
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'backgrounder/orm/activemodel'
|
4
|
+
|
5
|
+
describe CarrierWave::Backgrounder::ORM::ActiveModel do
|
6
|
+
before do
|
7
|
+
@mock_class = Class.new do
|
8
|
+
def self.before_save(method, opts); nil; end
|
9
|
+
def self.after_commit(method, opts); nil; end
|
10
|
+
def avatar_changed?; nil; end
|
11
|
+
def remote_avatar_url; OpenStruct.new(:present? => true); end
|
12
|
+
def previous_changes; {}; end
|
13
|
+
end
|
14
|
+
|
15
|
+
@mock_class.extend CarrierWave::Backgrounder::ORM::ActiveModel
|
16
|
+
@mock_class.process_in_background :avatar
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#trigger_column_background_processing?' do
|
20
|
+
it "calls up to processs column upload getter in the base class" do
|
21
|
+
instance = @mock_class.new
|
22
|
+
instance.expects(:process_avatar_upload)
|
23
|
+
instance.trigger_avatar_background_processing?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe CarrierWave::Backgrounder::ORM::Base do
|
5
|
+
before do
|
6
|
+
@mock_class = Class.new do
|
7
|
+
def self.before_save(method, opts); nil; end
|
8
|
+
def self.after_commit(method, opts); nil; end
|
9
|
+
end
|
10
|
+
|
11
|
+
@mock_class.extend CarrierWave::Backgrounder::ORM::Base
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'mixes in the two DSL methods' do
|
15
|
+
@mock_class.should respond_to(:process_in_background)
|
16
|
+
@mock_class.should respond_to(:store_in_background)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'process in background' do
|
20
|
+
|
21
|
+
describe 'setting up callbacks' do
|
22
|
+
|
23
|
+
it 'creates a before_save hook' do
|
24
|
+
@mock_class.expects(:before_save).with(:set_avatar_processing, :if => :trigger_avatar_background_processing?)
|
25
|
+
@mock_class.process_in_background :avatar
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'creates an after_save hook' do
|
29
|
+
@mock_class.expects(:after_commit).with(:enqueue_avatar_background_job, :if => :trigger_avatar_background_processing?)
|
30
|
+
@mock_class.process_in_background :avatar
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'including new methods' do
|
36
|
+
before do
|
37
|
+
@mock_class.process_in_background :avatar
|
38
|
+
@instance = @mock_class.new
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'creates a processing enabled accessor' do
|
42
|
+
@instance.should respond_to(:process_avatar_upload)
|
43
|
+
@instance.should respond_to(:process_avatar_upload=)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'create a setter for the processing attribute' do
|
47
|
+
@instance.should respond_to(:set_avatar_processing)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'create a background job queuer' do
|
51
|
+
@instance.should respond_to(:enqueue_avatar_background_job)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'create a trigger interrogator' do
|
55
|
+
@instance.should respond_to(:trigger_avatar_background_processing?)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'store in background' do
|
63
|
+
|
64
|
+
describe 'setting up callbacks' do
|
65
|
+
|
66
|
+
it 'creates an after_save hook' do
|
67
|
+
@mock_class.expects(:after_commit).with(:enqueue_avatar_background_job, :if => :trigger_avatar_background_storage?)
|
68
|
+
@mock_class.store_in_background :avatar
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'including new methods' do
|
74
|
+
before do
|
75
|
+
@mock_class.store_in_background :avatar
|
76
|
+
@instance = @mock_class.new
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'creates a processing enabled accessor' do
|
80
|
+
@instance.should respond_to(:process_avatar_upload)
|
81
|
+
@instance.should respond_to(:process_avatar_upload=)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'overrides the write column identifier method from carrierwave' do
|
85
|
+
@instance.should respond_to(:write_avatar_identifier)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'overrides the store column method from carrierwave' do
|
89
|
+
@instance.should respond_to(:store_avatar!)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'create a background job queuer' do
|
93
|
+
@instance.should respond_to(:enqueue_avatar_background_job)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'create a trigger interrogator' do
|
97
|
+
@instance.should respond_to(:trigger_avatar_background_storage?)
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave_backgrounder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: carrierwave
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ~>
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 0.12.0
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 0.12.0
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: rake
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -89,16 +89,17 @@ files:
|
|
89
89
|
- Rakefile
|
90
90
|
- carrierwave_backgrounder.gemspec
|
91
91
|
- lib/backgrounder/delay.rb
|
92
|
-
- lib/backgrounder/orm/
|
92
|
+
- lib/backgrounder/orm/activemodel.rb
|
93
93
|
- lib/backgrounder/orm/base.rb
|
94
94
|
- lib/backgrounder/orm/data_mapper.rb
|
95
|
-
- lib/backgrounder/orm/mongoid.rb
|
96
95
|
- lib/backgrounder/version.rb
|
97
96
|
- lib/backgrounder/workers.rb
|
98
97
|
- lib/backgrounder/workers/process_asset.rb
|
99
98
|
- lib/backgrounder/workers/store_asset.rb
|
100
99
|
- lib/carrierwave_backgrounder.rb
|
101
100
|
- spec/backgrounder/backends_spec.rb
|
101
|
+
- spec/backgrounder/orm/activemodel_spec.rb
|
102
|
+
- spec/backgrounder/orm/base_spec.rb
|
102
103
|
- spec/backgrounder/workers/fixtures/test.jpg
|
103
104
|
- spec/backgrounder/workers/process_asset_spec.rb
|
104
105
|
- spec/backgrounder/workers/store_asset_spec.rb
|
@@ -118,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
119
|
version: '0'
|
119
120
|
segments:
|
120
121
|
- 0
|
121
|
-
hash:
|
122
|
+
hash: 1188528619388271900
|
122
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
124
|
none: false
|
124
125
|
requirements:
|
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
128
|
version: '0'
|
128
129
|
segments:
|
129
130
|
- 0
|
130
|
-
hash:
|
131
|
+
hash: 1188528619388271900
|
131
132
|
requirements: []
|
132
133
|
rubyforge_project: carrierwave_backgrounder
|
133
134
|
rubygems_version: 1.8.24
|
@@ -137,6 +138,8 @@ summary: Offload CarrierWave's image processing and storage to a background proc
|
|
137
138
|
using Delayed Job
|
138
139
|
test_files:
|
139
140
|
- spec/backgrounder/backends_spec.rb
|
141
|
+
- spec/backgrounder/orm/activemodel_spec.rb
|
142
|
+
- spec/backgrounder/orm/base_spec.rb
|
140
143
|
- spec/backgrounder/workers/fixtures/test.jpg
|
141
144
|
- spec/backgrounder/workers/process_asset_spec.rb
|
142
145
|
- spec/backgrounder/workers/store_asset_spec.rb
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module CarrierWave
|
2
|
-
module Backgrounder
|
3
|
-
module ORM
|
4
|
-
|
5
|
-
module Mongoid
|
6
|
-
include CarrierWave::Backgrounder::ORM::Base
|
7
|
-
|
8
|
-
def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
|
9
|
-
super
|
10
|
-
|
11
|
-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
12
|
-
def trigger_#{column}_background_processing?
|
13
|
-
process_#{column}_upload != true && (#{column}_changed? || remote_#{column}_url.present?)
|
14
|
-
end
|
15
|
-
RUBY
|
16
|
-
end
|
17
|
-
|
18
|
-
def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
|
19
|
-
super
|
20
|
-
|
21
|
-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
22
|
-
def trigger_#{column}_background_storage?
|
23
|
-
process_#{column}_upload != true && (#{column}_changed? || remote_#{column}_url.present?)
|
24
|
-
end
|
25
|
-
RUBY
|
26
|
-
end
|
27
|
-
end # ActiveRecord
|
28
|
-
|
29
|
-
end # ORM
|
30
|
-
end # Backgrounder
|
31
|
-
end # CarrierWave
|
32
|
-
|
33
|
-
Mongoid::Document::ClassMethods.send(:include, ::CarrierWave::Backgrounder::ORM::Mongoid)
|