From a0775cb226d67bf6d3fa427d32b7aaeec0572041 Mon Sep 17 00:00:00 2001 From: Yashchuk Oleg Date: Fri, 11 Jul 2014 01:40:49 +0400 Subject: [PATCH] Changed attributes fix --- .../lib/elasticsearch/model/indexing.rb | 6 +++++- elasticsearch-model/test/unit/indexing_test.rb | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/elasticsearch-model/lib/elasticsearch/model/indexing.rb b/elasticsearch-model/lib/elasticsearch/model/indexing.rb index 360b8ea85..8aae2bd9b 100644 --- a/elasticsearch-model/lib/elasticsearch/model/indexing.rb +++ b/elasticsearch-model/lib/elasticsearch/model/indexing.rb @@ -336,7 +336,11 @@ def delete_document(options={}) def update_document(options={}) if changed_attributes = self.instance_variable_get(:@__changed_attributes) attributes = if respond_to?(:as_indexed_json) - changed_attributes.select { |k,v| self.as_indexed_json.keys.include? k } + json = self.as_indexed_json + changed_attributes.inject({}) do |memo,(key,value)| + memo[key] = json[key] if json.keys.include? key + memo + end else changed_attributes end diff --git a/elasticsearch-model/test/unit/indexing_test.rb b/elasticsearch-model/test/unit/indexing_test.rb index 57d121d61..a3e7bbbab 100644 --- a/elasticsearch-model/test/unit/indexing_test.rb +++ b/elasticsearch-model/test/unit/indexing_test.rb @@ -295,6 +295,24 @@ def as_indexed_json(options={}) instance.update_document end + + should "get attributes from as_indexed_json during partial update" do + client = mock('client') + instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new + + instance.instance_variable_set(:@__changed_attributes, {foo: {ru:'B'} }) + + client.expects(:update).with do |payload| + assert_equal({foo: 'B'}, payload[:body][:doc]) + end + + instance.expects(:client).returns(client) + instance.expects(:index_name).returns('foo') + instance.expects(:document_type).returns('bar') + instance.expects(:id).returns('1') + + instance.update_document + end end context "Re-creating the index" do