carrierwave_backgrounder 0.4.3 → 1.0.0.beta.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +8 -4
  4. data/CHANGELOG.md +14 -0
  5. data/Gemfile +1 -1
  6. data/README.md +59 -67
  7. data/carrierwave_backgrounder.gemspec +7 -2
  8. data/lib/backgrounder/orm/activemodel.rb +7 -7
  9. data/lib/backgrounder/orm/base.rb +18 -12
  10. data/lib/backgrounder/support/backends.rb +1 -45
  11. data/lib/backgrounder/version.rb +1 -1
  12. data/lib/backgrounder/workers/active_job/process_asset.rb +14 -0
  13. data/lib/backgrounder/workers/active_job/store_asset.rb +14 -0
  14. data/lib/backgrounder/workers/process_asset.rb +2 -0
  15. data/lib/backgrounder/workers/process_asset_mixin.rb +16 -7
  16. data/lib/backgrounder/workers/store_asset.rb +2 -0
  17. data/lib/backgrounder/workers/store_asset_mixin.rb +14 -19
  18. data/lib/backgrounder/workers.rb +0 -2
  19. data/lib/carrierwave_backgrounder.rb +24 -10
  20. data/lib/generators/carrierwave_backgrounder/install_generator.rb +1 -1
  21. data/lib/generators/carrierwave_backgrounder/templates/config/initializers/carrierwave_backgrounder.rb +1 -7
  22. data/spec/backgrounder/orm/activemodel_spec.rb +2 -5
  23. data/spec/backgrounder/support/backends_spec.rb +0 -159
  24. data/spec/backgrounder/workers/process_asset_spec.rb +8 -6
  25. data/spec/backgrounder/workers/store_asset_spec.rb +20 -99
  26. data/spec/integrations/process_in_background_multi_upload_spec.rb +81 -0
  27. data/spec/integrations/process_in_background_spec.rb +75 -0
  28. data/spec/integrations/store_in_background_multi_upload_spec.rb +87 -0
  29. data/spec/integrations/store_in_background_spec.rb +86 -0
  30. data/spec/rails_helper.rb +27 -0
  31. data/spec/spec_helper.rb +2 -0
  32. data/spec/support/dummy_app/Gemfile +49 -0
  33. data/spec/support/dummy_app/README.md +24 -0
  34. data/spec/support/dummy_app/Rakefile +6 -0
  35. data/spec/support/dummy_app/app/controllers/application_controller.rb +2 -0
  36. data/spec/support/dummy_app/app/controllers/concerns/.keep +0 -0
  37. data/spec/support/dummy_app/app/jobs/application_job.rb +7 -0
  38. data/spec/support/dummy_app/app/models/admin.rb +10 -0
  39. data/spec/support/dummy_app/app/models/application_record.rb +3 -0
  40. data/spec/support/dummy_app/app/models/concerns/.keep +0 -0
  41. data/spec/support/dummy_app/app/models/user.rb +10 -0
  42. data/spec/support/dummy_app/app/uploaders/avatar_uploader.rb +67 -0
  43. data/spec/support/dummy_app/bin/bundle +114 -0
  44. data/spec/support/dummy_app/bin/rails +4 -0
  45. data/spec/support/dummy_app/bin/rake +4 -0
  46. data/spec/support/dummy_app/bin/setup +33 -0
  47. data/spec/support/dummy_app/config/application.rb +39 -0
  48. data/spec/support/dummy_app/config/boot.rb +3 -0
  49. data/spec/support/dummy_app/config/database.yml +25 -0
  50. data/spec/support/dummy_app/config/environment.rb +8 -0
  51. data/spec/support/dummy_app/config/environments/development.rb +65 -0
  52. data/spec/support/dummy_app/config/environments/production.rb +86 -0
  53. data/spec/support/dummy_app/config/environments/test.rb +63 -0
  54. data/spec/support/dummy_app/config/initializers/carrierwave_backgrounder.rb +5 -0
  55. data/spec/support/dummy_app/config/initializers/cors.rb +16 -0
  56. data/spec/support/dummy_app/config/initializers/filter_parameter_logging.rb +8 -0
  57. data/spec/support/dummy_app/config/initializers/inflections.rb +16 -0
  58. data/spec/support/dummy_app/config/locales/en.yml +33 -0
  59. data/spec/support/dummy_app/config/routes.rb +6 -0
  60. data/spec/support/dummy_app/config/storage.yml +34 -0
  61. data/spec/support/dummy_app/config.ru +6 -0
  62. data/spec/support/dummy_app/db/migrate/20230804214459_create_users.rb +9 -0
  63. data/spec/support/dummy_app/db/migrate/20230807165013_add_avatar_tmp_column_to_user.rb +5 -0
  64. data/spec/support/dummy_app/db/migrate/20230808233036_add_avatar_processing_flag_to_users.rb +5 -0
  65. data/spec/support/dummy_app/db/migrate/20230809215320_create_admins.rb +10 -0
  66. data/spec/support/dummy_app/db/migrate/20230810182011_add_images_to_users.rb +7 -0
  67. data/spec/support/dummy_app/db/migrate/20230811155811_add_images_to_admins.rb +6 -0
  68. data/spec/support/dummy_app/db/schema.rb +34 -0
  69. data/spec/support/dummy_app/db/seeds.rb +7 -0
  70. data/spec/support/dummy_app/lib/tasks/.keep +0 -0
  71. data/spec/support/dummy_app/log/.keep +0 -0
  72. data/spec/support/dummy_app/public/robots.txt +1 -0
  73. data/spec/support/dummy_app/storage/.keep +0 -0
  74. data/spec/support/dummy_app/tmp/.keep +0 -0
  75. data/spec/support/dummy_app/tmp/development_secret.txt +1 -0
  76. data/spec/support/dummy_app/tmp/images/.gitkeep +0 -0
  77. data/spec/support/dummy_app/tmp/pids/.keep +0 -0
  78. data/spec/support/dummy_app/tmp/storage/.keep +0 -0
  79. data/spec/support/dummy_app/vendor/.keep +0 -0
  80. data/spec/support/fixtures/images/test-1.jpg +0 -0
  81. data/spec/support/fixtures/images/test-2.jpg +0 -0
  82. data/spec/support/global_macros.rb +29 -0
  83. data/spec/support/mock_worker.rb +2 -0
  84. metadata +203 -15
  85. data/spec/support/backend_constants.rb +0 -58
