sidekiq-activerecord 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4546acc561ce4ce6579666b4b05a66006ec39d9
4
- data.tar.gz: 73e5e411912bca36600f5bf091fb010767119f00
3
+ metadata.gz: d5ab93d1d218ef784216eb2a2769fae0fb837eeb
4
+ data.tar.gz: 9a346662abbacb3842fc32bfd8077e9e904cc9e7
5
5
  SHA512:
6
- metadata.gz: 716f41cd85a9809af9965675eca4220b7d35f5607cb42abfea2fe5c1251fb7d2a8f466219db43bcaefb19e9ab77b604a0926a11482039bb0f89a4333c4fc19af
7
- data.tar.gz: b5ec0c789fa00a57ee2464f72a88c40b930dd1683c858d914d8a1330059bf13338abd002d15537ea8419c089e37b76e719495ef34aaa4b1c6490e7b77a8fe018
6
+ metadata.gz: 68268f2371d13e508b4b21a65c090edc75d9575ee0ed1caf91cf339dd5359f0ef75a67bad6430c0dab3e8709f53b8dd0b42524a9686d3c2af2be5813d40f6e63
7
+ data.tar.gz: 0ff832cd5fa773e2de3cb682a4b6223c8557810015f358fb565e1cec06123eb664d2844c64ec3b7089d7bd3e1276f7a4600db0a6dd98d2925745c62551387da8
data/README.md CHANGED
@@ -11,7 +11,7 @@ If you've been using Sidekiq for a while, you've probably noticed a recurring pa
11
11
  ## [Sidekiq::ActiveRecord::TaskWorker](https://github.com/sidekiq-orm/sidekiq-activerecord/wiki/Task-Worker)
12
12
  A very conventional pattern, is to have a worker that gets a model identifier, loads it and runs some custom logic on the model.
13
13
 
14
- [```TaskWorker```]https://github.com/sidekiq-orm/sidekiq-activerecord/wiki/Task-Worker) provides a simple and clean interface, which reduces the boilerplate and exposes only the custom logic.
14
+ [```TaskWorker```](https://github.com/sidekiq-orm/sidekiq-activerecord/wiki/Task-Worker) provides a simple and clean interface, which reduces the boilerplate and exposes only the custom logic.
15
15
 
16
16
  Here's a simple example:
17
17
 
@@ -22,7 +22,9 @@ module Sidekiq
22
22
  # :worker_class - the worker class to delegate the task to. Alternative to the default `sidekiq_delegate_task_to`
23
23
  # :identifier_key - the model identifier column. Default 'id'
24
24
  # :additional_keys - additional model keys
25
- # :batch_size - Specifies the size of the batch. Default to 1000.
25
+ # :batch_size - Specifies the size of each batch to push in bulk.
26
+ # This is also the number of models to fetch in each find_in_batches query.
27
+ # Default is 1000.
26
28
  #
27
29
  # @example:
28
30
  # class UserTaskWorker
@@ -5,6 +5,30 @@ module Sidekiq
5
5
 
6
6
  attr_reader :task_model
7
7
 
8
+ # Helper method, to automatically call the task worker with the identifier.
9
+ # This will allow you to change the :identifier_key option, without needing to change it in other places.
10
+ #
11
+ # @example:
12
+ # class UserMailerTaskWorker < Sidekiq::ActiveRecord::TaskWorker
13
+ #
14
+ # sidekiq_task_model User
15
+ # sidekiq_task_options :identifier_key => :email
16
+ #
17
+ # end
18
+ #
19
+ # user = User.find_by(:email => user@mail.com)
20
+ #
21
+ # UserMailerTaskWorker.perform_async(user.email, arg1, arg2)
22
+ #
23
+ # # is the same as doing
24
+ # UserMailerTaskWorker.perform_async_on(user, arg1, arg2)
25
+ #
26
+ def self.perform_async_on(model, *args)
27
+ fail ArgumentError.new "Specified model must be a #{model_class.to_s}" unless model.class <= model_class
28
+ identifier = model.send(self.identifier_key)
29
+ perform_async(identifier, *args)
30
+ end
31
+
8
32
  # @example:
9
33
  # class UserMailerTaskWorker < Sidekiq::ActiveRecord::TaskWorker
10
34
  #
@@ -30,7 +54,7 @@ module Sidekiq
30
54
  # end
31
55
  #
32
56
  #
33
- # UserMailerTaskWorker.perform(user.id, :new_email)
57
+ # UserMailerTaskWorker.perform_async(user.id, :new_email)
34
58
  #
35
59
  def perform(identifier, *args)
36
60
  @task_model = fetch_model(identifier, *args)
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module ActiveRecord
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency 'activerecord', '>= 4.0'
23
23
 
24
24
  spec.add_development_dependency 'rake'
25
- spec.add_development_dependency 'rspec', '3.0.0.rc1'
25
+ spec.add_development_dependency 'rspec', '3.0.0'
26
26
  spec.add_development_dependency 'database_cleaner', '>= 1.2.0'
27
27
  spec.add_development_dependency 'factory_girl', '~> 4.0'
28
28
  end
@@ -255,4 +255,57 @@ describe Sidekiq::ActiveRecord::TaskWorker do
255
255
  end
256
256
  end
257
257
  end
258
+
259
+ describe 'perform_async_on', :focus do
260
+
261
+ before do
262
+ class UserTaskWorker
263
+ sidekiq_task_model User
264
+ end
265
+ end
266
+
267
+ let(:custom_arg1) { 'arg1' }
268
+ let(:custom_arg2) { 'arg2' }
269
+
270
+ before do
271
+ allow(task_worker_class).to receive(:perform_async)
272
+ end
273
+
274
+ context "when the specified model doesn't match the task_model class" do
275
+
276
+ let(:unrelated_model) {
277
+ Struct.new(:id, :name).new(55, 'Mike')
278
+ }
279
+
280
+ it "raises an ArgumentError and doesn't call perform_async" do
281
+ expect{
282
+ task_worker_class.perform_async_on(unrelated_model)
283
+ }.to raise_error(ArgumentError)
284
+ expect(task_worker_class).to_not have_received(:perform_async)
285
+ end
286
+ end
287
+
288
+ context 'when the identifier_key is undefined' do
289
+ it 'calls perform_async with the task_model.id and optionals arguments' do
290
+ task_worker_class.perform_async_on(user, custom_arg1, custom_arg2)
291
+ expect(task_worker_class).to have_received(:perform_async).with(user.id, custom_arg1, custom_arg2)
292
+ end
293
+ end
294
+
295
+ context 'when the identifier_key is defined' do
296
+
297
+ before do
298
+ class UserTaskWorker
299
+ sidekiq_task_options :identifier_key => :email
300
+ end
301
+ end
302
+
303
+ it 'calls perform_async with the task_model identifier and optionals arguments' do
304
+ task_worker_class.perform_async_on(user, custom_arg1, custom_arg2)
305
+ expect(task_worker_class).to have_received(:perform_async).with(user.email, custom_arg1, custom_arg2)
306
+ end
307
+ end
308
+
309
+ end
310
+
258
311
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Farhi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-05 00:00:00.000000000 Z
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 3.0.0.rc1
61
+ version: 3.0.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 3.0.0.rc1
68
+ version: 3.0.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: database_cleaner
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -152,3 +152,4 @@ test_files:
152
152
  - spec/support/database_cleaner.rb
153
153
  - spec/support/factory_girl.rb
154
154
  - spec/support/models.rb
155
+ has_rdoc: