elasticsearch-model 5.0.1 → 5.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 841a2cd66c4436518ca82fbc76d1f2d330df24bc
4
- data.tar.gz: 311da45695ae2e8cad3ddf61a0664f130ea5a446
3
+ metadata.gz: 2b1f416db5d498101719140c9fbfc19ac0f6c5ad
4
+ data.tar.gz: 617646d010a2b7f51ae5e11bf6bccca5836d815a
5
5
  SHA512:
6
- metadata.gz: 2e253ae0ff6bdd1f01348bc9b83696daa2f503f6ceb5b22684d36ad549ba47519d5eb8badc79837b38a646d66add5e67b18c5290d0e5def7b2e2735168ac5902
7
- data.tar.gz: 8bb946ea9226bbe29324e4308dd100bd53ea8e1718c64082698fa674c7a861e2d21aed50793314e34f118a658f13967376cb29a5ba50dc350b44de8e7d213c82
6
+ metadata.gz: 99f3847abe5159e91a74c178c0682c309bebe0d31af0bc5eab5cca21a2cb379e0c4f011c58fc73536c72ff0942ecaabac1c25db61c1c51f496405fa399426556
7
+ data.tar.gz: 1cc772377f57523268cf6767e833869719e25e5ba30c64b870ddeceb2550a55ce619dac5fb1a06349066d36662dbd2cedad6b087f5f26ddbbf535c7d663b93e2
@@ -307,17 +307,23 @@ module Elasticsearch
307
307
 
308
308
  def self.included(base)
309
309
  # Register callback for storing changed attributes for models
310
- # which implement `before_save` and `changed_attributes` methods
310
+ # which implement `before_save` and return changed attributes
311
+ # (ie. when `Elasticsearch::Model` is included)
311
312
  #
312
313
  # @note This is typically triggered only when the module would be
313
314
  # included in the model directly, not within the proxy.
314
315
  #
315
316
  # @see #update_document
316
317
  #
317
- 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
+ base.before_save do |i|
319
+ if i.class.instance_methods.include?(:changes_to_save) # Rails 5.1
320
+ i.instance_variable_set(:@__changed_model_attributes,
321
+ Hash[ i.changes_to_save.map { |key, value| [key, value.last] } ])
322
+ elsif i.class.instance_methods.include?(:changes)
323
+ i.instance_variable_set(:@__changed_model_attributes,
324
+ Hash[ i.changes.map { |key, value| [key, value.last] } ])
325
+ end
326
+ end if base.respond_to?(:before_save)
321
327
  end
322
328
 
323
329
  # Serializes the model instance into JSON (by calling `as_indexed_json`),
@@ -391,11 +397,11 @@ module Elasticsearch
391
397
  # @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:update
392
398
  #
393
399
  def update_document(options={})
394
- if changed_attributes = self.instance_variable_get(:@__changed_attributes)
400
+ if attributes_in_database = self.instance_variable_get(:@__changed_model_attributes)
395
401
  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 }
402
+ self.as_indexed_json.select { |k,v| attributes_in_database.keys.map(&:to_s).include? k.to_s }
397
403
  else
398
- changed_attributes
404
+ attributes_in_database
399
405
  end
400
406
 