@@ -1,3 +1,4 @@
1
+ require 'fileutils'
1
2
  require 'active_support/core_ext/object'
2
3
  require 'backgrounder/support/backends'
3
4
  require 'backgrounder/orm/base'
@@ -7,10 +8,32 @@ module CarrierWave
7
8
  module Backgrounder
8
9
  include Support::Backends
9
10
 
11
+ class << self
12
+ attr_reader :worker_klass
13
+ end
14
+
10
15
  def self.configure
11
16
  yield self
12
- case @backend
17
+
18
+ case backend
19
+ when :active_job
20
+ @worker_klass = "CarrierWave::Workers::ActiveJob"
21
+
22
+ require 'active_job'
23
+ require 'backgrounder/workers/active_job/process_asset'
24
+ require 'backgrounder/workers/active_job/store_asset'
25
+
26
+ queue_name = queue_options['queue'] || 'carrierwave'
27
+
28
+ ::CarrierWave::Workers::ActiveJob::ProcessAsset.class_eval do
29
+ queue_as queue_name
30
+ end
31
+ ::CarrierWave::Workers::ActiveJob::StoreAsset.class_eval do
32
+ queue_as queue_name
33
+ end
13
34
  when :sidekiq
35
+ @worker_klass = "CarrierWave::Workers"
36
+
14
37
  require 'sidekiq'
15
38
  ::CarrierWave::Workers::ProcessAsset.class_eval do
16
39
  include ::Sidekiq::Worker
@@ -18,17 +41,8 @@ module CarrierWave
18
41
  ::CarrierWave::Workers::StoreAsset.class_eval do
19
42
  include ::Sidekiq::Worker
