Skip to content

Commit 6699023

Browse files
committed
Use to_f for hash_cache_field to get millisecond precision for ActiveSupport::TimeWithZone timestamps
1 parent 0fea667 commit 6699023

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

lib/jsonapi/basic_resource.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,12 @@ def attribute_caching_context(_context)
10191019

10201020
# Generate a hashcode from the value to be used as part of the cache lookup
10211021
def hash_cache_field(value)
1022-
Digest::MD5.hexdigest(value.to_s)
1022+
if value.is_a?(ActiveSupport::TimeWithZone)
1023+
# for timestamps use to_f instead of computing a hash
1024+
value.to_f
1025+
else
1026+
Digest::MD5.hexdigest(value)
1027+
end
10231028
end
10241029

10251030
def _model_class

test/fixtures/posts.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ post_1:
33
title: New post
44
body: A body!!!
55
author_id: 1001
6+
created_at: 2020-01-01 14:15:16 UTC
7+
updated_at: 2020-01-15 14:15:12 UTC
68

79
post_2:
810
id: 2

test/unit/resource/resource_test.rb

+28-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class PostWithReadonlyAttributesResource < JSONAPI::Resource
9595

9696
class ResourceTest < ActiveSupport::TestCase
9797
def setup
98-
@post = Post.first
98+
@post = posts(:post_1)
9999
end
100100

101101
def test_model_name
@@ -126,6 +126,33 @@ def test_resource_for_resource_does_not_exist_at_root
126126
end
127127
end
128128

129+
def test_cache_ids_are_consistent_from_run_to_run
130+
assert_equal '2020-01-15 14:15:12 UTC', @post.updated_at.to_s
131+
132+
post_resource = PostResource.new(@post, {})
133+
assert_equal [@post.id, 1579097712.0], post_resource.cache_id
134+
end
135+
136+
def test_cache_ids_change_when_cache_field_is_changed
137+
assert_equal '2020-01-15 14:15:12 UTC', @post.updated_at.to_s
138+
139+
post_resource = PostResource.new(@post, {})
140+
assert_equal [@post.id, 1579097712.0], post_resource.cache_id
141+
142+
assert @post.touch
143+
refute_equal [@post.id, 1579097712.0], post_resource.cache_id
144+
end
145+
146+
def test_date_based_cache_ids_change_with_milliseconds_if_hash_cache_field_is_time_stamp
147+
assert_equal '2020-01-15 14:15:12 UTC', @post.updated_at.to_s
148+
149+
post_resource = PostResource.new(@post, {})
150+
assert_equal [@post.id, 1579097712.0], post_resource.cache_id
151+
152+
@post.updated_at = @post.updated_at + 0.222
153+
refute_equal [@post.id, 1579097712.0], post_resource.cache_id
154+
end
155+
129156
def test_resource_for_with_underscored_namespaced_paths
130157
assert_equal(JSONAPI::Resource.resource_klass_for('my_module/related'), MyModule::RelatedResource)
131158
assert_equal(PostResource.resource_klass_for('my_module/related'), MyModule::RelatedResource)

0 commit comments

Comments
 (0)