Skip to content

Commit f25c455

Browse files
committed
[MODEL] Small tweaks to the update_document_attributes method implementation
Related: elastic#230
1 parent b72a07d commit f25c455

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ def delete_document(options={})
317317
#
318318
# When the changed attributes are not available, performs full re-index of the record.
319319
#
320+
# See the {#update_document_attributes} method for updating specific attributes directly.
321+
#
320322
# @param options [Hash] Optional arguments for passing to the client
321323
#
322324
# @example Update a document corresponding to the record
@@ -352,21 +354,21 @@ def update_document(options={})
352354
end
353355
end
354356

355-
# Allows for partial update of a document by manually supplying the changes
357+
# Perform a _partial_ update of specific document attributes
358+
# (without consideration for changed attributes as in {#update_document})
356359
#
357-
# @param attributes [Hash] Required attributes to be updated
358-
# @param options [Hash] Optional arguments for passing to the client
360+
# @param attributes [Hash] Attributes to be updated
361+
# @param options [Hash] Optional arguments for passing to the client
359362
#
360-
# @example update just the title
363+
# @example Update the `title` attribute
361364
#
362365
# @article = Article.first
363366
# @article.title = "New title"
364-
# @article.__elasticsearch__.update_document_attributes({title: "New title"})
367+
# @article.__elasticsearch__.update_document_attributes title: "New title"
365368
#
366369
# @return [Hash] The response from Elasticsearch
367370
#
368-
#
369-
def update_document_attributes(attributes,options={})
371+
def update_document_attributes(attributes, options={})
370372
client.update(
371373
{ index: index_name,
372374
type: document_type,

elasticsearch-model/test/integration/active_record_basic_test.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,19 @@ class ::Article < ActiveRecord::Base
128128
assert_equal 1, response.records.size
129129
end
130130

131-
should "allow specific updates to be made to the document directly" do
131+
should "update specific attributes" do
132132
article = Article.first
133133

134-
article.update_document_attributes({title: 'green grass'})
134+
response = Article.search 'title:special'
135+
136+
assert_equal 0, response.results.size
137+
assert_equal 0, response.records.size
138+
139+
article.__elasticsearch__.update_document_attributes title: 'special'
135140

136141
Article.__elasticsearch__.refresh_index!
137142

138-
response = Article.search 'title:green'
143+
response = Article.search 'title:special'
139144

140145
assert_equal 1, response.results.size
141146
assert_equal 1, response.records.size

elasticsearch-model/test/unit/indexing_test.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -324,44 +324,46 @@ def as_indexed_json(options={})
324324
instance.update_document
325325
end
326326

327-
should "have the update_document_attributes method" do
327+
should "update only the specific attributes" do
328328
client = mock('client')
329329
instance = ::DummyIndexingModelWithCallbacks.new
330330

331+
# Set the fake `changes` hash
332+
instance.instance_variable_set(:@__changed_attributes, {author: 'john'})
333+
331334
client.expects(:update).with do |payload|
332335
assert_equal 'foo', payload[:index]
333336
assert_equal 'bar', payload[:type]
334337
assert_equal '1', payload[:id]
338+
assert_equal({title: 'green'}, payload[:body][:doc])
335339
end
336340

337341
instance.expects(:client).returns(client)
338342
instance.expects(:index_name).returns('foo')
339343
instance.expects(:document_type).returns('bar')
340344
instance.expects(:id).returns('1')
341345

342-
instance.update_document_attributes({})
346+
instance.update_document_attributes title: "green"
343347
end
344348

345-
should "update only the supplied attributes" do
349+
should "pass options to the update_document_attributes method" do
346350
client = mock('client')
347351
instance = ::DummyIndexingModelWithCallbacks.new
348352

349-
# Set the fake `changes` hash
350-
instance.instance_variable_set(:@__changed_attributes, {foo: 'bar'})
351-
352353
client.expects(:update).with do |payload|
353354
assert_equal 'foo', payload[:index]
354355
assert_equal 'bar', payload[:type]
355356
assert_equal '1', payload[:id]
356357
assert_equal({title: 'green'}, payload[:body][:doc])
358+
assert_equal true, payload[:refresh]
357359
end
358360

359361
instance.expects(:client).returns(client)
360362
instance.expects(:index_name).returns('foo')
361363
instance.expects(:document_type).returns('bar')
362364
instance.expects(:id).returns('1')
363365

364-
instance.update_document_attributes({title: "green"})
366+
instance.update_document_attributes( { title: "green" }, { refresh: true } )
365367
end
366368
end
367369

0 commit comments

Comments
 (0)