carrierwave_backgrounder 0.4.2 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -1
  3. data/.travis.yml +8 -2
  4. data/CHANGELOG.md +20 -0
  5. data/Gemfile +1 -1
  6. data/README.md +70 -44
  7. data/carrierwave_backgrounder.gemspec +7 -2
  8. data/lib/backgrounder/orm/activemodel.rb +7 -7
  9. data/lib/backgrounder/orm/base.rb +19 -13
  10. data/lib/backgrounder/orm/data_mapper.rb +1 -1
  11. data/lib/backgrounder/support/backends.rb +4 -44
  12. data/lib/backgrounder/version.rb +1 -1
  13. data/lib/backgrounder/workers/active_job/process_asset.rb +14 -0
  14. data/lib/backgrounder/workers/active_job/store_asset.rb +14 -0
  15. data/lib/backgrounder/workers/base.rb +15 -7
  16. data/lib/backgrounder/workers/class_methods.rb +14 -0
  17. data/lib/backgrounder/workers/process_asset.rb +2 -13
  18. data/lib/backgrounder/workers/process_asset_mixin.rb +37 -0
  19. data/lib/backgrounder/workers/store_asset.rb +2 -26
  20. data/lib/backgrounder/workers/store_asset_mixin.rb +38 -0
  21. data/lib/backgrounder/workers.rb +3 -0
  22. data/lib/carrierwave_backgrounder.rb +11 -10
  23. data/lib/generators/carrierwave_backgrounder/install_generator.rb +1 -1
  24. data/lib/generators/carrierwave_backgrounder/templates/config/initializers/carrierwave_backgrounder.rb +1 -6
  25. data/spec/backgrounder/orm/activemodel_spec.rb +2 -5
  26. data/spec/backgrounder/support/backends_spec.rb +9 -158
  27. data/spec/backgrounder/workers/process_asset_spec.rb +8 -6
  28. data/spec/backgrounder/workers/store_asset_spec.rb +20 -99
  29. data/spec/integrations/process_in_background_multi_upload_spec.rb +81 -0
  30. data/spec/integrations/process_in_background_spec.rb +75 -0
  31. data/spec/integrations/store_in_background_multi_upload_spec.rb +87 -0
  32. data/spec/integrations/store_in_background_spec.rb +86 -0
  33. data/spec/rails_helper.rb +27 -0
  34. data/spec/spec_helper.rb +2 -0
  35. data/spec/support/dummy_app/Gemfile +49 -0
  36. data/spec/support/dummy_app/README.md +24 -0
  37. data/spec/support/dummy_app/Rakefile +6 -0
  38. data/spec/support/dummy_app/app/controllers/application_controller.rb +2 -0
  39. data/spec/support/dummy_app/app/controllers/concerns/.keep +0 -0
  40. data/spec/support/dummy_app/app/jobs/application_job.rb +7 -0
  41. data/spec/support/dummy_app/app/models/admin.rb +10 -0
  42. data/spec/support/dummy_app/app/models/application_record.rb +3 -0
  43. data/spec/support/dummy_app/app/models/concerns/.keep +0 -0
  44. data/spec/support/dummy_app/app/models/user.rb +10 -0
  45. data/spec/support/dummy_app/app/uploaders/avatar_uploader.rb +67 -0
  46. data/spec/support/dummy_app/bin/bundle +114 -0
  47. data/spec/support/dummy_app/bin/rails +4 -0
  48. data/spec/support/dummy_app/bin/rake +4 -0
  49. data/spec/support/dummy_app/bin/setup +33 -0
  50. data/spec/support/dummy_app/config/application.rb +39 -0
  51. data/spec/support/dummy_app/config/boot.rb +3 -0
  52. data/spec/support/dummy_app/config/database.yml +25 -0
  53. data/spec/support/dummy_app/config/environment.rb +8 -0
  54. data/spec/support/dummy_app/config/environments/development.rb +65 -0
  55. data/spec/support/dummy_app/config/environments/production.rb +86 -0
  56. data/spec/support/dummy_app/config/environments/test.rb +61 -0
  57. data/spec/support/dummy_app/config/initializers/carrierwave_backgrounder.rb +4 -0
  58. data/spec/support/dummy_app/config/initializers/cors.rb +16 -0
  59. data/spec/support/dummy_app/config/initializers/filter_parameter_logging.rb +8 -0
  60. data/spec/support/dummy_app/config/initializers/inflections.rb +16 -0
  61. data/spec/support/dummy_app/config/locales/en.yml +33 -0
  62. data/spec/support/dummy_app/config/routes.rb +6 -0
  63. data/spec/support/dummy_app/config/storage.yml +34 -0
  64. data/spec/support/dummy_app/config.ru +6 -0
  65. data/spec/support/dummy_app/db/migrate/20230804214459_create_users.rb +9 -0
  66. data/spec/support/dummy_app/db/migrate/20230807165013_add_avatar_tmp_column_to_user.rb +5 -0
  67. data/spec/support/dummy_app/db/migrate/20230808233036_add_avatar_processing_flag_to_users.rb +5 -0
  68. data/spec/support/dummy_app/db/migrate/20230809215320_create_admins.rb +10 -0
  69. data/spec/support/dummy_app/db/migrate/20230810182011_add_images_to_users.rb +7 -0
  70. data/spec/support/dummy_app/db/migrate/20230811155811_add_images_to_admins.rb +6 -0
  71. data/spec/support/dummy_app/db/schema.rb +34 -0
  72. data/spec/support/dummy_app/db/seeds.rb +7 -0
  73. data/spec/support/dummy_app/lib/tasks/.keep +0 -0
  74. data/spec/support/dummy_app/log/.keep +0 -0
  75. data/spec/support/dummy_app/public/robots.txt +1 -0
  76. data/spec/support/dummy_app/storage/.keep +0 -0
  77. data/spec/support/dummy_app/tmp/.keep +0 -0
  78. data/spec/support/dummy_app/tmp/development_secret.txt +1 -0
  79. data/spec/support/dummy_app/tmp/images/.gitkeep +0 -0
  80. data/spec/support/dummy_app/tmp/pids/.keep +0 -0
  81. data/spec/support/dummy_app/tmp/storage/.keep +0 -0
  82. data/spec/support/dummy_app/vendor/.keep +0 -0
  83. data/spec/support/fixtures/images/test-1.jpg +0 -0
  84. data/spec/support/fixtures/images/test-2.jpg +0 -0
  85. data/spec/support/global_macros.rb +23 -0
  86. data/spec/support/mock_worker.rb +2 -0
  87. metadata +214 -18
  88. data/spec/support/backend_constants.rb +0 -58
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ module CarrierWave
3
+ module Workers
4
+
5
+ module StoreAssetMixin
6
+ include CarrierWave::Workers::Base
7
+
8
+ def self.included(base)
9
+ base.extend CarrierWave::Workers::ClassMethods
10
+ end
11
+
12
+ def perform(*args)
13
+ record = super(*args)
14
+
15
+ return unless record && record.send(:"#{column}_tmp")
16
+
17
+ record.send :"process_#{column}_upload=", true
18
+ record.send :"#{column}_cache=", record.send(:"#{column}_tmp") # Set the cache path
19
+ cache_assets! record.send(:"#{column}") # Trigger version creation
20
+ store_assets! record.send(:"#{column}") # Store the files
21
+ record.send :"#{column}_tmp=", nil
22
+ record.send :"#{column}_processing=", false if record.respond_to?(:"#{column}_processing")
23
+ record.save!
24
+ end
25
+
26
+ private
27
+
28
+ def cache_assets!(asset)
29
+ asset.is_a?(Array) ? asset.map(&:cache!) : asset.cache!
30
+ end
31
+
32
+ def store_assets!(asset)
33
+ asset.is_a?(Array) ? asset.map(&:store!) : asset.store!
34
+ end
35
+ end # StoreAssetMixin
36
+
37
+ end # Workers
38
+ end # Backgrounder
@@ -1,3 +1,6 @@
1
1
  require 'backgrounder/workers/base'
