Skip to content
Closed
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
4 changes: 3 additions & 1 deletion elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ def delete_document(options={})
def update_document(options={})
if changed_attributes = self.instance_variable_get(:@__changed_attributes)
attributes = if respond_to?(:as_indexed_json)
self.as_indexed_json.select { |k,v| changed_attributes.keys.map(&:to_s).include? k.to_s }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change for performance? Original version seemed more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original change included with_indifferent_access method which is a rails extension which I removed here.

indexed_json = self.as_indexed_json
(self.attributes.keys.map(&:to_s) - changed_attributes.keys.map(&:to_s)).each{ |k| indexed_json.delete_if{ |a,b| a.to_s == k } }
indexed_json
else
changed_attributes
end
Expand Down
70 changes: 70 additions & 0 deletions elasticsearch-model/test/unit/indexing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

def attributes; { :_id => 1, :foo => 'B', :bar => 'D', :baz => 'F' }; end

def changed_attributes; [:foo, :bar]; end

def changes
Expand All @@ -197,6 +199,31 @@ def as_indexed_json(options={})
end
end

class ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJsonWithDerivedAttribute
extend Elasticsearch::Model::Indexing::ClassMethods
include Elasticsearch::Model::Indexing::InstanceMethods

def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

def attributes; { :_id => 1, :foo => 'B', :bar => 'D', :baz => 'F' }; end

def changed_attributes; [:foo, :bar]; end

def changes
{:foo => ['A', 'B'], :bar => ['C', 'D']}
end

def as_indexed_json(options={})
{ :foo => 'B', :bar => 'D', :qux => derived_value }
end

def derived_value
'E'
end
end

should "register before_save callback when included" do
::DummyIndexingModelWithCallbacks.expects(:before_save).returns(true)
::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
Expand Down Expand Up @@ -367,6 +394,49 @@ def as_indexed_json(options={})
instance.update_document
end

should "exclude attributes not contained in changed_attributes from custom as_indexed_json during partial update" do
client = mock('client')
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new

# Set the fake `changes` hash
instance.instance_variable_set(:@__changed_attributes, {'foo' => 'B' })

# Overload as_indexed_json
instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR', 'bar' => 'BAZ' })

client.expects(:update).with do |payload|
assert_equal({'foo' => 'BAR'}, payload[:body][:doc])
true
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

should "include derived attributes from custom as_indexed_json during partial update" do
client = mock('client')
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJsonWithDerivedAttribute.new

# Set the fake `changes` hash
instance.instance_variable_set(:@__changed_attributes, {foo: 'B'})

client.expects(:update).with do |payload|
assert_equal({foo: 'B', qux: 'E'}, payload[:body][:doc])
true
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

should "update only the specific attributes" do
client = mock('client')
instance = ::DummyIndexingModelWithCallbacks.new
Expand Down