4
4
5
5
module ActiveModelSerializers
6
6
class CacheTest < ActiveSupport ::TestCase
7
+ class Article < ::Model
8
+ attributes :title
9
+ # To confirm error is raised when cache_key is not set and cache_key option not passed to cache
10
+ undef_method :cache_key
11
+ end
12
+ class ArticleSerializer < ActiveModel ::Serializer
13
+ cache only : [ :place ] , skip_digest : true
14
+ attributes :title
15
+ end
16
+
17
+ class Author < ::Model
18
+ attributes :id , :name
19
+ associations :posts , :bio , :roles
20
+ end
7
21
# Instead of a primitive cache key (i.e. a string), this class
8
22
# returns a list of objects that require to be expanded themselves.
9
23
class AuthorWithExpandableCacheElements < Author
@@ -27,45 +41,85 @@ def cache_key
27
41
]
28
42
end
29
43
end
30
-
31
44
class UncachedAuthor < Author
32
45
# To confirm cache_key is set using updated_at and cache_key option passed to cache
33
46
undef_method :cache_key
34
47
end
48
+ class AuthorSerializer < ActiveModel ::Serializer
49
+ cache key : 'writer' , skip_digest : true
50
+ attributes :id , :name
35
51
36
- class Article < ::Model
37
- attributes :title
38
- # To confirm error is raised when cache_key is not set and cache_key option not passed to cache
39
- undef_method :cache_key
52
+ has_many :posts
53
+ has_many :roles
54
+ has_one :bio
40
55
end
41
56
42
- class ArticleSerializer < ActiveModel :: Serializer
43
- cache only : [ :place ] , skip_digest : true
44
- attributes :title
57
+ class Blog < :: Model
58
+ attributes :name
59
+ associations :writer
45
60
end
61
+ class BlogSerializer < ActiveModel ::Serializer
62
+ cache key : 'blog'
63
+ attributes :id , :name
46
64
47
- class InheritedRoleSerializer < RoleSerializer
48
- cache key : 'inherited_role' , only : [ :name , :special_attribute ]
49
- attribute :special_attribute
65
+ belongs_to :writer
50
66
end
51
67
52
68
class Comment < ::Model
53
- attributes :body
69
+ attributes :id , : body
54
70
associations :post , :author
55
71
56
72
# Uses a custom non-time-based cache key
57
73
def cache_key
58
74
"comment/#{ id } "
59
75
end
60
76
end
77
+ class CommentSerializer < ActiveModel ::Serializer
78
+ cache expires_in : 1 . day , skip_digest : true
79
+ attributes :id , :body
80
+ belongs_to :post
81
+ belongs_to :author
82
+ end
83
+
84
+ class Post < ::Model
85
+ attributes :id , :title , :body
86
+ associations :author , :comments , :blog
87
+ end
88
+ class PostSerializer < ActiveModel ::Serializer
89
+ cache key : 'post' , expires_in : 0.1 , skip_digest : true
90
+ attributes :id , :title , :body
91
+
92
+ has_many :comments
93
+ belongs_to :blog
94
+ belongs_to :author
95
+ end
96
+
97
+ class Role < ::Model
98
+ attributes :name , :description , :special_attribute
99
+ associations :author
100
+ end
101
+ class RoleSerializer < ActiveModel ::Serializer
102
+ cache only : [ :name , :slug ] , skip_digest : true
103
+ attributes :id , :name , :description
104
+ attribute :friendly_id , key : :slug
105
+ belongs_to :author
106
+
107
+ def friendly_id
108
+ "#{ object . name } -#{ object . id } "
109
+ end
110
+ end
111
+ class InheritedRoleSerializer < RoleSerializer
112
+ cache key : 'inherited_role' , only : [ :name , :special_attribute ]
113
+ attribute :special_attribute
114
+ end
61
115
62
116
setup do
63
117
cache_store . clear
64
118
@comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
65
- @post = Post . new ( title : 'New Post' , body : 'Body' )
119
+ @post = Post . new ( id : 'post' , title : 'New Post' , body : 'Body' )
66
120
@bio = Bio . new ( id : 1 , content : 'AMS Contributor' )
67
- @author = Author . new ( name : 'Joao M. D. Moura' )
68
- @blog = Blog . new ( id : 999 , name : 'Custom blog' , writer : @author , articles : [ ] )
121
+ @author = Author . new ( id : 'author' , name : 'Joao M. D. Moura' )
122
+ @blog = Blog . new ( id : 999 , name : 'Custom blog' , writer : @author )
69
123
@role = Role . new ( name : 'Great Author' )
70
124
@location = Location . new ( lat : '-23.550520' , lng : '-46.633309' )
71
125
@place = Place . new ( name : 'Amazing Place' )
@@ -325,12 +379,14 @@ def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attribut
325
379
326
380
def test_uses_file_digest_in_cache_key
327
381
render_object_with_cache ( @blog )
328
- key = "#{ @blog . cache_key } /#{ adapter . cache_key } /#{ ::Model ::FILE_DIGEST } "
382
+ file_digest = Digest ::MD5 . hexdigest ( File . open ( __FILE__ ) . read )
383
+ key = "#{ @blog . cache_key } /#{ adapter . cache_key } /#{ file_digest } "
329
384
assert_equal ( @blog_serializer . attributes , cache_store . fetch ( key ) )
330
385
end
331
386
332
387
def test_cache_digest_definition
333
- assert_equal ( ::Model ::FILE_DIGEST , @post_serializer . class . _cache_digest )
388
+ file_digest = Digest ::MD5 . hexdigest ( File . open ( __FILE__ ) . read )
389
+ assert_equal ( file_digest , @post_serializer . class . _cache_digest )
334
390
end
335
391
336
392
def test_object_cache_keys
@@ -532,7 +588,7 @@ def test_fragment_fetch_with_virtual_attributes
532
588
role_hash = role_serializer . fetch_attributes_fragment ( adapter_instance )
533
589
assert_equal ( role_hash , expected_result )
534
590
535
- role . attributes [ :id ] = 'this has been updated'
591
+ role . id = 'this has been updated'
536
592
role . name = 'this was cached'
537
593
538
594
role_hash = role_serializer . fetch_attributes_fragment ( adapter_instance )
0 commit comments