Skip to content

Fix deprecation warning from Rails 5.1 upgrade (changed_attributes is now attributes_in_database) #758

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
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
@@ -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`),
@@ -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(
10 changes: 5 additions & 5 deletions elasticsearch-model/lib/elasticsearch/model/proxy.rb
Original file line number Diff line number Diff line change
@@ -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

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

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

def changes
def changes_to_save
{:foo => ['One', 'Two']}
end
end
@@ -186,9 +186,9 @@ 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
def changes_to_save
{:foo => ['A', 'B'], :bar => ['C', 'D']}
end

@@ -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
@@ -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
@@ -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]
@@ -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])
@@ -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' })

@@ -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]
8 changes: 4 additions & 4 deletions elasticsearch-model/test/unit/proxy_test.rb
Original file line number Diff line number Diff line change
@@ -23,9 +23,9 @@ def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

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

def changes
def changes_to_save
{:foo => ['One', 'Two']}
end
end
@@ -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