Skip to content

Commit b363c58

Browse files
committed
[MODEL] Removed the delegation of indexing/serializing methods from model instance to proxy
Added reversed delegation from the proxy to the instance, when it defines the `as_indexed_json` method, to pick it up when calling `index/update_document.
1 parent f6006db commit b363c58

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

elasticsearch-model/lib/elasticsearch/model.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ def self.included(base)
9090
include Elasticsearch::Model::Serializing::InstanceMethods
9191
end
9292

93+
Elasticsearch::Model::Proxy::InstanceMethodsProxy.class_eval <<-CODE, __FILE__, __LINE__ + 1
94+
def as_indexed_json(options={})
95+
target.respond_to?(:as_indexed_json) ? target.__send__(:as_indexed_json, options) : super
96+
end
97+
CODE
98+
9399
# Delegate important methods to the `__elasticsearch__` proxy, unless they are defined already
94100
#
95101
extend Support::Forwardable
@@ -101,11 +107,6 @@ def self.included(base)
101107
forward :'self.__elasticsearch__', :document_type unless respond_to?(:document_type)
102108
forward :'self.__elasticsearch__', :import unless respond_to?(:import)
103109

104-
instance_delegate [:as_indexed_json] => :__elasticsearch__
105-
instance_delegate [:index_document] => :__elasticsearch__
106-
instance_delegate [:update_document] => :__elasticsearch__
107-
instance_delegate [:delete_document] => :__elasticsearch__
108-
109110
# Mix the importing module into the proxy
110111
#
111112
self.__elasticsearch__.class.__send__ :include, Elasticsearch::Model::Importing::ClassMethods

elasticsearch-model/lib/elasticsearch/model/proxy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ module Model
2121
#
2222
# article = Article.first
2323
#
24-
# article.respond_to? :as_indexed_json
24+
# article.respond_to? :index_document
2525
# # => false
2626
#
27-
# article.__elasticsearch__.respond_to?(:as_indexed_json)
27+
# article.__elasticsearch__.respond_to?(:index_document)
2828
# # => true
2929
#
3030
module Proxy

elasticsearch-model/lib/elasticsearch/model/serializing.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ module InstanceMethods
1212

1313
# Serialize the record as a Hash, to be passed to the client.
1414
#
15+
# Re-define this method to customize the serialization.
16+
#
1517
# @return [Hash]
1618
#
17-
# @example
19+
# @example Return the model instance as a Hash
1820
#
19-
# Article.first.__elasticsearch__.as_indexed_json(only: 'title')
21+
# Article.first.__elasticsearch__.as_indexed_json
2022
# => {"title"=>"Foo"}
2123
#
2224
# @see Elasticsearch::Model::Indexing

0 commit comments

Comments
 (0)