File tree 4 files changed +38
-2
lines changed
elasticsearch-persistence
lib/elasticsearch/persistence/model
4 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ def initialize(attributes={})
13
13
end
14
14
15
15
def update ( attributes = { } , options = { } )
16
- super ( __convert_rails_dates ( attributes ) )
16
+ super ( __convert_rails_dates ( attributes ) , options )
17
17
end
18
18
end
19
19
end
Original file line number Diff line number Diff line change @@ -99,6 +99,16 @@ def destroy(options={})
99
99
# p.update name: 'UPDATED'
100
100
# => {"_index"=>"people", ... "_version"=>2}
101
101
#
102
+ # @example Pass a version for concurrency control
103
+ #
104
+ # p.update( { name: 'UPDATED' }, { version: 2 } )
105
+ # => {"_index"=>"people", ... "_version"=>3}
106
+ #
107
+ # @example An exception is raised when the version doesn't match
108
+ #
109
+ # p.update( { name: 'UPDATED' }, { version: 2 } )
110
+ # => Elasticsearch::Transport::Transport::Errors::Conflict: [409] {"error" ... }
111
+ #
102
112
# @return [Hash] The Elasticsearch response as a Hash
103
113
#
104
114
def update ( attributes = { } , options = { } )
@@ -112,7 +122,6 @@ def update(attributes={}, options={})
112
122
options . update type : self . _type if self . _type
113
123
114
124
attributes . update ( { updated_at : Time . now . utc } )
115
-
116
125
response = self . class . gateway . update ( self . id , { doc : attributes } . merge ( options ) )
117
126
118
127
self . attributes = self . attributes . merge ( attributes )
Original file line number Diff line number Diff line change @@ -144,6 +144,16 @@ class ::Person
144
144
assert found . updated_at > updated_at , [ found . updated_at , updated_at ] . inspect
145
145
end
146
146
147
+ should "respect the version" do
148
+ person = Person . create name : 'John Smith'
149
+
150
+ person . update ( { name : 'UPDATE 1' } )
151
+
152
+ assert_raise Elasticsearch ::Transport ::Transport ::Errors ::Conflict do
153
+ person . update ( { name : 'UPDATE 2' } , { version : 1 } )
154
+ end
155
+ end
156
+
147
157
should "find all instances" do
148
158
Person . create name : 'John Smith'
149
159
Person . create name : 'Mary Smith'
Original file line number Diff line number Diff line change @@ -91,5 +91,22 @@ class MyView; include ActionView::Helpers::UrlHelper; end
91
91
assert_equal "2014-01-01" , m . published_on . iso8601
92
92
end
93
93
94
+ context "when updating," do
95
+ should "pass the options to gateway" do
96
+ model = MyRailsModel . new name : 'Test'
97
+ model . stubs ( :persisted? ) . returns ( true )
98
+
99
+ model . class . gateway
100
+ . expects ( :update )
101
+ . with do |object , options |
102
+ assert_equal 'ABC' , options [ :routing ]
103
+ true
104
+ end
105
+ . returns ( { '_id' => 'abc123' } )
106
+
107
+ assert model . update ( { title : 'UPDATED' } , { routing : 'ABC' } )
108
+ end
109
+ end
110
+
94
111
end
95
112
end
You can’t perform that action at this time.
0 commit comments