Skip to content

Commit 64111a2

Browse files
committed
[MODEL] Clean up getting attributes from as_indexed_json in update_document
Basically, flipping the relationship here should be enough: select based on `changed_attributes` keys from the `as_indexed_json` Hash, not vice versa. Related: elastic#166
1 parent 45e6217 commit 64111a2

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,7 @@ def delete_document(options={})
336336
def update_document(options={})
337337
if changed_attributes = self.instance_variable_get(:@__changed_attributes)
338338
attributes = if respond_to?(:as_indexed_json)
339-
json = self.as_indexed_json
340-
changed_attributes.inject({}) do |memo,(key,value)|
341-
memo[key] = json[key] if json.keys.include? key
342-
memo
343-
end
339+
self.as_indexed_json.select { |k,v| changed_attributes.keys.map(&:to_s).include? k.to_s }
344340
else
345341
changed_attributes
346342
end

elasticsearch-model/test/unit/indexing_test.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def as_indexed_json(options={})
285285
instance.instance_variable_set(:@__changed_attributes, {'foo' => 'B', 'bar' => 'D' })
286286

287287
client.expects(:update).with do |payload|
288-
assert_equal({'foo' => 'B'}, payload[:body][:doc])
288+
assert_equal({:foo => 'B'}, payload[:body][:doc])
289289
end
290290

291291
instance.expects(:client).returns(client)
@@ -300,10 +300,12 @@ def as_indexed_json(options={})
300300
client = mock('client')
301301
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
302302

303-
instance.instance_variable_set(:@__changed_attributes, {foo: {ru:'B'} })
303+
instance.instance_variable_set(:@__changed_attributes, { 'foo' => { 'bar' => 'BAR'} })
304+
# Overload as_indexed_json
305+
instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR' })
304306

305307
client.expects(:update).with do |payload|
306-
assert_equal({foo: 'B'}, payload[:body][:doc])
308+
assert_equal({'foo' => 'BAR'}, payload[:body][:doc])
307309
end
308310

309311
instance.expects(:client).returns(client)

0 commit comments

Comments
 (0)