2
+ require 'backgrounder/workers/class_methods'
3
+ require 'backgrounder/workers/process_asset_mixin'
4
+ require 'backgrounder/workers/store_asset_mixin'
2
5
  require 'backgrounder/workers/process_asset'
3
6
  require 'backgrounder/workers/store_asset'
@@ -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,19 @@ 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"
13
21
  when :sidekiq
22
+ @worker_klass = "CarrierWave::Workers"
23
+
14
24
  require 'sidekiq'
15
25
  ::CarrierWave::Workers::ProcessAsset.class_eval do
16
26
  include ::Sidekiq::Worker
@@ -18,17 +28,8 @@ module CarrierWave
18
28
  ::CarrierWave::Workers::StoreAsset.class_eval do
19
29
  include ::Sidekiq::Worker
20
30
  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
31
  end
30
32
  end
31
-
32
33
  end
33
34
  end
34
35
 
@@ -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,9 +1,4 @@
1
1
  CarrierWave::Backgrounder.configure do |c|
2
- c.backend :delayed_job, queue: :carrierwave
3
- # c.backend :resque, queue: :carrierwave
2
+ c.backend :active_job, queue: :carrierwave
4
3
  # c.backend :sidekiq, queue: :carrierwave
5
- # c.backend :girl_friday, queue: :carrierwave
6
- # c.backend :sucker_punch, queue: :carrierwave
7
- # c.backend :qu, queue: :carrierwave
8
- # c.backend :qc
9
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
@@ -31,80 +30,13 @@ module CarrierWave::Backgrounder
31
30
  describe '#enqueue_for_backend' do
