@@ -168,6 +168,72 @@ def as_indexed_json(options={})
168
168
assert response . results . any? , "Search has not returned results: #{ response . to_a } "
169
169
end
170
170
end
171
+
172
+ context "importing when the model has a default scope" do
173
+ class ::MongoidArticleWithDefaultScope
174
+ include Mongoid ::Document
175
+ include Elasticsearch ::Model
176
+ include Elasticsearch ::Model ::Callbacks
177
+
178
+ default_scope -> { where ( status : 'active' ) }
179
+
180
+ field :id , type : String
181
+ field :title , type : String
182
+ field :status , type : String , default : 'active'
183
+ validates :status , inclusion : { in : %w( active removed ) }
184
+
185
+ attr_accessible :title if respond_to? :attr_accessible
186
+
187
+ settings index : { number_of_shards : 1 , number_of_replicas : 0 } do
188
+ mapping do
189
+ indexes :title , type : 'text' , analyzer : 'snowball'
190
+ indexes :created_at , type : 'date'
191
+ end
192
+ end
193
+
194
+ def as_indexed_json ( options = { } )
195
+ as_json ( except : [ :id , :_id ] )
196
+ end
197
+ end
198
+
199
+ setup do
200
+ Elasticsearch ::Model ::Adapter . register \
201
+ Elasticsearch ::Model ::Adapter ::Mongoid ,
202
+ lambda { |klass | !!defined? ( ::Mongoid ::Document ) && klass . respond_to? ( :ancestors ) && klass . ancestors . include? ( ::Mongoid ::Document ) }
203
+
204
+ MongoidArticleWithDefaultScope . __elasticsearch__ . create_index! force : true
205
+
206
+ MongoidArticleWithDefaultScope . delete_all
207
+
208
+ MongoidArticleWithDefaultScope . create! title : 'Test'
209
+ MongoidArticleWithDefaultScope . create! title : 'Testing Coding'
210
+ MongoidArticleWithDefaultScope . create! title : 'Test legacy code' , status : 'removed'
211
+
212
+ MongoidArticleWithDefaultScope . __elasticsearch__ . refresh_index!
213
+ MongoidArticleWithDefaultScope . __elasticsearch__ . client . cluster . health wait_for_status : 'yellow'
214
+ end
215
+
216
+ should "import only documents from the default scope" do
217
+ assert_equal 3 , MongoidArticleWithDefaultScope . count
218
+
219
+ assert_equal 0 , MongoidArticleWithDefaultScope . import
220
+
221
+ MongoidArticleWithDefaultScope . __elasticsearch__ . refresh_index!
222
+ assert_equal 3 , MongoidArticleWithDefaultScope . search ( '*' ) . results . total
223
+ end
224
+
225
+ should "import only documents from a specific query combined with the default scope" do
226
+ assert_equal 3 , MongoidArticleWithDefaultScope . count
227
+
228
+ assert_equal 0 , MongoidArticleWithDefaultScope . import ( query : -> { where ( title : /^Test/ ) } )
229
+
230
+ MongoidArticleWithDefaultScope . __elasticsearch__ . refresh_index!
231
+
232
+ puts "====== wtfbbq??"
233
+ puts MongoidArticleWithDefaultScope . search ( '*' ) . results . inspect
234
+ assert_equal 2 , MongoidArticleWithDefaultScope . search ( '*' ) . results . total
235
+ end
236
+ end
171
237
end
172
238
173
239
end
0 commit comments