diff --git a/elasticsearch-model/lib/elasticsearch/model/indexing.rb b/elasticsearch-model/lib/elasticsearch/model/indexing.rb index ac507b079..d58472a9f 100644 --- a/elasticsearch-model/lib/elasticsearch/model/indexing.rb +++ b/elasticsearch-model/lib/elasticsearch/model/indexing.rb @@ -307,7 +307,7 @@ 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. @@ -315,9 +315,9 @@ def self.included(base) # @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( diff --git a/elasticsearch-model/lib/elasticsearch/model/proxy.rb b/elasticsearch-model/lib/elasticsearch/model/proxy.rb index 3e37f28ec..606c079a6 100644 --- a/elasticsearch-model/lib/elasticsearch/model/proxy.rb +++ b/elasticsearch-model/lib/elasticsearch/model/proxy.rb @@ -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 diff --git a/elasticsearch-model/test/unit/indexing_test.rb b/elasticsearch-model/test/unit/indexing_test.rb index b78651340..94f3d967b 100644 --- a/elasticsearch-model/test/unit/indexing_test.rb +++ b/elasticsearch-model/test/unit/indexing_test.rb @@ -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] diff --git a/elasticsearch-model/test/unit/proxy_test.rb b/elasticsearch-model/test/unit/proxy_test.rb index d7299f884..e403b6141 100644 --- a/elasticsearch-model/test/unit/proxy_test.rb +++ b/elasticsearch-model/test/unit/proxy_test.rb @@ -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