carrierwave_backgrounder_revived 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +15 -0
  5. data/CHANGELOG.md +99 -0
  6. data/Gemfile +4 -0
  7. data/README.md +229 -0
  8. data/Rakefile +12 -0
  9. data/carrierwave_backgrounder.gemspec +25 -0
  10. data/lib/backgrounder/delay.rb +26 -0
  11. data/lib/backgrounder/orm/activemodel.rb +36 -0
  12. data/lib/backgrounder/orm/base.rb +120 -0
  13. data/lib/backgrounder/orm/data_mapper.rb +63 -0
  14. data/lib/backgrounder/railtie.rb +25 -0
  15. data/lib/backgrounder/support/backends.rb +94 -0
  16. data/lib/backgrounder/version.rb +5 -0
  17. data/lib/backgrounder/workers.rb +6 -0
  18. data/lib/backgrounder/workers/base.rb +42 -0
  19. data/lib/backgrounder/workers/class_methods.rb +14 -0
  20. data/lib/backgrounder/workers/process_asset.rb +10 -0
  21. data/lib/backgrounder/workers/process_asset_mixin.rb +28 -0
  22. data/lib/backgrounder/workers/store_asset.rb +10 -0
  23. data/lib/backgrounder/workers/store_asset_mixin.rb +43 -0
  24. data/lib/carrierwave_backgrounder.rb +35 -0
  25. data/lib/generators/carrierwave_backgrounder/USAGE +8 -0
  26. data/lib/generators/carrierwave_backgrounder/install_generator.rb +20 -0
  27. data/lib/generators/carrierwave_backgrounder/templates/config/initializers/carrierwave_backgrounder.rb +10 -0
  28. data/spec/backgrounder/orm/activemodel_spec.rb +97 -0
  29. data/spec/backgrounder/orm/base_spec.rb +74 -0
  30. data/spec/backgrounder/support/backends_spec.rb +234 -0
  31. data/spec/backgrounder/workers/fixtures/images/test.jpg +0 -0
  32. data/spec/backgrounder/workers/process_asset_spec.rb +70 -0
  33. data/spec/backgrounder/workers/store_asset_spec.rb +137 -0
  34. data/spec/spec_helper.rb +20 -0
  35. data/spec/support/backend_constants.rb +58 -0
  36. data/spec/support/mock_worker.rb +22 -0
  37. metadata +151 -0
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'backgrounder/workers/process_asset'
4
+
5
+ RSpec.describe CarrierWave::Workers::ProcessAsset do
6
+ let(:worker_class) { CarrierWave::Workers::ProcessAsset }
7
+ let(:user) { double('User') }
8
+ let!(:worker) { worker_class.new(user, '22', :image) }
9
+
10
+ describe ".perform" do
11
+ it 'creates a new instance and calls perform' do
12
+ args = [user, '22', :image]
13
+ expect(worker_class).to receive(:new).with(*args).and_return(worker)
14
+ expect_any_instance_of(worker_class).to receive(:perform)
15
+
16
+ worker_class.perform(*args)
17
+ end
18
+ end
19
+
20
+ describe "#perform" do
21
+ let(:image) { double('UserAsset') }
22
+
23
+ before do
24
+ allow(user).to receive(:find).with('22').and_return(user).once
25
+ allow(user).to receive(:image).twice.and_return(image)
26
+ allow(user).to receive(:process_image_upload=).with(true).once
27
+ allow(image).to receive(:recreate_versions!).once.and_return(true)
28
+ end
29
+
30
+ it 'processes versions with image_processing column' do
31
+ expect(user).to receive(:respond_to?).with(:image_processing).once.and_return(true)
32
+ expect(user).to receive(:update_attribute).with(:image_processing, false).once
33
+ worker.perform
34
+ end
35
+
36
+ it 'processes versions without image_processing column' do
37
+ expect(user).to receive(:respond_to?).with(:image_processing).once.and_return(false)
38
+ expect(user).to receive(:update_attribute).never
39
+ worker.perform
40
+ end
41
+ end
42
+
43
+ describe '#perform with args' do
44
+ let(:admin) { double('Admin') }
45
+ let(:avatar) { double('AdminAsset') }
46
+ let(:worker) { worker_class.new }
47
+
48
+ before do
49
+ allow(admin).to receive(:find).with('23').and_return(admin).once
50
+ allow(admin).to receive(:avatar).twice.and_return(avatar)
51
+ allow(admin).to receive(:process_avatar_upload=).with(true).once
52
+ allow(admin).to receive(:respond_to?).with(:avatar_processing).once.and_return(false)
53
+ allow(avatar).to receive(:recreate_versions!).once.and_return(true)
54
+
55
+ worker.perform admin, '23', :avatar
56
+ end
57
+
58
+ it 'sets klass' do
59
+ expect(worker.klass).to eql(admin)
60
+ end
61
+
62
+ it 'sets column' do
63
+ expect(worker.id).to eql('23')
64
+ end
65
+
66
+ it 'sets id' do
67
+ expect(worker.column).to eql(:avatar)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,137 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'backgrounder/workers/store_asset'
4
+
5
+ RSpec.describe CarrierWave::Workers::StoreAsset do
6
+ let(:fixtures_path) { File.expand_path('../fixtures/images', __FILE__) }
7
+ let(:worker_class) { CarrierWave::Workers::StoreAsset }
8
+ let(:user) { double('User') }
9
+ let!(:worker) { worker_class.new(user, '22', :image) }
10
+
11
+ describe ".perform" do
12
+ it 'creates a new instance and calls perform' do
13
+ args = [user, '22', :image]
14
+ expect(worker_class).to receive(:new).with(*args).and_return(worker)
15
+ expect_any_instance_of(worker_class).to receive(:perform)
16
+ worker_class.perform(*args)
17
+ end
18
+ end
19
+
20
+ describe "#perform" do
21
+ let(:image) { double('UserAsset') }
22
+
23
+ before do
24
+ allow(image).to receive(:root).once.and_return(File.expand_path('..', __FILE__))
25
+ allow(image).to receive(:cache_dir).once.and_return('fixtures')
26
+ allow(user).to receive(:image_tmp).twice.and_return('images/test.jpg')
27
+ allow(user).to receive(:find).with('22').once.and_return(user)
28
+ allow(user).to receive(:image).once.and_return(image)
29
+ allow(user).to receive(:process_image_upload=).with(true).once
30
+ allow(user).to receive(:image=).once
31
+ allow(user).to receive(:image_tmp=).with(nil).once
32
+ end
33
+
34
+ it 'removes tmp directory on success' do
35
+ expect(FileUtils).to receive(:rm_r).with(fixtures_path, :force => true).once
36
+ expect(user).to receive(:save!).once.and_return(true)
37
+ worker.perform
38
+ end
39
+
40
+ it 'does not remove the tmp directory if save! fails' do
41
+ expect(FileUtils).to receive(:rm_r).never
42
+ expect(user).to receive(:save!).once.and_return(false)
43
+ worker.perform
44
+ end
45
+
46
+ it 'sets the cache_path' do
47
+ expect(user).to receive(:save!).once.and_return(false)
48
+ worker.perform
49
+ expect(worker.cache_path).to eql(fixtures_path + '/test.jpg')
50
+ end
51
+
52
+ it 'sets the tmp_directory' do
53
+ expect(user).to receive(:save!).once.and_return(false)
54
+ worker.perform
55
+ expect(worker.tmp_directory).to eql(fixtures_path)
56
+ end
57
+ end
58
+
59
+ describe '#perform with args' do
60
+ let(:admin) { double('Admin') }
61
+ let(:image) { double('AdminAsset') }
62
+ let(:worker) { worker_class.new }
63
+
64
+ before do
65
+ allow(image).to receive(:root).once.and_return(File.expand_path('..', __FILE__))
66
+ allow(image).to receive(:cache_dir).once.and_return('fixtures')
67
+ allow(admin).to receive(:avatar_tmp).twice.and_return('images/test.jpg')
68
+ allow(admin).to receive(:find).with('23').once.and_return(admin)
69
+ allow(admin).to receive(:avatar).once.and_return(image)
70
+ allow(admin).to receive(:process_avatar_upload=).with(true).once
71
+ allow(admin).to receive(:avatar=).once
72
+ allow(admin).to receive(:avatar_tmp=).with(nil).once
73
+ allow(admin).to receive(:save!).once.and_return(false)
74
+ worker.perform admin, '23', :avatar
75
+ end
76
+
77
+ it 'sets klass' do
78
+ expect(worker.klass).to eql(admin)
79
+ end
80
+
81
+ it 'sets column' do
82
+ expect(worker.id).to eql('23')
83
+ end
84
+
85
+ it 'sets id' do
86
+ expect(worker.column).to eql(:avatar)
87
+ end
88
+ end
89
+
90
+ describe '#store_directories' do
91
+ let(:record) { double('Record') }
92
+
93
+ context 'cache_path' do
94
+ it 'sets the cache_path correctly if a full path is set for the cache_dir' do
95
+ root = '/Users/lar/Sites/bunker/public'
96
+ cache_dir = '/Users/lar/Sites/bunker/tmp/uploads'
97
+ asset = double(:cache_dir => cache_dir, :root => root)
98
+ expect(record).to receive(:image).and_return(asset)
99
+ expect(record).to receive(:image_tmp).and_return('images/test.jpg')
100
+ worker.send :store_directories, record
101
+ expect(worker.cache_path).to eql('/Users/lar/Sites/bunker/tmp/uploads/images/test.jpg')
102
+ end
103
+
104
+ it 'sets the cache_path correctly if a partial path is set for cache_dir' do
105
+ root = '/Users/lar/Sites/bunker/public'
106
+ cache_dir = 'uploads/tmp'
107
+ asset = double(:cache_dir => cache_dir, :root => root)
108
+ expect(record).to receive(:image).and_return(asset)
109
+ expect(record).to receive(:image_tmp).and_return('images/test.jpg')
110
+ worker.send :store_directories, record
111
+ expect(worker.cache_path).to eql('/Users/lar/Sites/bunker/public/uploads/tmp/images/test.jpg')
112
+ end
113
+ end
114
+
115
+ context 'tmp_directory' do
116
+ it 'sets the tmp_directory correctly if a full path is set for the cache_dir' do
117
+ root = '/Users/lar/Sites/bunker/public'
118
+ cache_dir = '/Users/lar/Sites/bunker/tmp/uploads'
119
+ asset = double(:cache_dir => cache_dir, :root => root)
120
+ expect(record).to receive(:image).and_return(asset)
121
+ expect(record).to receive(:image_tmp).and_return('images/test.jpg')
122
+ worker.send :store_directories, record
123
+ expect(worker.tmp_directory).to eql('/Users/lar/Sites/bunker/tmp/uploads/images')
124
+ end
125
+
126
+ it 'sets the tmp_directory correctly if a partial path is set for cache_dir' do
127
+ root = '/Users/lar/Sites/bunker/public'
128
+ cache_dir = 'uploads/tmp'
129
+ asset = double(:cache_dir => cache_dir, :root => root)
130
+ expect(record).to receive(:image).and_return(asset)
131
+ expect(record).to receive(:image_tmp).and_return('images/test.jpg')
132
+ worker.send :store_directories, record
133
+ expect(worker.tmp_directory).to eql('/Users/lar/Sites/bunker/public/uploads/tmp/images')
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'logger'
5
+ require 'carrierwave_backgrounder'
6
+
7
+ module WarningSuppression
8
+ def suppress_warnings
9
+ original_verbosity = $VERBOSE
10
+ $VERBOSE = nil
11
+ result = yield
12
+ $VERBOSE = original_verbosity
13
+ return result
14
+ end
15
+ end
16
+
17
+ RSpec.configure do |c|
18
+ c.include WarningSuppression
19
+ end
20
+
@@ -0,0 +1,58 @@
1
+ # Fixture module declarations for backend detection testing
2
+
3
+ module GirlFriday
4
+ class WorkQueue
5
+ end
6
+ end
7
+
8
+ module Delayed
9
+ class Job
10
+ def self.column_names
11
+ %w(id priority attempts handler queue last_error run_at locked_at failed_at locked_by created_at updated_at)
12
+ end
13
+
14
+ column_names.each do |column_name|
15
+ define_method(column_name) { nil }
16
+ end
17
+ end
18
+ end
19
+
20
+ module Resque
21
+ end
22
+
23
+ module Qu
24
+ end
25
+
26
+ module Sidekiq
27
+ module Client
28
+ end
29
+
30
+ module Worker
31
+ def self.included(base)
32
+ base.extend(ClassMethods)
33
+ end
34
+
35
+ module ClassMethods
36
+ def sidekiq_options(opts = {})
37
+ opts
38
+ end
39
+
40
+ def client_push(item)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ module QC
47
+ end
48
+
49
+ module SuckerPunch
50
+ class Queue
51
+ end
52
+ end
53
+
54
+ module Rails
55
+ def self.logger
56
+ @logger ||= Logger.new(STDOUT)
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ class MockWorker < Struct.new(:klass, :id, :column)
2
+ def self.perform(*args)
3
+ new(*args).perform
4
+ end
5
+
6
+ def perform(*args)
7
+ set_args(*args) unless args.empty?
8
+ end
9
+
10
+ def set_args(klass, id, column)
11
+ self.klass, self.id, self.column = klass, id, column
12
+ end
13
+ end
14
+
15
+ class MockSidekiqWorker < MockWorker
16
+ include Sidekiq::Worker
17
+ end
18
+
19
+ class MockNamedSidekiqWorker < MockWorker
20
+ include Sidekiq::Worker
21
+ sidekiq_options queue: :even_better_name
22
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave_backgrounder_revived
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Larry Sprock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.5'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: mime-types
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.99'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.99'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 3.5.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 3.5.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description:
76
+ email:
77
+ - larry@lucidbleu.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".rspec"
84
+ - ".travis.yml"
85
+ - CHANGELOG.md
86
+ - Gemfile
87
+ - README.md
88
+ - Rakefile
89
+ - carrierwave_backgrounder.gemspec
90
+ - lib/backgrounder/delay.rb
91
+ - lib/backgrounder/orm/activemodel.rb
92
+ - lib/backgrounder/orm/base.rb
93
+ - lib/backgrounder/orm/data_mapper.rb
94
+ - lib/backgrounder/railtie.rb
95
+ - lib/backgrounder/support/backends.rb
96
+ - lib/backgrounder/version.rb
97
+ - lib/backgrounder/workers.rb
98
+ - lib/backgrounder/workers/base.rb
99
+ - lib/backgrounder/workers/class_methods.rb
100
+ - lib/backgrounder/workers/process_asset.rb
101
+ - lib/backgrounder/workers/process_asset_mixin.rb
102
+ - lib/backgrounder/workers/store_asset.rb
103
+ - lib/backgrounder/workers/store_asset_mixin.rb
104
+ - lib/carrierwave_backgrounder.rb
105
+ - lib/generators/carrierwave_backgrounder/USAGE
106
+ - lib/generators/carrierwave_backgrounder/install_generator.rb
107
+ - lib/generators/carrierwave_backgrounder/templates/config/initializers/carrierwave_backgrounder.rb
108
+ - spec/backgrounder/orm/activemodel_spec.rb
109
+ - spec/backgrounder/orm/base_spec.rb
110
+ - spec/backgrounder/support/backends_spec.rb
111
+ - spec/backgrounder/workers/fixtures/images/test.jpg
112
+ - spec/backgrounder/workers/process_asset_spec.rb
113
+ - spec/backgrounder/workers/store_asset_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/backend_constants.rb
116
+ - spec/support/mock_worker.rb
117
+ homepage: https://github.com/lardawge/carrierwave_backgrounder
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.11
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Offload CarrierWave's image processing and storage to a background process
141
+ using Delayed Job, Resque, Sidekiq, Qu, Queue Classic or Girl Friday
142
+ test_files:
143
+ - spec/backgrounder/orm/activemodel_spec.rb
144
+ - spec/backgrounder/orm/base_spec.rb
145
+ - spec/backgrounder/support/backends_spec.rb
146
+ - spec/backgrounder/workers/fixtures/images/test.jpg
147
+ - spec/backgrounder/workers/process_asset_spec.rb
148
+ - spec/backgrounder/workers/store_asset_spec.rb
149
+ - spec/spec_helper.rb
150
+ - spec/support/backend_constants.rb
151
+ - spec/support/mock_worker.rb