chewy_kiqqer 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -2
- data/lib/chewy_kiqqer.rb +1 -0
- data/lib/chewy_kiqqer/index.rb +27 -0
- data/lib/chewy_kiqqer/mixin.rb +16 -7
- data/lib/chewy_kiqqer/version.rb +1 -1
- data/spec/index_spec.rb +66 -0
- data/spec/mixin_spec.rb +30 -19
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56d8782dbf31ea522b3949701611734c73d7c719
|
4
|
+
data.tar.gz: ab2aca5b6fd3414b9bd28ecbd44c34d80364ae7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7495bbdf62b2a67264c7a6dd0af12bec1381264b0a41b6210f817da6e2ea72d995c4425d15229bf6baa5b0aca7332eefac6955ab16d2118b663d893c059da80
|
7
|
+
data.tar.gz: 20573650635ad47f0c1ed8b6031f8e82462673ad3a47e9abc97ca221711718743c0b569bdb11ae71da56ad3df15273d7226d9d446fc5949f447e2f5a567c8102
|
data/README.md
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
This is an alternative udpate/callback mechanism for [Chewy](https://github.com/toptal/chewy). It queues the updates as [Sidekiq](https://github.com/mperham/sidekiq) jobs.
|
4
4
|
|
5
|
-
|
5
|
+
You can pass backrefs like with the standard chewy mechanism, but the job itself will always receive an array of ids.
|
6
|
+
|
7
|
+
It is possible to install more multiple update hooks.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -25,7 +27,7 @@ Just add the module and set it up:
|
|
25
27
|
class User < ActiveRecord::Base
|
26
28
|
include ChewyKiqqer::Mixin
|
27
29
|
|
28
|
-
async_update_index index: 'users#user', queue: :other_than_default
|
30
|
+
async_update_index index: 'users#user', queue: :other_than_default, backref: :something
|
29
31
|
end
|
30
32
|
|
31
33
|
You can also include the mixin into ActiveRecord::Base in an initializer if it should be generally available.
|
@@ -33,6 +35,16 @@ The queue name is optional. You can also set a default queue name for your appli
|
|
33
35
|
|
34
36
|
ChewyKiqqer.default_queue = :my_queue
|
35
37
|
|
38
|
+
Giving a backref is also optional (also check the chewy docs for the concept). The backref is the element
|
39
|
+
which will be indexed. The default is to use the current record.
|
40
|
+
|
41
|
+
# use the current record for the backref
|
42
|
+
... backref: :self
|
43
|
+
# call a method on the current record to get the backref
|
44
|
+
... backref: :some_method
|
45
|
+
# Pass a proc. It will be called with the current record to get the backref
|
46
|
+
... backref: -> (rec) { |rec| rec.do_something }
|
47
|
+
|
36
48
|
## Contributing
|
37
49
|
|
38
50
|
1. Fork it
|
data/lib/chewy_kiqqer.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
module ChewyKiqqer
|
2
|
+
|
3
|
+
class Index
|
4
|
+
|
5
|
+
def initialize(backref: :self, index: nil, queue: ChewyKiqqer.default_queue)
|
6
|
+
@index_name = index
|
7
|
+
@backref_method = backref
|
8
|
+
@queue = queue
|
9
|
+
end
|
10
|
+
|
11
|
+
def enqueue(object)
|
12
|
+
Sidekiq::Client.push(queue: @queue, class: ChewyKiqqer::Worker, args: [@index_name, backref_ids(object)])
|
13
|
+
end
|
14
|
+
|
15
|
+
def backref(object)
|
16
|
+
return @backref_method.call(object) if @backref_method.respond_to?(:call)
|
17
|
+
return object if @backref_method.to_s == 'self'
|
18
|
+
object.send(@backref_method)
|
19
|
+
end
|
20
|
+
|
21
|
+
def backref_ids(object)
|
22
|
+
Array.wrap(backref(object)).map { |object| object.respond_to?(:id) ? object.id : object.to_i }
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/chewy_kiqqer/mixin.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/array/wrap'
|
2
3
|
|
3
4
|
module ChewyKiqqer
|
4
5
|
|
@@ -16,19 +17,27 @@ module ChewyKiqqer
|
|
16
17
|
|
17
18
|
module ClassMethods
|
18
19
|
|
19
|
-
attr_reader :
|
20
|
+
attr_reader :indexers
|
20
21
|
|
21
|
-
def async_update_index(index: nil, queue: ChewyKiqqer::default_queue)
|
22
|
-
@
|
22
|
+
def async_update_index(index: nil, queue: ChewyKiqqer::default_queue, backref: :self)
|
23
|
+
@indexers ||= []
|
24
|
+
install_chewy_hooks if @indexers.empty?
|
25
|
+
@indexers << ChewyKiqqer::Index.new(index: index, queue: queue, backref: backref)
|
26
|
+
end
|
27
|
+
|
28
|
+
def reset_async_chewy
|
29
|
+
@indexers and @indexers.clear
|
30
|
+
end
|
31
|
+
|
32
|
+
def install_chewy_hooks
|
23
33
|
after_save :queue_chewy_job
|
24
34
|
after_destroy :queue_chewy_job
|
25
|
-
ChewyKiqqer::Worker.sidekiq_options queue: queue
|
26
35
|
end
|
27
|
-
|
28
36
|
end
|
29
37
|
|
30
|
-
def
|
31
|
-
|
38
|
+
def queue_chewy_jobs
|
39
|
+
self.class.indexers or return
|
40
|
+
self.class.indexers.each { |idx| idx.enqueue(self) }
|
32
41
|
end
|
33
42
|
end
|
34
43
|
end
|
data/lib/chewy_kiqqer/version.rb
CHANGED
data/spec/index_spec.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'chewy_kiqqer'
|
2
|
+
|
3
|
+
describe ChewyKiqqer::Index do
|
4
|
+
|
5
|
+
context 'compute the backref' do
|
6
|
+
|
7
|
+
it 'defaults to the corresponding object' do
|
8
|
+
object = Object.new
|
9
|
+
idx = ChewyKiqqer::Index.new
|
10
|
+
idx.backref(object).should eq object
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'can run a random method' do
|
14
|
+
object = double(foobar: :backref)
|
15
|
+
idx = ChewyKiqqer::Index.new(backref: :foobar)
|
16
|
+
idx.backref(object).should eq :backref
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'can use a proc' do
|
20
|
+
object = double(foobar: :my_backref)
|
21
|
+
idx = ChewyKiqqer::Index.new(backref: -> (r) { r.foobar })
|
22
|
+
idx.backref(object).should eq :my_backref
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'turning backrefs into ids' do
|
26
|
+
let(:idx) { ChewyKiqqer::Index.new }
|
27
|
+
|
28
|
+
it 'uses the ids of the objects' do
|
29
|
+
idx.backref_ids([double(id: 3), double(id: 6)]).should eq [3, 6]
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'turns everything else into ints' do
|
33
|
+
idx.backref_ids([3, '6']).should eq [3, 6]
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'queue the job' do
|
39
|
+
it 'queues the job with the settings from the index' do
|
40
|
+
object = double(id: 24)
|
41
|
+
idx = ChewyKiqqer::Index.new index: 'foo#bar',
|
42
|
+
queue: 'hello'
|
43
|
+
|
44
|
+
Sidekiq::Client.should_receive(:push)
|
45
|
+
.with(queue: 'hello',
|
46
|
+
class: ChewyKiqqer::Worker,
|
47
|
+
args: ['foo#bar', [24]])
|
48
|
+
idx.enqueue object
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'queues the job on the chewy default queue if not told otherwise' do
|
52
|
+
object = double(id: 24)
|
53
|
+
ChewyKiqqer.default_queue = :my_default
|
54
|
+
idx = ChewyKiqqer::Index.new index: 'foo#bar'
|
55
|
+
|
56
|
+
Sidekiq::Client.should_receive(:push)
|
57
|
+
.with(queue: :my_default,
|
58
|
+
class: ChewyKiqqer::Worker,
|
59
|
+
args: ['foo#bar', [24]])
|
60
|
+
idx.enqueue object
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/spec/mixin_spec.rb
CHANGED
@@ -5,44 +5,55 @@ class Gummi
|
|
5
5
|
|
6
6
|
def self.after_save(*args) ; end
|
7
7
|
def self.after_destroy(*args) ; end
|
8
|
-
def id ; 17 ; end
|
9
8
|
|
10
9
|
end
|
11
10
|
|
12
11
|
describe ChewyKiqqer::Mixin do
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
before(:each) { Gummi.reset_async_chewy }
|
14
|
+
|
15
|
+
context '#update_index' do
|
16
|
+
|
17
|
+
it 'installs the hooks if nothing was installed yet' do
|
18
|
+
Gummi.should_receive(:install_chewy_hooks)
|
18
19
|
Gummi.async_update_index(index: 'xxx')
|
19
20
|
end
|
20
21
|
|
21
|
-
it '
|
22
|
-
Gummi.
|
23
|
-
Gummi.
|
22
|
+
it 'does not install the hooks twice' do
|
23
|
+
Gummi.should_receive(:install_chewy_hooks).once
|
24
|
+
Gummi.async_update_index(index: 'xxx')
|
25
|
+
Gummi.async_update_index(index: 'xxx')
|
24
26
|
end
|
25
27
|
|
26
|
-
it '
|
27
|
-
ChewyKiqqer::
|
28
|
-
|
28
|
+
it 'installs the indexer' do
|
29
|
+
ChewyKiqqer::Index.should_receive(:new)
|
30
|
+
.with(index: 'some#thing', queue: :medium, backref: :foobar)
|
31
|
+
.and_return(:idx)
|
32
|
+
Gummi.async_update_index(index: 'some#thing', queue: :medium, backref: :foobar)
|
33
|
+
Gummi.indexers.should eq [:idx]
|
29
34
|
end
|
30
35
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
36
|
+
end
|
37
|
+
|
38
|
+
context '#install hooks' do
|
39
|
+
|
40
|
+
it 'installs the hooks' do
|
41
|
+
Gummi.should_receive(:after_save).with(:queue_chewy_job)
|
42
|
+
Gummi.should_receive(:after_destroy).with(:queue_chewy_job)
|
43
|
+
Gummi.install_chewy_hooks
|
35
44
|
end
|
45
|
+
|
36
46
|
end
|
37
47
|
|
38
|
-
context '#
|
48
|
+
context '#queue_chewy_jobs' do
|
39
49
|
|
40
50
|
let(:record) { Gummi.new }
|
41
51
|
|
42
52
|
it 'queues the job' do
|
43
|
-
|
44
|
-
|
45
|
-
|
53
|
+
idx = double
|
54
|
+
idx.should_receive(:enqueue).with(record)
|
55
|
+
Gummi.stub(indexers: [idx])
|
56
|
+
record.queue_chewy_jobs
|
46
57
|
end
|
47
58
|
|
48
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chewy_kiqqer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Hahn
|
@@ -108,9 +108,11 @@ files:
|
|
108
108
|
- Rakefile
|
109
109
|
- chewy_kiqqer.gemspec
|
110
110
|
- lib/chewy_kiqqer.rb
|
111
|
+
- lib/chewy_kiqqer/index.rb
|
111
112
|
- lib/chewy_kiqqer/mixin.rb
|
112
113
|
- lib/chewy_kiqqer/version.rb
|
113
114
|
- lib/chewy_kiqqer/worker.rb
|
115
|
+
- spec/index_spec.rb
|
114
116
|
- spec/mixin_spec.rb
|
115
117
|
- spec/worker.spec.rb
|
116
118
|
homepage: ''
|
@@ -139,5 +141,6 @@ specification_version: 4
|
|
139
141
|
summary: Small helper gem that allows you to automatically run all chewy index updates
|
140
142
|
in sidekiq
|
141
143
|
test_files:
|
144
|
+
- spec/index_spec.rb
|
142
145
|
- spec/mixin_spec.rb
|
143
146
|
- spec/worker.spec.rb
|