File tree Expand file tree Collapse file tree 3 files changed +27
-4
lines changed Expand file tree Collapse file tree 3 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -83,7 +83,11 @@ module Importing
83
83
# @see http://api.rubyonrails.org/classes/ActiveRecord/Batches.html ActiveRecord::Batches.find_in_batches
84
84
#
85
85
def __find_in_batches ( options = { } , &block )
86
- find_in_batches ( options ) do |batch |
86
+ named_scope = options . delete ( :scope )
87
+
88
+ scope = named_scope ? self . send ( named_scope ) : self
89
+
90
+ scope . find_in_batches ( options ) do |batch |
87
91
batch_for_bulk = batch . map { |a | { index : { _id : a . id , data : a . __elasticsearch__ . as_indexed_json } } }
88
92
yield batch_for_bulk
89
93
end
Original file line number Diff line number Diff line change @@ -64,6 +64,10 @@ module ClassMethods
64
64
#
65
65
# Article.import index: 'my-new-index', type: 'my-other-type'
66
66
#
67
+ # @example Pass an ActiveRecord scope to limit the imported records
68
+ #
69
+ # Article.import scope: 'published'
70
+ #
67
71
def import ( options = { } , &block )
68
72
errors = 0
69
73
refresh = options . delete ( :refresh ) || false
Original file line number Diff line number Diff line change @@ -82,13 +82,28 @@ def ids
82
82
end
83
83
84
84
context "Importing" do
85
+ setup do
86
+ DummyClassForActiveRecord . __send__ :extend , Elasticsearch ::Model ::Adapter ::ActiveRecord ::Importing
87
+ end
88
+
89
+ should "raise an exception when passing an invalid scope" do
90
+ assert_raise NoMethodError do
91
+ DummyClassForActiveRecord . __find_in_batches ( scope : :not_found_method ) do ; end
92
+ end
93
+ end
94
+
85
95
should "implement the __find_in_batches method" do
86
96
DummyClassForActiveRecord . expects ( :find_in_batches ) . returns ( [ ] )
87
-
88
- DummyClassForActiveRecord . __send__ :extend , Elasticsearch ::Model ::Adapter ::ActiveRecord ::Importing
89
97
DummyClassForActiveRecord . __find_in_batches do ; end
90
98
end
91
- end
92
99
100
+ should "limit the relation to a specific scope" do
101
+ DummyClassForActiveRecord . expects ( :find_in_batches ) . returns ( [ ] )
102
+ DummyClassForActiveRecord . expects ( :published ) . returns ( DummyClassForActiveRecord )
103
+
104
+ DummyClassForActiveRecord . __find_in_batches ( scope : :published ) do ; end
105
+ end
106
+
107
+ end
93
108
end
94
109
end
You can’t perform that action at this time.
0 commit comments