lib/elasticsearch/model/indexing.rb in elasticsearch-model-5.0.1 vs lib/elasticsearch/model/indexing.rb in elasticsearch-model-5.0.2
- old
+ new
@@ -305,21 +305,27 @@
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 return changed attributes
+ # (ie. when `Elasticsearch::Model` is included)
#
# @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)
+ base.before_save do |i|
+ if i.class.instance_methods.include?(:changes_to_save) # Rails 5.1
+ i.instance_variable_set(:@__changed_model_attributes,
+ Hash[ i.changes_to_save.map { |key, value| [key, value.last] } ])
+ elsif i.class.instance_methods.include?(:changes)
+ i.instance_variable_set(:@__changed_model_attributes,
+ Hash[ i.changes.map { |key, value| [key, value.last] } ])
+ end
+ end if base.respond_to?(:before_save)
end
# Serializes the model instance into JSON (by calling `as_indexed_json`),
# and saves the document into the Elasticsearch index.
#
@@ -389,14 +395,14 @@
# @return [Hash] The response from Elasticsearch
#
# @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(:@__changed_model_attributes)
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(
{ index: index_name,
type: document_type,