@@ -96,26 +96,27 @@ def test_cache_key_definition
96
96
assert_equal ( nil , @comment_serializer . class . _cache_key )
97
97
end
98
98
99
-
100
- def test_error_is_raised_if_cache_key_is_not_defined_on_object_or_passed_as_cache_option
101
- article = Article . new ( title : 'Must Read' )
102
- assert_raises ActiveModel ::Serializer ::Adapter ::CachedSerializer ::UndefinedCacheKey do
103
- render_object_with_cache ( article )
104
- end
105
- end
106
-
107
99
def test_cache_key_interpolation_with_updated_at_when_cache_key_is_not_defined_on_object
108
100
uncached_author = UncachedAuthor . new ( name : 'Joao M. D. Moura' )
109
101
uncached_author_serializer = AuthorSerializer . new ( uncached_author )
110
102
111
103
render_object_with_cache ( uncached_author )
112
- key = cache_key_with_adapter ( "#{ uncached_author_serializer . class . _cache_key } /#{ uncached_author_serializer . object . id } -#{ uncached_author_serializer . object . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } " )
113
- assert_equal ( uncached_author_serializer . attributes . to_json , ActionController :: Base . cache_store . fetch ( key ) . to_json )
104
+ key = "#{ uncached_author_serializer . class . _cache_key } /#{ uncached_author_serializer . object . id } -#{ uncached_author_serializer . object . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } "
105
+ assert_equal ( uncached_author_serializer . attributes . to_json , cache_store . fetch ( key ) . to_json )
114
106
end
115
107
116
108
def test_default_cache_key_fallback
117
109
render_object_with_cache ( @comment )
118
- assert_equal ( @comment_serializer . attributes . to_json , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @comment . cache_key ) ) . to_json )
110
+ key = @comment . cache_key
111
+ assert_equal ( @comment_serializer . attributes . to_json , cache_store . fetch ( key ) . to_json )
112
+ end
113
+
114
+ def test_error_is_raised_if_cache_key_is_not_defined_on_object_or_passed_as_cache_option
115
+ article = Article . new ( title : 'Must Read' )
116
+ e = assert_raises ActiveModelSerializers ::CachedSerializer ::UndefinedCacheKey do
117
+ render_object_with_cache ( article )
118
+ end
119
+ assert_match ( /ActiveModelSerializers::CacheTest::Article must define #cache_key, or the 'key:' option must be passed into 'CachedActiveModelSerializers_CacheTest_ArticleSerializer.cache'/ , e . message )
119
120
end
120
121
121
122
def test_cache_options_definition
@@ -137,8 +138,10 @@ def test_associations_separately_cache
137
138
Timecop . freeze ( Time . current ) do
138
139
render_object_with_cache ( @post )
139
140
140
- assert_equal ( @post_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @post . cache_key ) ) )
141
- assert_equal ( @comment_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @comment . cache_key ) ) )
141
+ key = @post . cache_key
142
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
143
+ key = @comment . cache_key
144
+ assert_equal ( @comment_serializer . attributes , cache_store . fetch ( key ) )
142
145
end
143
146
end
144
147
@@ -148,9 +151,10 @@ def test_associations_cache_when_updated
148
151
render_object_with_cache ( @post )
149
152
150
153
# Check if it cached the objects separately
151
- assert_equal ( @post_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @post . cache_key ) ) )
152
- assert_equal ( @comment_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @comment . cache_key ) ) )
153
-
154
+ key = @post . cache_key
155
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
156
+ key = @comment . cache_key
157
+ assert_equal ( @comment_serializer . attributes , cache_store . fetch ( key ) )
154
158
155
159
# Simulating update on comments relationship with Post
156
160
new_comment = Comment . new ( id : 2567 , body : 'ZOMG A NEW COMMENT' )
@@ -161,8 +165,10 @@ def test_associations_cache_when_updated
161
165
render_object_with_cache ( @post )
162
166
163
167
# Check if the the new comment was cached
164
- assert_equal ( new_comment_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( new_comment . cache_key ) ) )
165
- assert_equal ( @post_serializer . attributes , ActionController ::Base . cache_store . fetch ( cache_key_with_adapter ( @post . cache_key ) ) )
168
+ key = new_comment . cache_key
169
+ assert_equal ( new_comment_serializer . attributes , cache_store . fetch ( key ) )
170
+ key = @post . cache_key
171
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
166
172
end
167
173
end
168
174
@@ -177,7 +183,7 @@ def test_fragment_fetch_with_virtual_associations
177
183
hash = render_object_with_cache ( @location )
178
184
179
185
assert_equal ( hash , expected_result )
180
- assert_equal ( { place : 'Nowhere' } , ActionController :: Base . cache_store . fetch ( cache_key_with_adapter ( @location . cache_key ) ) )
186
+ assert_equal ( { place : 'Nowhere' } , cache_store . fetch ( @location . cache_key ) )
181
187
end
182
188
183
189
def test_fragment_cache_with_inheritance
@@ -188,18 +194,14 @@ def test_fragment_cache_with_inheritance
188
194
refute_includes ( base . keys , :special_attribute )
189
195
end
190
196
191
- def test_uses_adapter_in_cache_key
192
- render_object_with_cache ( @post )
193
- assert_equal ( @post_serializer . attributes , ActionController ::Base . cache_store . fetch ( "#{ @post . cache_key } /#{ adapter . class . to_s . demodulize . underscore } " ) )
194
- end
195
-
196
197
def test_uses_file_digest_in_cache_key
197
198
render_object_with_cache ( @blog )
198
- assert_equal ( @blog_serializer . attributes , ActionController ::Base . cache_store . fetch ( "#{ cache_key_with_adapter ( @blog . cache_key ) } /#{ @blog . class ::FILE_DIGEST } " ) )
199
+ key = "#{ @blog . cache_key } /#{ ::Model ::FILE_DIGEST } "
200
+ assert_equal ( @blog_serializer . attributes , cache_store . fetch ( key ) )
199
201
end
200
202
201
203
def test_cache_digest_definition
202
- assert_equal ( FILE_DIGEST , @post_serializer . class . _cache_digest )
204
+ assert_equal ( :: Model :: FILE_DIGEST , @post_serializer . class . _cache_digest )
203
205
end
204
206
205
207
def test_object_cache_keys
@@ -211,7 +213,7 @@ def test_object_cache_keys
211
213
assert_equal actual . size , 3
212
214
assert actual . any? { |key | key == 'comment/1' }
213
215
assert actual . any? { |key | key =~ %r{post/post-\d +} }
214
- assert actual . any? { |key | key =~ %r{writer /author-\d +} }
216
+ assert actual . any? { |key | key =~ %r{author /author-\d +} }
215
217
end
216
218
217
219
def test_cached_attributes
@@ -228,7 +230,7 @@ def test_cached_attributes
228
230
assert_equal cached_attributes [ @comment . post . cache_key ] , Post . new ( id : 'post' , title : 'New Post' , body : 'Body' ) . attributes
229
231
230
232
writer = @comment . post . blog . writer
231
- writer_cache_key = " writer/ #{ writer . id } - #{ writer . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } "
233
+ writer_cache_key = writer . cache_key
232
234
233
235
assert_equal cached_attributes [ writer_cache_key ] , Author . new ( id : 'author' , name : 'Joao M. D. Moura' ) . attributes
234
236
end
@@ -286,16 +288,7 @@ def test_warn_on_serializer_not_defined_in_file
286
288
private
287
289
288
290
def render_object_with_cache ( obj , options = { } )
289
- @serializable_resource = serializable ( obj , options ) . serializable_hash
290
- @serializable_resource . serializable_hash
291
- end
292
-
293
- def adapter
294
- @serializable_resource . adapter
295
- end
296
-
297
- def cache_key_with_adapter ( key )
298
- "#{ key } /#{ adapter . name . underscore } "
291
+ serializable ( obj , options ) . serializable_hash
299
292
end
300
293
301
294
def cache_store
0 commit comments