Skip to content

Commit 022a071

Browse files
committed
[MODEL] Support scope, query and preprocess importing options in Mongoid Adapter
1 parent 3b6f27d commit 022a071

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

elasticsearch-model/lib/elasticsearch/model/adapters/mongoid.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ module Importing
6464
#
6565
def __find_in_batches(options={}, &block)
6666
options[:batch_size] ||= 1_000
67+
query = options.delete(:query)
68+
named_scope = options.delete(:scope)
69+
preprocess = options.delete(:preprocess)
70+
71+
scope = all
72+
scope = scope.send(named_scope) if named_scope
73+
scope = query.is_a?(Proc) ? scope.class_exec(&query) : scope.where(query) if query
6774

68-
all.no_timeout.each_slice(options[:batch_size]) do |items|
69-
yield items
75+
scope.no_timeout.each_slice(options[:batch_size]) do |items|
76+
yield (preprocess ? self.__send__(preprocess, items) : items)
7077
end
7178
end
7279

elasticsearch-model/test/unit/adapter_mongoid_test.rb

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,64 @@ def ids
9898
assert_equal @transform.call(model), { index: { _id: "1", data: {} } }
9999
end
100100
end
101-
end
102101

102+
should "limit the relation to a specific scope" do
103+
relation = mock()
104+
relation.stubs(:no_timeout).returns(relation)
105+
relation.expects(:published).returns(relation)
106+
relation.expects(:each_slice).returns([])
107+
DummyClassForMongoid.expects(:all).returns(relation)
108+
109+
DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
110+
DummyClassForMongoid.__find_in_batches(scope: :published) do; end
111+
end
112+
113+
context "when limit the relation with proc" do
114+
setup do
115+
@query = Proc.new { where(color: "red") }
116+
end
117+
should "query with a specific criteria" do
118+
relation = mock()
119+
relation.stubs(:no_timeout).returns(relation)
120+
relation.expects(:class_exec).returns(relation)
121+
relation.expects(:each_slice).returns([])
122+
DummyClassForMongoid.expects(:all).returns(relation)
123+
124+
DummyClassForMongoid.__find_in_batches(query: @query) do; end
125+
end
126+
end
127+
128+
context "when limit the relation with hash" do
129+
setup do
130+
@query = { color: "red" }
131+
end
132+
should "query with a specific criteria" do
133+
relation = mock()
134+
relation.stubs(:no_timeout).returns(relation)
135+
relation.expects(:class_exec).returns(relation)
136+
relation.expects(:each_slice).returns([])
137+
DummyClassForMongoid.expects(:all).returns(relation)
138+
139+
DummyClassForMongoid.__find_in_batches(query: @query) do; end
140+
end
141+
end
142+
143+
should "preprocess the batch if option provided" do
144+
class << DummyClassForMongoid
145+
# Updates/transforms the batch while fetching it from the database
146+
# (eg. with information from an external system)
147+
#
148+
def update_batch(batch)
149+
batch.collect { |b| b.to_s + '!' }
150+
end
151+
end
152+
153+
DummyClassForMongoid.expects(:__find_in_batches).returns( [:a, :b] )
154+
155+
DummyClassForMongoid.__find_in_batches(preprocess: :update_batch) do |batch|
156+
assert_same_elements ["a!", "b!"], batch
157+
end
158+
end
159+
end
103160
end
104161
end

0 commit comments

Comments
 (0)