@@ -13,7 +13,7 @@ class BlogSerializer < ActiveModel::Serializer
1313Rails . configuration . serializers << BlogSerializer
1414
1515class CommentSerializer < ActiveModel ::Serializer
16- attributes :id , :body
16+ attributes :id , :body , :updated_at
1717
1818 belongs_to :post
1919 belongs_to :author
@@ -43,7 +43,7 @@ def blog
4343Rails . configuration . serializers << PostSerializer
4444
4545class CachingAuthorSerializer < AuthorSerializer
46- cache key : 'writer' , only : [ :first_name , :last_name ] , skip_digest : true
46+ cache key : 'writer' , skip_digest : true
4747end
4848Rails . configuration . serializers << CachingAuthorSerializer
4949
@@ -52,14 +52,66 @@ class CachingCommentSerializer < CommentSerializer
5252end
5353Rails . configuration . serializers << CachingCommentSerializer
5454
55- class CachingPostSerializer < PostSerializer
55+ # see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
56+ class CachingPostSerializer < ActiveModel ::Serializer
5657 cache key : 'post' , expires_in : 0.1 , skip_digest : true
58+
59+ attributes :id , :title , :body
60+
5761 belongs_to :blog , serializer : BlogSerializer
5862 belongs_to :author , serializer : CachingAuthorSerializer
5963 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
6077end
6178Rails . configuration . serializers << CachingPostSerializer
6279
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+
63115if ENV [ 'ENABLE_ACTIVE_RECORD' ] == 'true'
64116 require 'active_record'
65117
@@ -150,7 +202,7 @@ def read_attribute_for_serialization(key)
150202 end
151203
152204 class Comment < BenchmarkModel
153- attr_accessor :id , :body
205+ attr_accessor :id , :body , :updated_at
154206 end
155207
156208 class Author < BenchmarkModel
0 commit comments