@@ -176,6 +176,19 @@ def changes_to_save
176
176
end
177
177
end
178
178
179
+ class ::DummyIndexingModelWithNoChanges
180
+ extend Elasticsearch ::Model ::Indexing ::ClassMethods
181
+ include Elasticsearch ::Model ::Indexing ::InstanceMethods
182
+
183
+ def self . before_save ( &block )
184
+ ( @callbacks ||= { } ) [ block . hash ] = block
185
+ end
186
+
187
+ def changes_to_save
188
+ { }
189
+ end
190
+ end
191
+
179
192
class ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson
180
193
extend Elasticsearch ::Model ::Indexing ::ClassMethods
181
194
include Elasticsearch ::Model ::Indexing ::InstanceMethods
@@ -393,6 +406,26 @@ def changes
393
406
instance . update_document
394
407
end
395
408
409
+ should "index instead of update when nothing was changed" do
410
+ client = mock ( 'client' )
411
+ instance = ::DummyIndexingModelWithNoChanges . new
412
+
413
+ # Set the fake `changes` hash
414
+ instance . instance_variable_set ( :@__changed_model_attributes , { } )
415
+ # Overload as_indexed_json for running index
416
+ instance . expects ( :as_indexed_json ) . returns ( { 'foo' => 'BAR' } )
417
+
418
+ client . expects ( :index )
419
+ client . expects ( :update ) . never
420
+
421
+ instance . expects ( :client ) . returns ( client )
422
+ instance . expects ( :index_name ) . returns ( 'foo' )
423
+ instance . expects ( :document_type ) . returns ( 'bar' )
424
+ instance . expects ( :id ) . returns ( '1' )
425
+
426
+ instance . update_document ( { } )
427
+ end
428
+
396
429
should "update only the specific attributes" do
397
430
client = mock ( 'client' )
398
431
instance = ::DummyIndexingModelWithCallbacks . new
0 commit comments