32
31
  let!(:worker) { MockWorker.new('FakeClass', 1, :image) }
33
32
 
34
- context 'delayed_job' do
35
- before do
36
- @mock_worker = Class.new do
37
- def self.perform(*args); new(*args).perform; end
38
- end
39
-
40
- allow(MockWorker).to receive(:new).and_return(worker)
41
- end
42
-
43
- context 'queue column exists' do
44
- it 'does not pass the queue name if none passed to #backend' do
45
- mock_module.backend :delayed_job
46
- expect(Delayed::Job).to receive(:enqueue).with(worker, {})
47
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
48
- end
49
-
50
- it 'sets the queue name to the queue name passed to #backend' do
51
- mock_module.backend :delayed_job, :queue => :awesome_queue
52
- expect(Delayed::Job).to receive(:enqueue).with(worker, :queue => :awesome_queue)
53
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
54
- end
55
- end
56
-
57
- context 'priority set in config' do
58
- it 'sets the priority which is passed to #backend' do
59
- mock_module.backend :delayed_job, :priority => 5
60
- expect(Delayed::Job).to receive(:enqueue).with(worker, :priority => 5)
61
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
62
- end
63
- end
64
-
65
- context 'queue column does not exist' do
66
- before do
67
- column_names = Delayed::Job.column_names.tap { |cn| cn.delete('queue') }
68
- allow(Delayed::Job).to receive(:column_names).and_return(column_names)
69
- Delayed::Job.class_eval { remove_method(:queue) }
70
- end
71
-
72
- after do
73
- Delayed::Job.class_eval { define_method(:queue) { nil } }
74
- end
75
-
76
- it 'does not pass a queue name if none passed to #backend' do
77
- mock_module.backend :delayed_job
78
- expect(Delayed::Job).to receive(:enqueue).with(worker, {})
79
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
80
- end
81
-
82
- it 'does not pass a queue name and logs a warning message if a queue name is passed to #backend' do
83
- mock_module.backend :delayed_job, :queue => :awesome_queue
84
- expect(Rails.logger).to receive(:warn).with(instance_of(String))
85
- expect(Delayed::Job).to receive(:enqueue).with(worker, {})
86
- mock_module.enqueue_for_backend MockWorker, 'FakeClass', 1, :image
87
- end
88
- end
89
- end
90
-
91
- context 'resque' do
92
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
93
-
94
- before do
95
- allow(Resque).to receive(:enqueue).with(*args)
96
- end
97
-
98
- it 'sets a variable with the queue name, defaults to :carrierwave' do
99
- mock_module.backend :resque
100
- mock_module.enqueue_for_backend(*args)
101
- expect(MockWorker.instance_variable_get '@queue').to eql(:carrierwave)
102
- end
33
+ context 'active_job' do
34
+ let(:args) { ['FakeClass', 1, :image] }
103
35
 
104
- it 'sets a variable to the queue name passed to #backend' do
105
- mock_module.backend :resque, :queue => :awesome_queue
106
- mock_module.enqueue_for_backend(*args)
107
- expect(MockWorker.instance_variable_get '@queue').to eql(:awesome_queue)
36
+ it 'invokes perform_later with string arguments' do
37
+ expect(MockWorker).to receive(:perform_later).with('FakeClass', '1', 'image')
38
+ mock_module.backend :active_job
39
+ mock_module.enqueue_for_backend(MockWorker, *args)
108
40
  end
109
41
  end
110
42
 
@@ -112,7 +44,7 @@ module CarrierWave::Backgrounder
112
44
  let(:args) { ['FakeClass', 1, :image] }
113
45
 
114
46
  it 'invokes client_push on the class with passed args' do
115
- expect(MockSidekiqWorker).to receive(:client_push).with({ 'class' => MockSidekiqWorker, 'args' => args })
47
+ expect(MockSidekiqWorker).to receive(:client_push).with({ 'class' => MockSidekiqWorker, 'args' => args.map(&:to_s) })
116
48
  mock_module.backend :sidekiq
117
49
  mock_module.enqueue_for_backend(MockSidekiqWorker, *args)
118
50
  end
@@ -122,7 +54,7 @@ module CarrierWave::Backgrounder
122
54
  'retry' => false,
