Skip to content

Commit 900899c

Browse files
Geesukarmi
authored andcommitted
[MODEL] Updated the changes method name in Indexing to changes_to_save for compatibility with Rails 5.1
Without this patch, the log on Rails 5.1 is full of deprecation warnings like: DEPRECATION WARNING: The behavior of `changed_attributes` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_changes.transform_values(&:first)` instead. (called from update_funding_caches at... It was first reported in #714. This patch fixes #758.
1 parent 22b698a commit 900899c

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

Diff for: elasticsearch-model/lib/elasticsearch/model/indexing.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,17 @@ module InstanceMethods
307307

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

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

401401
client.update(

Diff for: elasticsearch-model/lib/elasticsearch/model/proxy.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ def __elasticsearch__ &block
5454
end
5555

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

Diff for: elasticsearch-model/test/unit/indexing_test.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ def self.before_save(&block)
171171
(@callbacks ||= {})[block.hash] = block
172172
end
173173

174-
def changed_attributes; [:foo]; end
174+
def attributes_in_database; [:foo]; end
175175

176-
def changes
176+
def changes_to_save
177177
{:foo => ['One', 'Two']}
178178
end
179179
end
@@ -186,9 +186,9 @@ def self.before_save(&block)
186186
(@callbacks ||= {})[block.hash] = block
187187
end
188188

189-
def changed_attributes; [:foo, :bar]; end
189+
def attributes_in_database; [:foo, :bar]; end
190190

191-
def changes
191+
def changes_to_save
192192
{:foo => ['A', 'B'], :bar => ['C', 'D']}
193193
end
194194

@@ -202,10 +202,10 @@ def as_indexed_json(options={})
202202
::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
203203
end
204204

205-
should "set the @__changed_attributes variable before save" do
205+
should "set the @__attributes_in_database variable before save" do
206206
instance = ::DummyIndexingModelWithCallbacks.new
207207
instance.expects(:instance_variable_set).with do |name, value|
208-
assert_equal :@__changed_attributes, name
208+
assert_equal :@__attributes_in_database, name
209209
assert_equal({foo: 'Two'}, value)
210210
true
211211
end
@@ -297,7 +297,7 @@ def as_indexed_json(options={})
297297
instance = ::DummyIndexingModelWithCallbacks.new
298298

299299
# Reset the fake `changes`
300-
instance.instance_variable_set(:@__changed_attributes, nil)
300+
instance.instance_variable_set(:@__attributes_in_database, nil)
301301

302302
instance.expects(:index_document)
303303
instance.update_document
@@ -308,7 +308,7 @@ def as_indexed_json(options={})
308308
instance = ::DummyIndexingModelWithCallbacks.new
309309

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

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

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

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

353-
instance.instance_variable_set(:@__changed_attributes, { 'foo' => { 'bar' => 'BAR'} })
353+
instance.instance_variable_set(:@__attributes_in_database, { 'foo' => { 'bar' => 'BAR'} })
354354
# Overload as_indexed_json
355355
instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR' })
356356

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

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

377377
client.expects(:update).with do |payload|
378378
assert_equal 'foo', payload[:index]

Diff for: elasticsearch-model/test/unit/proxy_test.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def self.before_save(&block)
2323
(@callbacks ||= {})[block.hash] = block
2424
end
2525

26-
def changed_attributes; [:foo]; end
26+
def attributes_in_database; [:foo]; end
2727

28-
def changes
28+
def changes_to_save
2929
{:foo => ['One', 'Two']}
3030
end
3131
end
@@ -43,10 +43,10 @@ def changes
4343
DummyProxyModelWithCallbacks.__send__ :include, Elasticsearch::Model::Proxy
4444
end
4545

46-
should "set the @__changed_attributes variable before save" do
46+
should "set the @__attributes_in_database variable before save" do
4747
instance = ::DummyProxyModelWithCallbacks.new
4848
instance.__elasticsearch__.expects(:instance_variable_set).with do |name, value|
49-
assert_equal :@__changed_attributes, name
49+
assert_equal :@__attributes_in_database, name
5050
assert_equal({foo: 'Two'}, value)
5151
true
5252
end

0 commit comments

Comments
 (0)