Skip to content

Commit b25bac9

Browse files
committed
Model#save should persist updated_at timestamp
1 parent 8508577 commit b25bac9

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def deserialize(document)
121121

122122
# Set up common attributes
123123
#
124-
attribute :created_at, DateTime, default: lambda { |o,a| Time.now.utc }
125-
attribute :updated_at, DateTime, default: lambda { |o,a| Time.now.utc }
124+
attribute :created_at, Time, default: lambda { |o,a| Time.now.utc }
125+
attribute :updated_at, Time, default: lambda { |o,a| Time.now.utc }
126126

127127
attr_reader :hit
128128
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ def save(options={})
5353
options.update index: self._index if self._index
5454
options.update type: self._type if self._type
5555

56-
response = self.class.gateway.save(self, options)
57-
5856
self[:updated_at] = Time.now.utc
5957

58+
response = self.class.gateway.save(self, options)
59+
6060
@_id = response['_id']
6161
@_index = response['_index']
6262
@_type = response['_type']

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ class ::Person
144144
assert found.updated_at > updated_at, [found.updated_at, updated_at].inspect
145145
end
146146

147+
should 'update the object timestamp on save' do
148+
person = Person.create name: 'John Smith'
149+
person.admin = true
150+
sleep 1
151+
person.save
152+
153+
Person.gateway.refresh_index!
154+
155+
found = Person.find(person.id)
156+
157+
# Compare without usec
158+
assert_equal found.updated_at.to_i, person.updated_at.to_i
159+
end
160+
147161
should "find all instances" do
148162
Person.create name: 'John Smith'
149163
Person.create name: 'Mary Smith'

0 commit comments

Comments
 (0)