Skip to content

Update changed_attributes to attributes_in_database #738

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

Closed
wants to merge 1 commit into from
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
14 changes: 7 additions & 7 deletions elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,17 @@ module InstanceMethods

def self.included(base)
# Register callback for storing changed attributes for models
# which implement `before_save` and `changed_attributes` methods
# which implement `before_save` and `attributes_in_database` methods
#
# @note This is typically triggered only when the module would be
# included in the model directly, not within the proxy.
#
# @see #update_document
#
base.before_save do |instance|
instance.instance_variable_set(:@__changed_attributes,
Hash[ instance.changes.map { |key, value| [key, value.last] } ])
end if base.respond_to?(:before_save) && base.instance_methods.include?(:changed_attributes)
instance.instance_variable_set(:@__attributes_in_database,
Hash[ instance.changes_to_save.map { |key, value| [key, value.last] } ])
end if base.respond_to?(:before_save) && base.instance_methods.include?(:attributes_in_database)
end

# Serializes the model instance into JSON (by calling `as_indexed_json`),
Expand Down Expand Up @@ -391,11 +391,11 @@ def delete_document(options={})
# @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:update
#
def update_document(options={})
if changed_attributes = self.instance_variable_get(:@__changed_attributes)
if attributes_in_database = self.instance_variable_get(:@__attributes_in_database)
attributes = if respond_to?(:as_indexed_json)
self.as_indexed_json.select { |k,v| changed_attributes.keys.map(&:to_s).include? k.to_s }
self.as_indexed_json.select { |k,v| attributes_in_database.keys.map(&:to_s).include? k.to_s }
else
changed_attributes
attributes_in_database
end

client.update(
Expand Down
10 changes: 5 additions & 5 deletions elasticsearch-model/lib/elasticsearch/model/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def __elasticsearch__ &block
end

# Register a callback for storing changed attributes for models which implement
# `before_save` and `changed_attributes` methods (when `Elasticsearch::Model` is included)
# `before_save` and `attributes_in_database` methods (when `Elasticsearch::Model` is included)
#
# @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
#
before_save do |i|
changed_attr = i.__elasticsearch__.instance_variable_get(:@__changed_attributes) || {}
i.__elasticsearch__.instance_variable_set(:@__changed_attributes,
changed_attr.merge(Hash[ i.changes.map { |key, value| [key, value.last] } ]))
end if respond_to?(:before_save) && instance_methods.include?(:changed_attributes)
changed_attr = i.__elasticsearch__.instance_variable_get(:@__attributes_in_database) || {}
i.__elasticsearch__.instance_variable_set(:@__attributes_in_database,
changed_attr.merge(Hash[ i.changes_to_save.map { |key, value| [key, value.last] } ]))
end if respond_to?(:before_save) && instance_methods.include?(:attributes_in_database)
end
end

Expand Down
18 changes: 9 additions & 9 deletions elasticsearch-model/test/unit/indexing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

def changed_attributes; [:foo]; end
def attributes_in_database; [:foo]; end

def changes
{:foo => ['One', 'Two']}
Expand All @@ -186,7 +186,7 @@ def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

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

def changes
{:foo => ['A', 'B'], :bar => ['C', 'D']}
Expand All @@ -202,10 +202,10 @@ def as_indexed_json(options={})
::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
end

should "set the @__changed_attributes variable before save" do
should "set the @__attributes_in_database variable before save" do
instance = ::DummyIndexingModelWithCallbacks.new
instance.expects(:instance_variable_set).with do |name, value|
assert_equal :@__changed_attributes, name
assert_equal :@__attributes_in_database, name
assert_equal({foo: 'Two'}, value)
true
end
Expand Down Expand Up @@ -297,7 +297,7 @@ def as_indexed_json(options={})
instance = ::DummyIndexingModelWithCallbacks.new

# Reset the fake `changes`
instance.instance_variable_set(:@__changed_attributes, nil)
instance.instance_variable_set(:@__attributes_in_database, nil)

instance.expects(:index_document)
instance.update_document
Expand All @@ -308,7 +308,7 @@ def as_indexed_json(options={})
instance = ::DummyIndexingModelWithCallbacks.new

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

client.expects(:update).with do |payload|
assert_equal 'foo', payload[:index]
Expand All @@ -331,7 +331,7 @@ def as_indexed_json(options={})
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new

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

client.expects(:update).with do |payload|
assert_equal({:foo => 'B'}, payload[:body][:doc])
Expand All @@ -350,7 +350,7 @@ def as_indexed_json(options={})
client = mock('client')
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new

instance.instance_variable_set(:@__changed_attributes, { 'foo' => { 'bar' => 'BAR'} })
instance.instance_variable_set(:@__attributes_in_database, { 'foo' => { 'bar' => 'BAR'} })
# Overload as_indexed_json
instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR' })

Expand All @@ -372,7 +372,7 @@ def as_indexed_json(options={})
instance = ::DummyIndexingModelWithCallbacks.new

# Set the fake `changes` hash
instance.instance_variable_set(:@__changed_attributes, {author: 'john'})
instance.instance_variable_set(:@__attributes_in_database, {author: 'john'})

client.expects(:update).with do |payload|
assert_equal 'foo', payload[:index]
Expand Down
6 changes: 3 additions & 3 deletions elasticsearch-model/test/unit/proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

def changed_attributes; [:foo]; end
def attributes_in_database; [:foo]; end

def changes
{:foo => ['One', 'Two']}
Expand All @@ -43,10 +43,10 @@ def changes
DummyProxyModelWithCallbacks.__send__ :include, Elasticsearch::Model::Proxy
end

should "set the @__changed_attributes variable before save" do
should "set the @__attributes_in_database variable before save" do
instance = ::DummyProxyModelWithCallbacks.new
instance.__elasticsearch__.expects(:instance_variable_set).with do |name, value|
assert_equal :@__changed_attributes, name
assert_equal :@__attributes_in_database, name
assert_equal({foo: 'Two'}, value)
true
end
Expand Down