1
1
Rails . configuration . serializers = [ ]
2
- class AuthorSerializer < ActiveModel ::Serializer
2
+ class HasOneRelationshipSerializer < ActiveModel ::Serializer
3
3
attributes :id , :first_name , :last_name
4
4
5
- has_many :posts , embed : :ids
5
+ has_many :primary_resources , embed : :ids
6
6
has_one :bio
7
7
end
8
- Rails . configuration . serializers << AuthorSerializer
8
+ Rails . configuration . serializers << HasOneRelationshipSerializer
9
9
10
- class BlogSerializer < ActiveModel ::Serializer
10
+ class VirtualAttributeSerializer < ActiveModel ::Serializer
11
11
attributes :id , :name
12
12
end
13
- Rails . configuration . serializers << BlogSerializer
13
+ Rails . configuration . serializers << VirtualAttributeSerializer
14
14
15
- class CommentSerializer < ActiveModel ::Serializer
16
- attributes :id , :body , :updated_at
15
+ class HasManyRelationshipSerializer < ActiveModel ::Serializer
16
+ attributes :id , :body
17
17
18
- belongs_to :post
19
- belongs_to :author
18
+ belongs_to :primary_resource
19
+ belongs_to :has_one_relationship
20
20
end
21
- Rails . configuration . serializers << CommentSerializer
21
+ Rails . configuration . serializers << HasManyRelationshipSerializer
22
22
23
- class PostSerializer < ActiveModel ::Serializer
23
+ class PrimaryResourceSerializer < ActiveModel ::Serializer
24
24
attributes :id , :title , :body
25
25
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
29
29
30
- link ( :post_authors ) { 'https://example.com/post_authors ' }
30
+ link ( :primary_resource_has_one_relationships ) { 'https://example.com/primary_resource_has_one_relationships ' }
31
31
32
32
meta do
33
33
{
@@ -36,33 +36,33 @@ class PostSerializer < ActiveModel::Serializer
36
36
}
37
37
end
38
38
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 ' )
41
41
end
42
42
end
43
- Rails . configuration . serializers << PostSerializer
43
+ Rails . configuration . serializers << PrimaryResourceSerializer
44
44
45
- class CachingAuthorSerializer < AuthorSerializer
45
+ class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
46
46
cache key : 'writer' , skip_digest : true
47
47
end
48
- Rails . configuration . serializers << CachingAuthorSerializer
48
+ Rails . configuration . serializers << CachingHasOneRelationshipSerializer
49
49
50
- class CachingCommentSerializer < CommentSerializer
50
+ class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
51
51
cache expires_in : 1 . day , skip_digest : true
52
52
end
53
- Rails . configuration . serializers << CachingCommentSerializer
53
+ Rails . configuration . serializers << CachingHasManyRelationshipSerializer
54
54
55
55
# 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
58
58
59
59
attributes :id , :title , :body
60
60
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
64
64
65
- link ( :post_authors ) { 'https://example.com/post_authors ' }
65
+ link ( :primary_resource_has_one_relationships ) { 'https://example.com/primary_resource_has_one_relationships ' }
66
66
67
67
meta do
68
68
{
@@ -71,33 +71,33 @@ class CachingPostSerializer < ActiveModel::Serializer
71
71
}
72
72
end
73
73
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 ' )
76
76
end
77
77
end
78
- Rails . configuration . serializers << CachingPostSerializer
78
+ Rails . configuration . serializers << CachingPrimaryResourceSerializer
79
79
80
- class FragmentCachingAuthorSerializer < AuthorSerializer
80
+ class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
81
81
cache key : 'writer' , only : [ :first_name , :last_name ] , skip_digest : true
82
82
end
83
- Rails . configuration . serializers << FragmentCachingAuthorSerializer
83
+ Rails . configuration . serializers << FragmentCachingHasOneRelationshipSerializer
84
84
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
87
87
end
88
- Rails . configuration . serializers << CachingCommentSerializer
88
+ Rails . configuration . serializers << CachingHasManyRelationshipSerializer
89
89
90
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
91
+ class FragmentCachingPrimaryResourceSerializer < ActiveModel ::Serializer
92
+ cache key : 'primary_resource ' , expires_in : 0.1 , skip_digest : true
93
93
94
94
attributes :id , :title , :body
95
95
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
99
99
100
- link ( :post_authors ) { 'https://example.com/post_authors ' }
100
+ link ( :primary_resource_has_one_relationships ) { 'https://example.com/primary_resource_has_one_relationships ' }
101
101
102
102
meta do
103
103
{
@@ -106,11 +106,11 @@ class FragmentCachingPostSerializer < ActiveModel::Serializer
106
106
}
107
107
end
108
108
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 ' )
111
111
end
112
112
end
113
- Rails . configuration . serializers << FragmentCachingPostSerializer
113
+ Rails . configuration . serializers << FragmentCachingPrimaryResourceSerializer
114
114
115
115
if ENV [ 'ENABLE_ACTIVE_RECORD' ] == 'true'
116
116
require 'active_record'
@@ -119,48 +119,48 @@ def blog
119
119
ActiveRecord ::Schema . define do
120
120
self . verbose = false
121
121
122
- create_table :blogs , force : true do |t |
122
+ create_table :virtual_attributes , force : true do |t |
123
123
t . string :name
124
124
t . timestamps null : false
125
125
end
126
- create_table :authors , force : true do |t |
126
+ create_table :has_one_relationships , force : true do |t |
127
127
t . string :first_name
128
128
t . string :last_name
129
129
t . timestamps null : false
130
130
end
131
- create_table :posts , force : true do |t |
131
+ create_table :primary_resources , force : true do |t |
132
132
t . string :title
133
133
t . text :body
134
- t . references :author
135
- t . references :blog
134
+ t . references :has_one_relationship
135
+ t . references :virtual_attribute
136
136
t . timestamps null : false
137
137
end
138
- create_table :comments , force : true do |t |
138
+ create_table :has_many_relationships , force : true do |t |
139
139
t . text :body
140
- t . references :author
141
- t . references :post
140
+ t . references :has_one_relationship
141
+ t . references :primary_resource
142
142
t . timestamps null : false
143
143
end
144
144
end
145
145
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
149
149
end
150
150
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
154
154
end
155
155
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
160
160
end
161
161
162
- class Blog < ActiveRecord ::Base
163
- has_many :posts
162
+ class VirtualAttribute < ActiveRecord ::Base
163
+ has_many :primary_resources
164
164
end
165
165
else
166
166
# ActiveModelSerializers::Model is a convenient
@@ -201,19 +201,19 @@ def read_attribute_for_serialization(key)
201
201
end
202
202
end
203
203
204
- class Comment < BenchmarkModel
205
- attr_accessor :id , :body , :updated_at
204
+ class HasManyRelationship < BenchmarkModel
205
+ attr_accessor :id , :body
206
206
end
207
207
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
210
210
end
211
211
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
214
214
end
215
215
216
- class Blog < BenchmarkModel
216
+ class VirtualAttribute < BenchmarkModel
217
217
attr_accessor :id , :name
218
218
end
219
219
end
0 commit comments