Skip to content

Commit 32ae874

Browse files
committed
[STORE] Fixed, that options have not been passed to gateway in MyModel#update
In the `Rails` module, the `update` method has been redefined to allow converting dates coming in from the Rails' forms, but the `options` argument has not been passed down to the `update` method in the `Store` module. This patch fixes that and adds unit and integration tests to cover the beahviour. Closes elastic#347
1 parent 1c43a53 commit 32ae874

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/model/rails.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(attributes={})
1313
end
1414

1515
def update(attributes={}, options={})
16-
super(__convert_rails_dates(attributes))
16+
super(__convert_rails_dates(attributes), options)
1717
end
1818
end
1919
end

elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def destroy(options={})
9999
# p.update name: 'UPDATED'
100100
# => {"_index"=>"people", ... "_version"=>2}
101101
#
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+
#
102112
# @return [Hash] The Elasticsearch response as a Hash
103113
#
104114
def update(attributes={}, options={})
@@ -112,7 +122,6 @@ def update(attributes={}, options={})
112122
options.update type: self._type if self._type
113123

114124
attributes.update( { updated_at: Time.now.utc } )
115-
116125
response = self.class.gateway.update(self.id, { doc: attributes}.merge(options))
117126

118127
self.attributes = self.attributes.merge(attributes)

elasticsearch-persistence/test/integration/model/model_basic_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ class ::Person
144144
assert found.updated_at > updated_at, [found.updated_at, updated_at].inspect
145145
end
146146

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+
147157
should "find all instances" do
148158
Person.create name: 'John Smith'
149159
Person.create name: 'Mary Smith'

elasticsearch-persistence/test/unit/model_rails_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,22 @@ class MyView; include ActionView::Helpers::UrlHelper; end
9191
assert_equal "2014-01-01", m.published_on.iso8601
9292
end
9393

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+
94111
end
95112
end

0 commit comments

Comments
 (0)