123
55
  'timeout' => 60,
124
56
  'queue' => :awesome_queue,
125
- 'args' => args })
57
+ 'args' => args.map(&:to_s) })
126
58
  options = {:retry => false, :timeout => 60, :queue => :awesome_queue}
127
59
  mock_module.backend :sidekiq, options
128
60
  mock_module.enqueue_for_backend(MockSidekiqWorker, *args)
@@ -132,93 +64,12 @@ module CarrierWave::Backgrounder
132
64
  expect(MockNamedSidekiqWorker).to receive(:client_push).with({ 'class' => MockNamedSidekiqWorker,
133
65
  'retry' => false,
134
66
  'timeout' => 60,
135
- 'args' => args })
67
+ 'args' => args.map(&:to_s) })
136
68
  options = {:retry => false, :timeout => 60}
137
69
  mock_module.backend :sidekiq, options
138
70
  mock_module.enqueue_for_backend(MockNamedSidekiqWorker, *args)
139
71
  end
140
72
  end
141
-
142
- context 'girl_friday' do
143
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
144
-
145
- it 'instantiates a GirlFriday work queue if one does not exist' do
146
- mock_module.backend :girl_friday
147
- expect(GirlFriday::WorkQueue).to receive(:new).with(:carrierwave, {}).and_return([])
148
- mock_module.enqueue_for_backend(*args)
149
- end
150
-
151
- it 'instantiates a GirlFriday work queue passing the args to the queue' do
152
- mock_module.backend :girl_friday, :queue => :awesome_queue, :size => 3
153
- expect(GirlFriday::WorkQueue).to receive(:new).with(:awesome_queue, {:size => 3}).and_return([])
154
- mock_module.enqueue_for_backend(*args)
155
- end
156
-
157
- it 'does not instantiate a GirlFriday work queue if one exists' do
158
- mock_module.backend :girl_friday
159
- mock_module.instance_variable_set('@girl_friday_queue', [])
160
- expect(GirlFriday::WorkQueue).to receive(:new).never
161
- mock_module.enqueue_for_backend(*args)
162
- end
163
-
164
- it 'add a worker to the girl_friday queue' do
165
- expected = [{ :worker => MockWorker.new('FakeClass', 1, :image) }]
166
- mock_module.backend :girl_friday
167
- mock_module.instance_variable_set('@girl_friday_queue', [])
168
- mock_module.enqueue_for_backend(*args)
169
- expect(mock_module.instance_variable_get '@girl_friday_queue').to eql(expected)
170
- end
171
- end
172
-
173
- context 'sucker_punch' do
174
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
175
- let(:job) { double('job') }
176
-
177
- it 'invokes a new worker' do
178
- expect(MockWorker).to receive(:new).and_return(worker)
179
- expect(worker).to receive(:async).and_return(job)
180
- expect(job).to receive(:perform).with('FakeClass', 1, :image)
181
- mock_module.backend :sucker_punch
182
- mock_module.enqueue_for_backend(*args)
183
- end
184
- end
185
-
186
- context 'qu' do
187
- let(:args) { [MockWorker, 'FakeClass', 1, :image] }
188
- before do
189
- allow(Qu).to receive(:enqueue).with(*args)
190
- end
191
-
192
- it 'sets a variable with the queue name, defaults to :carrierwave' do
193
- mock_module.backend :qu
194
- mock_module.enqueue_for_backend(*args)
195
- expect(MockWorker.instance_variable_get '@queue').to eql(:carrierwave)
196
- end
197
-
198
- it 'sets a variable to the queue name passed to #backend' do
199
- mock_module.backend :qu, :queue => :awesome_queue
200
- mock_module.enqueue_for_backend(*args)
201
- expect(MockWorker.instance_variable_get '@queue').to eql(:awesome_queue)
202
- end
203
- end
204
-
205
- context 'qc' do
206
- it 'calls enqueue with the passed args' do
207
- expect(QC).to receive(:enqueue).with("MockWorker.perform", 'FakeClass', 1, 'image')
208
- mock_module.backend :qc
209
- mock_module.enqueue_for_backend(MockWorker, 'FakeClass', 1, :image)
210
- end
211
- end
212
-
213
- context 'immediate' do
214
- it 'instantiates a worker passing the args and calls perform' do
215
- worker = double('Worker')
216
- expect(MockWorker).to receive(:new).with('FakeClass', 1, :image).and_return(worker)
217
- expect(worker).to receive(:perform)
218
- mock_module.backend :immediate
219
- mock_module.enqueue_for_backend(MockWorker, 'FakeClass', 1, :image)
220
- end
221
- end
222
73
  end
223
74
  end
224
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