@@ -13,7 +13,7 @@ class BlogSerializer < ActiveModel::Serializer
13
13
Rails . configuration . serializers << BlogSerializer
14
14
15
15
class CommentSerializer < ActiveModel ::Serializer
16
- attributes :id , :body
16
+ attributes :id , :body , :updated_at
17
17
18
18
belongs_to :post
19
19
belongs_to :author
@@ -43,7 +43,7 @@ def blog
43
43
Rails . configuration . serializers << PostSerializer
44
44
45
45
class CachingAuthorSerializer < AuthorSerializer
46
- cache key : 'writer' , only : [ :first_name , :last_name ] , skip_digest : true
46
+ cache key : 'writer' , skip_digest : true
47
47
end
48
48
Rails . configuration . serializers << CachingAuthorSerializer
49
49
@@ -52,14 +52,66 @@ class CachingCommentSerializer < CommentSerializer
52
52
end
53
53
Rails . configuration . serializers << CachingCommentSerializer
54
54
55
- class CachingPostSerializer < PostSerializer
55
+ # see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
56
+ class CachingPostSerializer < ActiveModel ::Serializer
56
57
cache key : 'post' , expires_in : 0.1 , skip_digest : true
58
+
59
+ attributes :id , :title , :body
60
+
57
61
belongs_to :blog , serializer : BlogSerializer
58
62
belongs_to :author , serializer : CachingAuthorSerializer
59
63
has_many :comments , serializer : CachingCommentSerializer
64
+
65
+ link ( :post_authors ) { 'https://example.com/post_authors' }
66
+
67
+ meta do
68
+ {
69
+ rating : 5 ,
70
+ favorite_count : 10
71
+ }
72
+ end
73
+
74
+ def blog
75
+ Blog . new ( id : 999 , name : 'Custom blog' )
76
+ end
60
77
end
61
78
Rails . configuration . serializers << CachingPostSerializer
62
79
80
+ class FragmentCachingAuthorSerializer < AuthorSerializer
81
+ cache key : 'writer' , only : [ :first_name , :last_name ] , skip_digest : true
82
+ end
83
+ Rails . configuration . serializers << FragmentCachingAuthorSerializer
84
+
85
+ class FragmentCachingCommentSerializer < CommentSerializer
86
+ cache expires_in : 1 . day , except : [ :updated_at ] , skip_digest : true
87
+ end
88
+ Rails . configuration . serializers << CachingCommentSerializer
89
+
90
+ # see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
91
+ class FragmentCachingPostSerializer < ActiveModel ::Serializer
92
+ cache key : 'post' , expires_in : 0.1 , skip_digest : true
93
+
94
+ attributes :id , :title , :body
95
+
96
+ belongs_to :blog , serializer : BlogSerializer
97
+ belongs_to :author , serializer : FragmentCachingAuthorSerializer
98
+ has_many :comments , serializer : FragmentCachingCommentSerializer
99
+
100
+ link ( :post_authors ) { 'https://example.com/post_authors' }
101
+
102
+ meta do
103
+ {
104
+ rating : 5 ,
105
+ favorite_count : 10
106
+ }
107
+ end
108
+
109
+ def blog
110
+ Blog . new ( id : 999 , name : 'Custom blog' )
111
+ end
112
+ end
113
+ Rails . configuration . serializers << FragmentCachingPostSerializer
114
+
63
115
if ENV [ 'ENABLE_ACTIVE_RECORD' ] == 'true'
64
116
require 'active_record'
65
117
@@ -150,7 +202,7 @@ def read_attribute_for_serialization(key)
150
202
end
151
203
152
204
class Comment < BenchmarkModel
153
- attr_accessor :id , :body
205
+ attr_accessor :id , :body , :updated_at
154
206
end
155
207
156
208
class Author < BenchmarkModel
0 commit comments