11Rails . configuration . serializers = [ ]
2- class AuthorSerializer < ActiveModel ::Serializer
2+ class HasOneRelationshipSerializer < ActiveModel ::Serializer
33 attributes :id , :first_name , :last_name
44
5- has_many :posts , embed : :ids
5+ has_many :primary_resources , embed : :ids
66 has_one :bio
77end
8- Rails . configuration . serializers << AuthorSerializer
8+ Rails . configuration . serializers << HasOneRelationshipSerializer
99
10- class BlogSerializer < ActiveModel ::Serializer
10+ class VirtualAttributeSerializer < ActiveModel ::Serializer
1111 attributes :id , :name
1212end
13- Rails . configuration . serializers << BlogSerializer
13+ Rails . configuration . serializers << VirtualAttributeSerializer
1414
15- class CommentSerializer < ActiveModel ::Serializer
16- attributes :id , :body , :updated_at
15+ class HasManyRelationshipSerializer < ActiveModel ::Serializer
16+ attributes :id , :body
1717
18- belongs_to :post
19- belongs_to :author
18+ belongs_to :primary_resource
19+ belongs_to :has_one_relationship
2020end
21- Rails . configuration . serializers << CommentSerializer
21+ Rails . configuration . serializers << HasManyRelationshipSerializer
2222
23- class PostSerializer < ActiveModel ::Serializer
23+ class PrimaryResourceSerializer < ActiveModel ::Serializer
2424 attributes :id , :title , :body
2525
26- has_many :comments , serializer : CommentSerializer
27- belongs_to :blog , serializer : BlogSerializer
28- belongs_to :author , serializer : AuthorSerializer
26+ has_many :has_many_relationships , serializer : HasManyRelationshipSerializer
27+ belongs_to :virtual_attribute , serializer : VirtualAttributeSerializer
28+ belongs_to :has_one_relationship , serializer : HasOneRelationshipSerializer
2929
30- link ( :post_authors ) { 'https://example.com/post_authors ' }
30+ link ( :primary_resource_has_one_relationships ) { 'https://example.com/primary_resource_has_one_relationships ' }
3131
3232 meta do
3333 {
@@ -36,33 +36,33 @@ class PostSerializer < ActiveModel::Serializer
3636 }
3737 end
3838
39- def blog
40- Blog . new ( id : 999 , name : 'Custom blog ' )
39+ def virtual_attribute
40+ VirtualAttribute . new ( id : 999 , name : 'Free-Range Virtual Attribute ' )
4141 end
4242end
43- Rails . configuration . serializers << PostSerializer
43+ Rails . configuration . serializers << PrimaryResourceSerializer
4444
45- class CachingAuthorSerializer < AuthorSerializer
45+ class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
4646 cache key : 'writer' , skip_digest : true
4747end
48- Rails . configuration . serializers << CachingAuthorSerializer
48+ Rails . configuration . serializers << CachingHasOneRelationshipSerializer
4949
50- class CachingCommentSerializer < CommentSerializer
50+ class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
5151 cache expires_in : 1 . day , skip_digest : true
5252end
53- Rails . configuration . serializers << CachingCommentSerializer
53+ Rails . configuration . serializers << CachingHasManyRelationshipSerializer
5454
5555# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
56- class CachingPostSerializer < ActiveModel ::Serializer
57- cache key : 'post ' , expires_in : 0.1 , skip_digest : true
56+ class CachingPrimaryResourceSerializer < ActiveModel ::Serializer
57+ cache key : 'primary_resource ' , expires_in : 0.1 , skip_digest : true
5858
5959 attributes :id , :title , :body
6060
61- belongs_to :blog , serializer : BlogSerializer
62- belongs_to :author , serializer : CachingAuthorSerializer
63- has_many :comments , serializer : CachingCommentSerializer
61+ belongs_to :virtual_attribute , serializer : VirtualAttributeSerializer
62+ belongs_to :has_one_relationship , serializer : CachingHasOneRelationshipSerializer
63+ has_many :has_many_relationships , serializer : CachingHasManyRelationshipSerializer
6464
65- link ( :post_authors ) { 'https://example.com/post_authors ' }
65+ link ( :primary_resource_has_one_relationships ) { 'https://example.com/primary_resource_has_one_relationships ' }
6666
6767 meta do
6868 {
@@ -71,33 +71,33 @@ class CachingPostSerializer < ActiveModel::Serializer
7171 }
7272 end
7373
74- def blog
75- Blog . new ( id : 999 , name : 'Custom blog ' )
74+ def virtual_attribute
75+ VirtualAttribute . new ( id : 999 , name : 'Free-Range Virtual Attribute ' )
7676 end
7777end
78- Rails . configuration . serializers << CachingPostSerializer
78+ Rails . configuration . serializers << CachingPrimaryResourceSerializer
7979
80- class FragmentCachingAuthorSerializer < AuthorSerializer
80+ class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
8181 cache key : 'writer' , only : [ :first_name , :last_name ] , skip_digest : true
8282end
83- Rails . configuration . serializers << FragmentCachingAuthorSerializer
83+ Rails . configuration . serializers << FragmentCachingHasOneRelationshipSerializer
8484
85- class FragmentCachingCommentSerializer < CommentSerializer
86- cache expires_in : 1 . day , except : [ :updated_at ] , skip_digest : true
85+ class FragmentCachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
86+ cache expires_in : 1 . day , except : [ :body ] , skip_digest : true
8787end
88- Rails . configuration . serializers << CachingCommentSerializer
88+ Rails . configuration . serializers << CachingHasManyRelationshipSerializer
8989
9090# 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
91+ class FragmentCachingPrimaryResourceSerializer < ActiveModel ::Serializer
92+ cache key : 'primary_resource ' , expires_in : 0.1 , skip_digest : true
9393
9494 attributes :id , :title , :body
9595
96- belongs_to :blog , serializer : BlogSerializer
97- belongs_to :author , serializer : FragmentCachingAuthorSerializer
98- has_many :comments , serializer : FragmentCachingCommentSerializer
96+ belongs_to :virtual_attribute , serializer : VirtualAttributeSerializer
97+ belongs_to :has_one_relationship , serializer : FragmentCachingHasOneRelationshipSerializer
98+ has_many :has_many_relationships , serializer : FragmentCachingHasManyRelationshipSerializer
9999
100- link ( :post_authors ) { 'https://example.com/post_authors ' }
100+ link ( :primary_resource_has_one_relationships ) { 'https://example.com/primary_resource_has_one_relationships ' }
101101
102102 meta do
103103 {
@@ -106,11 +106,11 @@ class FragmentCachingPostSerializer < ActiveModel::Serializer
106106 }
107107 end
108108
109- def blog
110- Blog . new ( id : 999 , name : 'Custom blog ' )
109+ def virtual_attribute
110+ VirtualAttribute . new ( id : 999 , name : 'Free-Range Virtual Attribute ' )
111111 end
112112end
113- Rails . configuration . serializers << FragmentCachingPostSerializer
113+ Rails . configuration . serializers << FragmentCachingPrimaryResourceSerializer
114114
115115if ENV [ 'ENABLE_ACTIVE_RECORD' ] == 'true'
116116 require 'active_record'
@@ -119,48 +119,48 @@ def blog
119119 ActiveRecord ::Schema . define do
120120 self . verbose = false
121121
122- create_table :blogs , force : true do |t |
122+ create_table :virtual_attributes , force : true do |t |
123123 t . string :name
124124 t . timestamps null : false
125125 end
126- create_table :authors , force : true do |t |
126+ create_table :has_one_relationships , force : true do |t |
127127 t . string :first_name
128128 t . string :last_name
129129 t . timestamps null : false
130130 end
131- create_table :posts , force : true do |t |
131+ create_table :primary_resources , force : true do |t |
132132 t . string :title
133133 t . text :body
134- t . references :author
135- t . references :blog
134+ t . references :has_one_relationship
135+ t . references :virtual_attribute
136136 t . timestamps null : false
137137 end
138- create_table :comments , force : true do |t |
138+ create_table :has_many_relationships , force : true do |t |
139139 t . text :body
140- t . references :author
141- t . references :post
140+ t . references :has_one_relationship
141+ t . references :primary_resource
142142 t . timestamps null : false
143143 end
144144 end
145145
146- class Comment < ActiveRecord ::Base
147- belongs_to :author
148- belongs_to :post
146+ class HasManyRelationship < ActiveRecord ::Base
147+ belongs_to :has_one_relationship
148+ belongs_to :primary_resource
149149 end
150150
151- class Author < ActiveRecord ::Base
152- has_many :posts
153- has_many :comments
151+ class HasOneRelationship < ActiveRecord ::Base
152+ has_many :primary_resources
153+ has_many :has_many_relationships
154154 end
155155
156- class Post < ActiveRecord ::Base
157- has_many :comments
158- belongs_to :author
159- belongs_to :blog
156+ class PrimaryResource < ActiveRecord ::Base
157+ has_many :has_many_relationships
158+ belongs_to :has_one_relationship
159+ belongs_to :virtual_attribute
160160 end
161161
162- class Blog < ActiveRecord ::Base
163- has_many :posts
162+ class VirtualAttribute < ActiveRecord ::Base
163+ has_many :primary_resources
164164 end
165165else
166166 # ActiveModelSerializers::Model is a convenient
@@ -201,19 +201,19 @@ def read_attribute_for_serialization(key)
201201 end
202202 end
203203
204- class Comment < BenchmarkModel
205- attr_accessor :id , :body , :updated_at
204+ class HasManyRelationship < BenchmarkModel
205+ attr_accessor :id , :body
206206 end
207207
208- class Author < BenchmarkModel
209- attr_accessor :id , :first_name , :last_name , :posts
208+ class HasOneRelationship < BenchmarkModel
209+ attr_accessor :id , :first_name , :last_name , :primary_resources
210210 end
211211
212- class Post < BenchmarkModel
213- attr_accessor :id , :title , :body , :comments , :blog , :author
212+ class PrimaryResource < BenchmarkModel
213+ attr_accessor :id , :title , :body , :has_many_relationships , :virtual_attribute , :has_one_relationship
214214 end
215215
216- class Blog < BenchmarkModel
216+ class VirtualAttribute < BenchmarkModel
217217 attr_accessor :id , :name
218218 end
219219end
0 commit comments