401
407
  client.update(
@@ -54,15 +54,21 @@ module Elasticsearch
54
54
  end
55
55
 
56
56
  # 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` method and return changed attributes (ie. when `Elasticsearch::Model` is included)
58
58
  #
59
59
  # @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
60
60
  #
61
61
  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
+ if i.class.instance_methods.include?(:changes_to_save) # Rails 5.1
63
+ a = i.__elasticsearch__.instance_variable_get(:@__changed_model_attributes) || {}
64
+ i.__elasticsearch__.instance_variable_set(:@__changed_model_attributes,
65
+ a.merge(Hash[ i.changes_to_save.map { |key, value| [key, value.last] } ]))
66
+ elsif i.class.instance_methods.include?(:changes)
67
+ a = i.__elasticsearch__.instance_variable_get(:@__changed_model_attributes) || {}
68
+ i.__elasticsearch__.instance_variable_set(:@__changed_model_attributes,
69
+ a.merge(Hash[ i.changes.map { |key, value| [key, value.last] } ]))
70
+ end
71
+ end if respond_to?(:before_save)
66
72
  end
67
73
  end
68
74
 
@@ -1,5 +1,5 @@
1
1
  module Elasticsearch
2
2
  module Model
3
- VERSION = "5.0.1"
3
+ VERSION = "5.0.2"
4
4
  end
5
5
  end
@@ -171,9 +171,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
171
171
  (@callbacks ||= {})[block.hash] = block
172
172
  end
173
173
 
174
- def changed_attributes; [:foo]; end
175
-
176
- def changes
174
+ def changes_to_save
177
175
  {:foo => ['One', 'Two']}
178
176
  end
179
177
  end
@@ -186,9 +184,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
186
184
  (@callbacks ||= {})[block.hash] = block
187
185
  end
188
186
 
189
- def changed_attributes; [:foo, :bar]; end
190
-
191
- def changes
187
+ def changes_to_save
192
188
  {:foo => ['A', 'B'], :bar => ['C', 'D']}
193
189
  end
194
190
 
@@ -197,15 +193,28 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
197
193
  end
198
194
  end
199
195
 
196
+ class ::DummyIndexingModelWithOldDirty
197
+ extend Elasticsearch::Model::Indexing::ClassMethods
198
+ include Elasticsearch::Model::Indexing::InstanceMethods
199
+
200
+ def self.before_save(&block)
201
+ (@callbacks ||= {})[block.hash] = block
202
+ end
203
+
204
+ def changes
205
+ {:foo => ['One', 'Two']}
206
+ end
207
+ end
208
+
200
209
  should "register before_save callback when included" do
201
210
  ::DummyIndexingModelWithCallbacks.expects(:before_save).returns(true)
202
211
  ::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
203
212
  end
204
213
 
205
- should "set the @__changed_attributes variable before save" do
214
+ should "set the @__changed_model_attributes variable before save" do
206
215
  instance = ::DummyIndexingModelWithCallbacks.new
207
216
  instance.expects(:instance_variable_set).with do |name, value|
208
- assert_equal :@__changed_attributes, name
217
+ assert_equal :@__changed_model_attributes, name
209
218
  assert_equal({foo: 'Two'}, value)
210
219
  true
211
220
  end
@@ -217,6 +226,23 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
217
226
  end
218
227
  end
219
228
 
229
+ # https://github.com/elastic/elasticsearch-rails/issues/714
230
+ # https://github.com/rails/rails/pull/25337#issuecomment-225166796
231
+ should "set the @__changed_model_attributes variable before save for old ActiveModel::Dirty" do
232
+ instance = ::DummyIndexingModelWithOldDirty.new
233
+ instance.expects(:instance_variable_set).with do |name, value|
234
+ assert_equal :@__changed_model_attributes, name
235
+ assert_equal({foo: 'Two'}, value)
236
+ true
237
+ end
238
+
239
+ ::DummyIndexingModelWithOldDirty.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
240
+
241
+ ::DummyIndexingModelWithOldDirty.instance_variable_get(:@callbacks).each do |n,b|
242
+ instance.instance_eval(&b)
243
+ end
244
+ end
245
+
220
246
  should "have the index_document method" do
221
247
  client = mock('client')
222
248
  instance = ::DummyIndexingModelWithCallbacks.new
@@ -297,7 +323,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
297
323
  instance = ::DummyIndexingModelWithCallbacks.new
298
324
 
299
325
  # Reset the fake `changes`
300
- instance.instance_variable_set(:@__changed_attributes, nil)
326
+ instance.instance_variable_set(:@__changed_model_attributes, nil)
301
327
 
302
328
  instance.expects(:index_document)
303
329
  instance.update_document
@@ -308,7 +334,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
308
334
  instance = ::DummyIndexingModelWithCallbacks.new
309
335
 
310
336
  # Set the fake `changes` hash
311
- instance.instance_variable_set(:@__changed_attributes, {foo: 'bar'})
337
+ instance.instance_variable_set(:@__changed_model_attributes, {foo: 'bar'})
312
338
 
313
339
  client.expects(:update).with do |payload|
314
340
  assert_equal 'foo', payload[:index]
@@ -331,7 +357,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
331
357
  instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
332
358
 
333
359
  # Set the fake `changes` hash
334
- instance.instance_variable_set(:@__changed_attributes, {'foo' => 'B', 'bar' => 'D' })
360
+ instance.instance_variable_set(:@__changed_model_attributes, {'foo' => 'B', 'bar' => 'D' })
335
361
 
336
362
  client.expects(:update).with do |payload|
337
363
  assert_equal({:foo => 'B'}, payload[:body][:doc])
@@ -350,7 +376,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
350
376
  client = mock('client')
351
377
  instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
352
378
 
353
- instance.instance_variable_set(:@__changed_attributes, { 'foo' => { 'bar' => 'BAR'} })
379
+ instance.instance_variable_set(:@__changed_model_attributes, { 'foo' => { 'bar' => 'BAR'} })
354
380
  # Overload as_indexed_json
355
381
  instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR' })
356
382
 
@@ -372,7 +398,7 @@ class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
372
398
  instance = ::DummyIndexingModelWithCallbacks.new
373
399
 
374
400
  # Set the fake `changes` hash
375
- instance.instance_variable_set(:@__changed_attributes, {author: 'john'})
401
+ instance.instance_variable_set(:@__changed_model_attributes, {author: 'john'})
376
402
 
377
403
  client.expects(:update).with do |payload|
378
404
  assert_equal 'foo', payload[:index]
@@ -23,9 +23,7 @@ class Elasticsearch::Model::SearchTest < Test::Unit::TestCase
23
23
  (@callbacks ||= {})[block.hash] = block
24
24
  end
25
25
 
26
- def changed_attributes; [:foo]; end
27
-
28
- def changes
26
+ def changes_to_save
29
27
  {:foo => ['One', 'Two']}
30
28
  end
31
29
  end
@@ -43,10 +41,10 @@ class Elasticsearch::Model::SearchTest < Test::Unit::TestCase
43
41
  DummyProxyModelWithCallbacks.__send__ :include, Elasticsearch::Model::Proxy
44
42
  end
45
43
 
46
- should "set the @__changed_attributes variable before save" do
44
+ should "set the @__changed_model_attributes variable before save" do
47
45
  instance = ::DummyProxyModelWithCallbacks.new
48
46
  instance.__elasticsearch__.expects(:instance_variable_set).with do |name, value|
49
- assert_equal :@__changed_attributes, name
47
+ assert_equal :@__changed_model_attributes, name
50
48
  assert_equal({foo: 'Two'}, value)
51
49
  true
52
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.1
4
+ version: 5.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-04 00:00:00.000000000 Z
11
+ date: 2017-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch
@@ -432,7 +432,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
432
432
  version: '0'
433
433
  requirements: []
434
434
  rubyforge_project:
435
- rubygems_version: 2.6.10
435
+ rubygems_version: 2.5.2
436
436
  signing_key:
437
437
  specification_version: 4
438
438
  summary: ActiveModel/Record integrations for Elasticsearch.