Skip to content

Only execute update if document attributes is not empty #862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def update_document(options={})
type: document_type,
id: self.id,
body: { doc: attributes } }.merge(options)
)
) unless attributes.empty?
else
index_document(options)
end
Expand Down
23 changes: 19 additions & 4 deletions elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,10 @@ def changes
context 'when changes are present' do

before do
expect(instance).to receive(:client).and_return(client)
expect(instance).to receive(:index_name).and_return('foo')
expect(instance).to receive(:document_type).and_return('bar')
expect(instance).to receive(:id).and_return('1')
allow(instance).to receive(:client).and_return(client)
allow(instance).to receive(:index_name).and_return('foo')
allow(instance).to receive(:document_type).and_return('bar')
allow(instance).to receive(:id).and_return('1')
end

context 'when the changes are included in the as_indexed_json representation' do
Expand Down Expand Up @@ -474,6 +474,21 @@ def changes
end
end

context 'when none of the changes are included in the as_indexed_json representation' do

let(:instance) do
DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
end

before do
instance.instance_variable_set(:@__changed_model_attributes, {'bar' => 'D' })
end

it 'does not update the document' do
expect(instance.update_document).to_not be(true)
end
end

context 'when there are partial updates' do

let(:instance) do
Expand Down