@@ -19,15 +19,15 @@ module Elasticsearch
19
19
module Model
20
20
21
21
# This module provides a proxy interfacing between the including class and
22
- # { Elasticsearch::Model} , preventing the pollution of the including class namespace.
22
+ # ` Elasticsearch::Model` , preventing the pollution of the including class namespace.
23
23
#
24
24
# The only "gateway" between the model and Elasticsearch::Model is the
25
- # `__elasticsearch__` class and instance method.
25
+ # `# __elasticsearch__` class and instance method.
26
26
#
27
27
# The including class must be compatible with
28
28
# [ActiveModel](https://github.com/rails/rails/tree/master/activemodel).
29
29
#
30
- # @example Include the { Elasticsearch::Model} module into an `Article` model
30
+ # @example Include the ` Elasticsearch::Model` module into an `Article` model
31
31
#
32
32
# class Article < ActiveRecord::Base
33
33
# include Elasticsearch::Model
@@ -53,40 +53,48 @@ module Proxy
53
53
# module and the functionality is accessible via the proxy.
54
54
#
55
55
def self . included ( base )
56
+
56
57
base . class_eval do
57
- # {ClassMethodsProxy} instance, accessed as `MyModel.__elasticsearch__`
58
- #
58
+
59
+ # `ClassMethodsProxy` instance, accessed as `MyModel.__elasticsearch__`
59
60
def self . __elasticsearch__ &block
60
61
@__elasticsearch__ ||= ClassMethodsProxy . new ( self )
61
62
@__elasticsearch__ . instance_eval ( &block ) if block_given?
62
63
@__elasticsearch__
63
64
end
64
65
65
- # {InstanceMethodsProxy}, accessed as `@mymodel.__elasticsearch__`
66
- #
67
- def __elasticsearch__ &block
68
- @__elasticsearch__ ||= InstanceMethodsProxy . new ( self )
69
- @__elasticsearch__ . instance_eval ( &block ) if block_given?
70
- @__elasticsearch__
66
+ # Mix the importing module into the `ClassMethodsProxy`
67
+ self . __elasticsearch__ . class_eval do
68
+ include Adapter . from_class ( base ) . importing_mixin
71
69
end
72
70
73
71
# Register a callback for storing changed attributes for models which implement
74
72
# `before_save` method and return changed attributes (ie. when `Elasticsearch::Model` is included)
75
73
#
76
74
# @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
77
75
#
78
- before_save do |i |
79
- if i . class . instance_methods . include? ( :changes_to_save ) # Rails 5.1
80
- a = i . __elasticsearch__ . instance_variable_get ( :@__changed_model_attributes ) || { }
81
- i . __elasticsearch__ . instance_variable_set ( :@__changed_model_attributes ,
82
- a . merge ( Hash [ i . changes_to_save . map { |key , value | [ key , value . last ] } ] ) )
83
- elsif i . class . instance_methods . include? ( :changes )
84
- a = i . __elasticsearch__ . instance_variable_get ( :@__changed_model_attributes ) || { }
85
- i . __elasticsearch__ . instance_variable_set ( :@__changed_model_attributes ,
86
- a . merge ( Hash [ i . changes . map { |key , value | [ key , value . last ] } ] ) )
76
+ before_save do |obj |
77
+ if obj . respond_to? ( :changes_to_save ) # Rails 5.1
78
+ changes_to_save = obj . changes_to_save
79
+ elsif obj . respond_to? ( :changes )
80
+ changes_to_save = obj . changes
81
+ end
82
+
83
+ if changes_to_save
84
+ attrs = obj . __elasticsearch__ . instance_variable_get ( :@__changed_model_attributes ) || { }
85
+ latest_changes = changes_to_save . inject ( { } ) { |latest_changes , ( k , v ) | latest_changes . merge! ( k => v . last ) }
86
+ obj . __elasticsearch__ . instance_variable_set ( :@__changed_model_attributes , attrs . merge ( latest_changes ) )
87
87
end
88
88
end if respond_to? ( :before_save )
89
89
end
90
+
91
+ # {InstanceMethodsProxy}, accessed as `@mymodel.__elasticsearch__`
92
+ #
93
+ def __elasticsearch__ &block
94
+ @__elasticsearch__ ||= InstanceMethodsProxy . new ( self )
95
+ @__elasticsearch__ . instance_eval ( &block ) if block_given?
96
+ @__elasticsearch__
97
+ end
90
98
end
91
99
92
100
# @overload dup
@@ -130,6 +138,11 @@ def inspect
130
138
#
131
139
class ClassMethodsProxy
132
140
include Base
141
+ include Elasticsearch ::Model ::Client ::ClassMethods
142
+ include Elasticsearch ::Model ::Naming ::ClassMethods
143
+ include Elasticsearch ::Model ::Indexing ::ClassMethods
144
+ include Elasticsearch ::Model ::Searching ::ClassMethods
145
+ include Elasticsearch ::Model ::Importing ::ClassMethods
133
146
end
134
147
135
148
# A proxy interfacing between Elasticsearch::Model instance methods and model instance methods
@@ -138,6 +151,10 @@ class ClassMethodsProxy
138
151
#
139
152
class InstanceMethodsProxy
140
153
include Base
154
+ include Elasticsearch ::Model ::Client ::InstanceMethods
155
+ include Elasticsearch ::Model ::Naming ::InstanceMethods
156
+ include Elasticsearch ::Model ::Indexing ::InstanceMethods
157
+ include Elasticsearch ::Model ::Serializing ::InstanceMethods
141
158
142
159
def klass
143
160
target . class
@@ -153,8 +170,11 @@ def class
153
170
def as_json ( options = { } )
154
171
target . as_json ( options )
155
172
end
156
- end
157
173
174
+ def as_indexed_json ( options = { } )
175
+ target . respond_to? ( :as_indexed_json ) ? target . __send__ ( :as_indexed_json , options ) : super
176
+ end
177
+ end
158
178
end
159
179
end
160
180
end
0 commit comments