20
43
  end
21
- when :sucker_punch
22
- require 'sucker_punch'
23
- ::CarrierWave::Workers::ProcessAsset.class_eval do
24
- include ::SuckerPunch::Job
25
- end
26
- ::CarrierWave::Workers::StoreAsset.class_eval do
27
- include ::SuckerPunch::Job
28
- end
29
44
  end
30
45
  end
31
-
32
46
  end
33
47
  end
34
48
 
@@ -10,7 +10,7 @@ module CarrierwaveBackgrounder
10
10
  def info_config
11
11
  puts <<-EOF
12
12
 
13
- \e[33mBy default :delayed_job is used as the backend for carrierwave_backgrounder with :carrierwave as the queue name.
13
+ \e[33mBy default :active_job is used as the backend for carrierwave_backgrounder with :carrierwave as the queue name.
14
14
  To change this, edit config/initializers/carrierwave_backgrounder.rb.\e[0m
15
15
 
16
16
  EOF
@@ -1,10 +1,4 @@
1
1
  CarrierWave::Backgrounder.configure do |c|
2
- c.backend :delayed_job, queue: :carrierwave
3
- # c.backend :active_job, queue: :carrierwave
4
- # c.backend :resque, queue: :carrierwave
2
+ c.backend :active_job, queue: :carrierwave
5
3
  # c.backend :sidekiq, queue: :carrierwave
6
- # c.backend :girl_friday, queue: :carrierwave
7
- # c.backend :sucker_punch, queue: :carrierwave
8
- # c.backend :qu, queue: :carrierwave
9
- # c.backend :qc
10
4
  end
@@ -8,6 +8,7 @@ RSpec.describe CarrierWave::Backgrounder::ORM::ActiveModel do
8
8
  def self.before_save(method, opts); nil; end
9
9
  def self.after_commit(method, opts); nil; end
10
10
  def avatar_changed?; nil; end
11
+ def avatar_present?; true; end
11
12
  def remote_avatar_url; OpenStruct.new(:present? => true); end
12
13
  def remove_avatar?; false; end
13
14
  def previous_changes; {}; end
@@ -54,7 +55,7 @@ RSpec.describe CarrierWave::Backgrounder::ORM::ActiveModel do
54
55
  end
55
56
 
56
57
  it "returns true if alternate column is changed" do
57
- expect(instance).to receive(:some_other_column_changed?).and_return(true)
58
+ expect(instance).to receive(:previous_changes).and_return({:some_other_column => true})
58
59
  expect(instance.avatar_updated?).to be_truthy
59
60
  end
60
61
  end
@@ -66,20 +67,17 @@ RSpec.describe CarrierWave::Backgrounder::ORM::ActiveModel do
66
67
 
67
68
  it "calls column_changed?" do
68
69
  expect(instance).to receive(:process_avatar_upload).and_return(false)
69
- expect(instance).to receive(:avatar_changed?)
70
70
  expect(instance.enqueue_avatar_background_job?).to be_truthy
71
71
  end
72
72
 
73
73
  it "calls previous_changes" do
74
74
  expect(instance).to receive(:process_avatar_upload).and_return(false)
75
- expect(instance).to receive(:avatar_changed?).and_return(false)
76
75
  expect(instance).to receive(:previous_changes).and_return({:avatar => true})
77
76
  expect(instance.enqueue_avatar_background_job?).to be_truthy
78
77
  end
79
78
 
80
79
  it "calls avatar_remote_url" do
81
80
  expect(instance).to receive(:process_avatar_upload).and_return(false)
82
- expect(instance).to receive(:avatar_changed?).and_return(false)
83
81
  expect(instance).to receive(:previous_changes).and_return({})
84
82
  expect(instance).to receive(:remote_avatar_url).and_return('yup')
85
83
  expect(instance.enqueue_avatar_background_job?).to be_truthy
@@ -87,7 +85,6 @@ RSpec.describe CarrierWave::Backgrounder::ORM::ActiveModel do
87
85
 
88
86
  it "calls avatar_cache" do
89
87
  expect(instance).to receive(:process_avatar_upload).and_return(false)
90
- expect(instance).to receive(:avatar_changed?).and_return(false)
91
88
  expect(instance).to receive(:previous_changes).and_return({})
92
89
  expect(instance).to receive(:remote_avatar_url).and_return(nil)
93
90
  expect(instance).to receive(:avatar_cache).and_return('yup')
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'support/backend_constants'
3
2
  require 'support/mock_worker'
4
3
 
5
4
  module CarrierWave::Backgrounder
@@ -41,83 +40,6 @@ module CarrierWave::Backgrounder
41
40
  end
42
41
  end
43
42
 
44
- context 'delayed_job' do
45
- before do
46
- @mock_worker = Class.new do
47
- def self.perform(*args); new(*args).perform; end
48
- end
49
-
50
- allow(MockWorker).to receive(:new).and_return(worker)
51
- end
52
-
53
- context 'queue column exists' do
54
- it 'does not pass the queue name if none passed to #backend' do
55
- mock_module.backend :delayed_job
56
- expect(Delayed::Job).to receive(:enqueue).with(worker, {})
57
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
58
- end
59
-
60
- it 'sets the queue name to the queue name passed to #backend' do
61
- mock_module.backend :delayed_job, :queue => :awesome_queue
62
- expect(Delayed::Job).to receive(:enqueue).with(worker, :queue => :awesome_queue)
63
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
64
- end
65
- end
66
-
67
- context 'priority set in config' do
68
- it 'sets the priority which is passed to #backend' do
69
- mock_module.backend :delayed_job, :priority => 5
70
- expect(Delayed::Job).to receive(:enqueue).with(worker, :priority => 5)
71
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
72
- end
73
- end
74
-
75
- context 'queue column does not exist' do
76
- before do
77
- column_names = Delayed::Job.column_names.tap { |cn| cn.delete('queue') }
78
- allow(Delayed::Job).to receive(:column_names).and_return(column_names)
79
- Delayed::Job.class_eval { remove_method(:queue) }
80
- end
81
-
82
- after do
83
- Delayed::Job.class_eval { define_method(:queue) { nil } }
84
- end
85
-
86
- it 'does not pass a queue name if none passed to #backend' do
87
- mock_module.backend :delayed_job
88
- expect(Delayed::Job).to receive(:enqueue).with(worker, {})
89
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
90
- end
91
-
92
- it 'does not pass a queue name and logs a warning message if a queue name is passed to #backend' do
93
- mock_module.backend :delayed_job, :queue => :awesome_queue
94
- expect(Rails.logger).to receive(:warn).with(instance_of(String))
95
- expect(Delayed::Job).to receive(:enqueue).with(worker, {})
96
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
97
- end
98
- end
99
- end
100
-
101
- context 'resque' do
102
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
103
-
104
- before do
105
- allow(Resque).to receive(:enqueue).with(*args)
106
- end
107
-
108
- it 'sets a variable with the queue name, defaults to :carrierwave' do
109
- mock_module.backend :resque
110
- mock_module.enqueue_for_backend(*args)
111
- expect(MockWorker.instance_variable_get '@queue').to eql(:carrierwave)
112
- end
113
-
114
- it 'sets a variable to the queue name passed to #backend' do
115
- mock_module.backend :resque, :queue => :awesome_queue
116
- mock_module.enqueue_for_backend(*args)
117
- expect(MockWorker.instance_variable_get '@queue').to eql(:awesome_queue)
118
- end
119
- end
120
-
121
43
  context 'sidekiq' do
122
44
  let(:args) { ['FakeClass', 1, :image] }
123
45
 
@@ -148,87 +70,6 @@ module CarrierWave::Backgrounder
148
70
  mock_module.enqueue_for_backend(MockNamedSidekiqWorker, *args)
149
71
  end
150
72
  end
151
-
152
- context 'girl_friday' do
153
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
154
-
155
- it 'instantiates a GirlFriday work queue if one does not exist' do
156
- mock_module.backend :girl_friday
157
- expect(GirlFriday::WorkQueue).to receive(:new).with(:carrierwave, {}).and_return([])
158
- mock_module.enqueue_for_backend(*args)
159
- end
160
-
161
- it 'instantiates a GirlFriday work queue passing the args to the queue' do
162
- mock_module.backend :girl_friday, :queue => :awesome_queue, :size => 3
163
- expect(GirlFriday::WorkQueue).to receive(:new).with(:awesome_queue, {:size => 3}).and_return([])
164
- mock_module.enqueue_for_backend(*args)
165
- end
166
-
167
- it 'does not instantiate a GirlFriday work queue if one exists' do
168
- mock_module.backend :girl_friday
169
- mock_module.instance_variable_set('@girl_friday_queue', [])
170
- expect(GirlFriday::WorkQueue).to receive(:new).never
171
- mock_module.enqueue_for_backend(*args)
172
- end
173
-
174
- it 'add a worker to the girl_friday queue' do
175
- expected = [{ :worker => MockWorker.new('FakeClass', 1, :image) }]
176
- mock_module.backend :girl_friday
177
- mock_module.instance_variable_set('@girl_friday_queue', [])
178
- mock_module.enqueue_for_backend(*args)
179
- expect(mock_module.instance_variable_get '@girl_friday_queue').to eql(expected)
180
- end
181
- end
182
-
183
- context 'sucker_punch' do
184
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
185
- let(:job) { double('job') }
186
-
187
- it 'invokes a new worker' do
188
- expect(MockWorker).to receive(:new).and_return(worker)
189
- expect(worker).to receive(:async).and_return(job)
190
- expect(job).to receive(:perform).with('FakeClass', 1, :image)
191
- mock_module.backend :sucker_punch
192
- mock_module.enqueue_for_backend(*args)
193
- end
194
- end
195
-
196
- context 'qu' do
197
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
198
- before do
199
- allow(Qu).to receive(:enqueue).with(*args)
200
- end
201
-
202
- it 'sets a variable with the queue name, defaults to :carrierwave' do
203
- mock_module.backend :qu
204
- mock_module.enqueue_for_backend(*args)
205
- expect(MockWorker.instance_variable_get '@queue').to eql(:carrierwave)
206
- end
207
-
208
- it 'sets a variable to the queue name passed to #backend' do
209
- mock_module.backend :qu, :queue => :awesome_queue
210
- mock_module.enqueue_for_backend(*args)
211
- expect(MockWorker.instance_variable_get '@queue').to eql(:awesome_queue)
212
- end
213
- end
214
-
215
- context 'qc' do
216
- it 'calls enqueue with the passed args' do
217
- expect(QC).to receive(:enqueue).with("MockWorker.perform", 'FakeClass', 1, 'image')
218
- mock_module.backend :qc
219
- mock_module.enqueue_for_backend(MockWorker, 'FakeClass', 1, :image)
220
- end
221
- end
222
-
223
- context 'immediate' do
224
- it 'instantiates a worker passing the args and calls perform' do
225
- worker = double('Worker')
226
- expect(MockWorker).to receive(:new).with('FakeClass', 1, :image).and_return(worker)
227
- expect(worker).to receive(:perform)
228
- mock_module.backend :immediate
229
- mock_module.enqueue_for_backend(MockWorker, 'FakeClass', 1, :image)
230
- end
231
- end
232
73
  end
233
74
  end
234
75
  end
@@ -22,9 +22,10 @@ RSpec.describe CarrierWave::Workers::ProcessAsset do
22
22
 
23
23
  before do
24
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)
25
+ allow(user).to receive(:image).thrice.and_return(image)
26
+
27
+ expect(image).to receive(:recreate_versions!).once.and_return(true)
28
+ expect(image).to receive(:file).and_return(image)
28
29
  end
29
30
 
30
31
  it 'processes versions with image_processing column' do
@@ -48,9 +49,10 @@ RSpec.describe CarrierWave::Workers::ProcessAsset do
48
49
  before do
49
50
  allow(admin).to receive(:find).with('23').and_return(admin).once
50
51
  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)
52
+
53
+ expect(admin).to receive(:respond_to?).with(:avatar_processing).once.and_return(false)
54
+ expect(avatar).to receive(:recreate_versions!).once.and_return(true)
55
+ expect(avatar).to receive(:file).and_return(avatar)
54
56
 
55
57
  worker.perform admin, '23', :avatar
56
58
  end
@@ -8,6 +8,22 @@ RSpec.describe CarrierWave::Workers::StoreAsset do
8
8
  let(:user) { double('User') }
9
9
  let!(:worker) { worker_class.new(user, '22', :image) }
10
10
 
11
+ def stub_worker(obj, asset)
12
+ allow(asset).to receive(:root).once.and_return(File.expand_path('..', __FILE__))
13
+ allow(asset).to receive(:cache_dir).once.and_return('fixtures')
14
+ allow(obj).to receive(:image_tmp).and_return('images/test.jpg')
15
+ allow(obj).to receive(:find).with('22').once.and_return(obj)
16
+ allow(obj).to receive(:image=)
17
+
18
+ expect(asset).to receive(:cache!).once
19
+ expect(asset).to receive(:store!).once
20
+ expect(obj).to receive(:image).twice.and_return(image)
21
+ expect(obj).to receive(:process_image_upload=).with(true).once
22
+ expect(obj).to receive(:image_cache=).with("images/test.jpg").once
23
+ expect(obj).to receive(:image_tmp=).with(nil).once
24
+ expect(obj).to receive(:save!).once
25
+ end
26
+
11
27
  describe ".perform" do
12
28
  it 'creates a new instance and calls perform' do
13
29
  args = [user, '22', :image]
@@ -17,61 +33,14 @@ RSpec.describe CarrierWave::Workers::StoreAsset do
17
33
  end
18
34
  end
19
35
 
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
36
  describe '#perform with args' do
60
37
  let(:admin) { double('Admin') }
61
38
  let(:image) { double('AdminAsset') }
62
39
  let(:worker) { worker_class.new }
63
40
 
64
41
  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
42
+ stub_worker(admin, image)
43
+ worker.perform admin, '22', :image
75
44
  end
76
45
 
77
46
  it 'sets klass' do
@@ -79,59 +48,11 @@ RSpec.describe CarrierWave::Workers::StoreAsset do
79
48
  end
80
49
 
81
50
  it 'sets column' do
82
- expect(worker.id).to eql('23')
51
+ expect(worker.id).to eql('22')
83
52
  end
84
53
 
85
54
  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
55
+ expect(worker.column).to eql(:image)
135
56
  end
136
57
  end
137
58
  end
@@ -0,0 +1,81 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe '::process_in_background multi-upload support', clear_images: true do
4
+ let(:admin) { Admin.new }
5
+
6
+ context 'when assigning an asset' do
7
+ before(:each) do
8
+ admin.update(images: load_files('test-1.jpg', 'test-2.jpg'))
9
+ end
10
+
11
+ it 'creates a temp file and stores the path' do
12
+ expect(file_count("spec/support/dummy_app/public/uploads/admin/images/#{admin.id}")).to eql(2)
13
+ expect(admin.images.present?).to be(true)
14
+ end
15
+
16
+ it 'creates a background job in carrierwave queue' do
17
+ expect(Sidekiq::Queues["carrierwave"].size).to eql(1)
18
+ end
19
+
20
+ it 'sets the <column>_processing flag to true' do
21
+ expect(admin.images_processing).to be(true)
22
+ end
23
+ end
24
+
25
+ context 'when processing the worker' do
26
+ before do
27
+ admin.update(images: load_files('test-1.jpg','test-2.jpg'))
28
+ expect(admin.images_processing).to be(true)
29
+ process_latest_sidekiq_job
30
+ admin.reload
31
+ end
32
+
33
+ it 'creates the versions' do
34
+ version_paths = AvatarUploader.versions.keys.map { |key| admin.images.map { |i| i.send(key).current_path } }.flatten
35
+ version_paths.each { |path| expect(File.exist? path).to be(true) }
36
+ end
37
+
38
+ it 'removes the files tmp directory' do
39
+ expect(file_count('spec/support/dummy_app/tmp/images')).to eql(0)
40
+ end
41
+
42
+ it 'sets the <column>_processing flag to false' do
43
+ expect(admin.avatar_processing).to be(false)
44
+ end
45
+ end
46
+
47
+ context 'when saving a record' do
48
+ let!(:admin) {
49
+ Sidekiq::Testing.inline! do
50
+ Admin.create(images: load_files('test-1.jpg'))
51
+ end
52
+ }
53
+
54
+ it 'does not enqueue a new job' do
55
+ expect { admin.reload.save }.to_not change(Sidekiq::Queues["carrierwave"], :size)
56
+ end
57
+ end
58
+
59
+ context 'when setting a column for removal' do
60
+ let!(:admin) {
61
+ Sidekiq::Testing.inline! do
62
+ Admin.create(images: load_files('test-1.jpg'))
63
+ end
64
+ }
65
+
66
+ before do
67
+ expect(admin.reload.images.present?).to be(true)
68
+
69
+ admin.remove_images = true
70
+ admin.save!
71
+ end
72
+
73
+ it 'removes the attachment' do
74
+ expect(admin.images.present?).to be(false)
75
+ end
76
+
77
+ it 'does not enqueue a new job' do
78
+ expect(Sidekiq::Queues["carrierwave"].size).to be(0)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,75 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe '::process_in_background', clear_images: true do
4
+ let(:admin) { Admin.new }
5
+
6
+ context 'when assigning an asset' do
7
+ before(:each) do
8
+ admin.update(avatar: load_file('test-1.jpg'))
9
+ end
10
+
11
+ it 'stores the original file' do
12
+ expect(file_count("spec/support/dummy_app/public/uploads/admin/avatar/#{admin.id}")).to eql(1)
13
+ expect(admin.avatar.file.present?).to be(true)
14
+ end
15
+
16
+ it 'creates a background job in carrierwave queue' do
17
+ expect(Sidekiq::Queues["carrierwave"].size).to eql(1)
18
+ end
19
+
20
+ it 'sets the <column>_processing flag to true' do
21
+ expect(admin.avatar_processing).to be(true)
22
+ end
23
+ end
24
+
25
+ context 'when processing the worker' do
26
+ before do
27
+ admin.update(avatar: load_file('test-1.jpg'))
28
+ expect(admin.avatar_processing).to be(true)
29
+ process_latest_sidekiq_job
30
+ admin.reload
31
+ end
32
+
33
+ it 'creates the versions' do
34
+ version_paths = admin.avatar.versions.keys.map { |key| admin.avatar.send(key).current_path }
35
+ version_paths.each { |path| expect(File.exist? path).to be(true) }
36
+ end
37
+
38
+ it 'removes the files tmp directory' do
39
+ expect(file_count('spec/support/dummy_app/tmp/images')).to eql(0)
40
+ end
41
+
42
+ it 'sets the <column>_processing flag to false' do
43
+ expect(admin.avatar_processing).to be(false)
44
+ end
45
+ end
46
+
47
+ context 'when saving a record' do
48
+ let!(:admin) {
49
+ Sidekiq::Testing.inline! do
50
+ Admin.create(avatar: load_file('test-1.jpg'))
51
+ end
52
+ }
53
+
54
+ it 'does not enqueue a new job' do
55
+ expect { admin.reload.save }.to_not change(Sidekiq::Queues["carrierwave"], :size)
56
+ end
57
+ end
58
+
59
+ context 'when setting a column for removal' do
60
+ let!(:admin) {
61
+ Sidekiq::Testing.inline! do
62
+ Admin.create(avatar: load_file('test-1.jpg'))
63
+ end
64
+ }
65
+
66
+ it 'removes the attachment' do
67
+ expect(admin.reload.avatar.file.nil?).to be(false)
68
+
69
+ admin.remove_avatar = true
70
+ admin.save!
71
+
72
+ expect(admin.avatar.file.nil?).to be(true)
73
+ end
74
+ end
75
+ end