alba 3.5.0 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +5 -39
- data/lib/alba/association.rb +4 -1
- data/lib/alba/conditional_attribute.rb +11 -14
- data/lib/alba/layout.rb +8 -6
- data/lib/alba/railtie.rb +2 -2
- data/lib/alba/resource.rb +59 -15
- data/lib/alba/typed_attribute.rb +2 -0
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +51 -32
- metadata +4 -52
- data/.codeclimate.yml +0 -12
- data/.editorconfig +0 -10
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -26
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- data/.github/dependabot.yml +0 -12
- data/.github/workflows/codeql-analysis.yml +0 -70
- data/.github/workflows/lint.yml +0 -17
- data/.github/workflows/main.yml +0 -41
- data/.gitignore +0 -11
- data/.rubocop.yml +0 -156
- data/.yardopts +0 -4
- data/CODE_OF_CONDUCT.md +0 -132
- data/CONTRIBUTING.md +0 -30
- data/Gemfile +0 -39
- data/HACKING.md +0 -42
- data/Rakefile +0 -17
- data/SECURITY.md +0 -12
- data/alba.gemspec +0 -33
- data/benchmark/Gemfile +0 -26
- data/benchmark/README.md +0 -137
- data/benchmark/collection.rb +0 -297
- data/benchmark/prep.rb +0 -56
- data/benchmark/single_resource.rb +0 -300
- data/bin/console +0 -15
- data/bin/setup +0 -8
- data/codecov.yml +0 -8
- data/docs/migrate_from_active_model_serializers.md +0 -359
- data/docs/migrate_from_jbuilder.md +0 -237
- data/docs/rails.md +0 -56
- data/gemfiles/without_active_support.gemfile +0 -19
- data/gemfiles/without_oj.gemfile +0 -19
- data/logo/alba-card.png +0 -0
- data/logo/alba-sign.png +0 -0
- data/logo/alba-typography.png +0 -0
@@ -1,300 +0,0 @@
|
|
1
|
-
# Benchmark script to run varieties of JSON serializers
|
2
|
-
# Fetch Alba from local, otherwise fetch latest from RubyGems
|
3
|
-
|
4
|
-
require_relative 'prep'
|
5
|
-
|
6
|
-
# --- Alba serializers ---
|
7
|
-
|
8
|
-
require "alba"
|
9
|
-
|
10
|
-
class AlbaCommentResource
|
11
|
-
include ::Alba::Resource
|
12
|
-
attributes :id, :body
|
13
|
-
end
|
14
|
-
|
15
|
-
class AlbaPostResource
|
16
|
-
include ::Alba::Resource
|
17
|
-
attributes :id, :body
|
18
|
-
attribute :commenter_names do |post|
|
19
|
-
post.commenters.pluck(:name)
|
20
|
-
end
|
21
|
-
many :comments, resource: AlbaCommentResource
|
22
|
-
end
|
23
|
-
|
24
|
-
# --- ActiveModelSerializer serializers ---
|
25
|
-
|
26
|
-
require "active_model_serializers"
|
27
|
-
|
28
|
-
class AMSCommentSerializer < ActiveModel::Serializer
|
29
|
-
attributes :id, :body
|
30
|
-
end
|
31
|
-
|
32
|
-
class AMSPostSerializer < ActiveModel::Serializer
|
33
|
-
attributes :id, :body
|
34
|
-
attribute :commenter_names
|
35
|
-
has_many :comments, serializer: AMSCommentSerializer
|
36
|
-
|
37
|
-
def commenter_names
|
38
|
-
object.commenters.pluck(:name)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# --- Blueprint serializers ---
|
43
|
-
|
44
|
-
require "blueprinter"
|
45
|
-
|
46
|
-
class CommentBlueprint < Blueprinter::Base
|
47
|
-
fields :id, :body
|
48
|
-
end
|
49
|
-
|
50
|
-
class PostBlueprint < Blueprinter::Base
|
51
|
-
fields :id, :body, :commenter_names
|
52
|
-
association :comments, blueprint: CommentBlueprint
|
53
|
-
|
54
|
-
def commenter_names
|
55
|
-
commenters.pluck(:name)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# --- JBuilder serializers ---
|
60
|
-
|
61
|
-
require "jbuilder"
|
62
|
-
|
63
|
-
class Post
|
64
|
-
def to_builder
|
65
|
-
Jbuilder.new do |post|
|
66
|
-
post.call(self, :id, :body, :commenter_names, :comments)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def commenter_names
|
71
|
-
commenters.pluck(:name)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
class Comment
|
76
|
-
def to_builder
|
77
|
-
Jbuilder.new do |comment|
|
78
|
-
comment.call(self, :id, :body)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
# --- Jserializer serializers ---
|
84
|
-
|
85
|
-
require 'jserializer'
|
86
|
-
|
87
|
-
class JserializerCommentSerializer < Jserializer::Base
|
88
|
-
attributes :id, :body
|
89
|
-
end
|
90
|
-
|
91
|
-
class JserializerPostSerializer < Jserializer::Base
|
92
|
-
attributes :id, :body, :commenter_names
|
93
|
-
has_many :comments, serializer: JserializerCommentSerializer
|
94
|
-
def commenter_names
|
95
|
-
object.commenters.pluck(:name)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# --- Panko serializers ---
|
100
|
-
|
101
|
-
require "panko_serializer"
|
102
|
-
|
103
|
-
class PankoCommentSerializer < Panko::Serializer
|
104
|
-
attributes :id, :body
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
class PankoPostSerializer < Panko::Serializer
|
109
|
-
attributes :id, :body, :commenter_names
|
110
|
-
|
111
|
-
has_many :comments, serializer: PankoCommentSerializer
|
112
|
-
|
113
|
-
def commenter_names
|
114
|
-
object.commenters.pluck(:name)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
# --- Primalize serializers ---
|
119
|
-
|
120
|
-
class PrimalizeCommentResource < Primalize::Single
|
121
|
-
attributes id: integer, body: string
|
122
|
-
end
|
123
|
-
|
124
|
-
class PrimalizePostResource < Primalize::Single
|
125
|
-
alias post object
|
126
|
-
|
127
|
-
attributes(
|
128
|
-
id: integer,
|
129
|
-
body: string,
|
130
|
-
comments: array(primalize(PrimalizeCommentResource)),
|
131
|
-
commenter_names: array(string),
|
132
|
-
)
|
133
|
-
|
134
|
-
def commenter_names
|
135
|
-
post.commenters.pluck(:name)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# --- Representable serializers ---
|
140
|
-
|
141
|
-
require "representable"
|
142
|
-
|
143
|
-
class CommentRepresenter < Representable::Decorator
|
144
|
-
include Representable::JSON
|
145
|
-
|
146
|
-
property :id
|
147
|
-
property :body
|
148
|
-
end
|
149
|
-
|
150
|
-
class PostRepresenter < Representable::Decorator
|
151
|
-
include Representable::JSON
|
152
|
-
|
153
|
-
property :id
|
154
|
-
property :body
|
155
|
-
property :commenter_names
|
156
|
-
collection :comments
|
157
|
-
|
158
|
-
def commenter_names
|
159
|
-
commenters.pluck(:name)
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
# --- SimpleAMS serializers ---
|
164
|
-
|
165
|
-
require "simple_ams"
|
166
|
-
|
167
|
-
class SimpleAMSCommentSerializer
|
168
|
-
include SimpleAMS::DSL
|
169
|
-
|
170
|
-
attributes :id, :body
|
171
|
-
end
|
172
|
-
|
173
|
-
class SimpleAMSPostSerializer
|
174
|
-
include SimpleAMS::DSL
|
175
|
-
|
176
|
-
attributes :id, :body
|
177
|
-
attribute :commenter_names
|
178
|
-
has_many :comments, serializer: SimpleAMSCommentSerializer
|
179
|
-
|
180
|
-
def commenter_names
|
181
|
-
object.commenters.pluck(:name)
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
require 'turbostreamer'
|
186
|
-
TurboStreamer.set_default_encoder(:json, :oj)
|
187
|
-
|
188
|
-
class TurbostreamerSerializer
|
189
|
-
def initialize(post)
|
190
|
-
@post = post
|
191
|
-
end
|
192
|
-
|
193
|
-
def to_json
|
194
|
-
TurboStreamer.encode do |json|
|
195
|
-
json.object! do
|
196
|
-
json.extract! @post, :id, :body, :commenter_names
|
197
|
-
|
198
|
-
json.comments @post.comments do |comment|
|
199
|
-
json.object! do
|
200
|
-
json.extract! comment, :id, :body
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
# --- Test data creation ---
|
209
|
-
|
210
|
-
post = Post.create!(body: 'post')
|
211
|
-
user1 = User.create!(name: 'John')
|
212
|
-
user2 = User.create!(name: 'Jane')
|
213
|
-
post.comments.create!(commenter: user1, body: 'Comment1')
|
214
|
-
post.comments.create!(commenter: user2, body: 'Comment2')
|
215
|
-
post.reload
|
216
|
-
|
217
|
-
# --- Store the serializers in procs ---
|
218
|
-
|
219
|
-
alba = Proc.new { AlbaPostResource.new(post).serialize }
|
220
|
-
alba_inline = Proc.new do
|
221
|
-
Alba.serialize(post) do
|
222
|
-
attributes :id, :body
|
223
|
-
attribute :commenter_names do |post|
|
224
|
-
post.commenters.pluck(:name)
|
225
|
-
end
|
226
|
-
many :comments do
|
227
|
-
attributes :id, :body
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
ams = Proc.new { AMSPostSerializer.new(post, {}).to_json }
|
233
|
-
blueprinter = Proc.new { PostBlueprint.render(post) }
|
234
|
-
jbuilder = Proc.new { post.to_builder.target! }
|
235
|
-
jserializer = Proc.new { JserializerPostSerializer.new(post).to_json }
|
236
|
-
panko = proc { PankoPostSerializer.new.serialize_to_json(post) }
|
237
|
-
primalize = proc { PrimalizePostResource.new(post).to_json }
|
238
|
-
rails = Proc.new { ActiveSupport::JSON.encode(post.serializable_hash(include: :comments)) }
|
239
|
-
representable = Proc.new { PostRepresenter.new(post).to_json }
|
240
|
-
simple_ams = Proc.new { SimpleAMS::Renderer.new(post, serializer: SimpleAMSPostSerializer).to_json }
|
241
|
-
turbostreamer = Proc.new { TurbostreamerSerializer.new(post).to_json }
|
242
|
-
|
243
|
-
# --- Execute the serializers to check their output ---
|
244
|
-
|
245
|
-
puts "Serializer outputs ----------------------------------"
|
246
|
-
{
|
247
|
-
alba: alba,
|
248
|
-
alba_inline: alba_inline,
|
249
|
-
ams: ams,
|
250
|
-
blueprinter: blueprinter,
|
251
|
-
jbuilder: jbuilder, # different order
|
252
|
-
jserializer: jserializer,
|
253
|
-
panko: panko,
|
254
|
-
primalize: primalize,
|
255
|
-
rails: rails,
|
256
|
-
representable: representable,
|
257
|
-
simple_ams: simple_ams,
|
258
|
-
turbostreamer: turbostreamer
|
259
|
-
}.each do |name, serializer|
|
260
|
-
puts "#{name.to_s.ljust(24, ' ')} #{serializer.call}"
|
261
|
-
end
|
262
|
-
|
263
|
-
# --- Run the benchmarks ---
|
264
|
-
|
265
|
-
require 'benchmark/ips'
|
266
|
-
Benchmark.ips do |x|
|
267
|
-
x.report(:alba, &alba)
|
268
|
-
x.report(:alba_inline, &alba_inline)
|
269
|
-
x.report(:ams, &ams)
|
270
|
-
x.report(:blueprinter, &blueprinter)
|
271
|
-
x.report(:jbuilder, &jbuilder)
|
272
|
-
x.report(:jserializer, &jserializer)
|
273
|
-
x.report(:panko, &panko)
|
274
|
-
x.report(:primalize, &primalize)
|
275
|
-
x.report(:rails, &rails)
|
276
|
-
x.report(:representable, &representable)
|
277
|
-
x.report(:simple_ams, &simple_ams)
|
278
|
-
x.report(:turbostreamer, &turbostreamer)
|
279
|
-
|
280
|
-
x.compare!
|
281
|
-
end
|
282
|
-
|
283
|
-
|
284
|
-
require 'benchmark/memory'
|
285
|
-
Benchmark.memory do |x|
|
286
|
-
x.report(:alba, &alba)
|
287
|
-
x.report(:alba_inline, &alba_inline)
|
288
|
-
x.report(:ams, &ams)
|
289
|
-
x.report(:blueprinter, &blueprinter)
|
290
|
-
x.report(:jbuilder, &jbuilder)
|
291
|
-
x.report(:jserializer, &jserializer)
|
292
|
-
x.report(:panko, &panko)
|
293
|
-
x.report(:primalize, &primalize)
|
294
|
-
x.report(:rails, &rails)
|
295
|
-
x.report(:representable, &representable)
|
296
|
-
x.report(:simple_ams, &simple_ams)
|
297
|
-
x.report(:turbostreamer, &turbostreamer)
|
298
|
-
|
299
|
-
x.compare!
|
300
|
-
end
|
data/bin/console
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'bundler/setup'
|
5
|
-
require 'alba'
|
6
|
-
|
7
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
-
# with your gem easier. You can also use a different console, if you like.
|
9
|
-
|
10
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
-
# require "pry"
|
12
|
-
# Pry.start
|
13
|
-
|
14
|
-
require 'irb'
|
15
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
data/codecov.yml
DELETED
@@ -1,359 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Upgrading from ActiveModelSerializers
|
3
|
-
---
|
4
|
-
|
5
|
-
<!-- @format -->
|
6
|
-
|
7
|
-
This guide is aimed at helping ActiveModelSerializers users transition to Alba, and it consists of three parts:
|
8
|
-
|
9
|
-
1. Basic serialization
|
10
|
-
2. Complex serialization
|
11
|
-
3. Unsupported features
|
12
|
-
|
13
|
-
## Example class
|
14
|
-
|
15
|
-
Example class is inherited `ActiveRecord::Base`, because [serializing PORO with AMS is pretty hard](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/howto/serialize_poro.md).
|
16
|
-
|
17
|
-
```rb
|
18
|
-
class User < ActiveRecord::Base
|
19
|
-
# columns: id, created_at, updated_at
|
20
|
-
has_one :profile
|
21
|
-
has_many :articles
|
22
|
-
end
|
23
|
-
|
24
|
-
class Profile < ActiveRecord::Base
|
25
|
-
# columns: id, user_id, email, created_at, updated_at
|
26
|
-
belongs_to :user
|
27
|
-
end
|
28
|
-
|
29
|
-
class Article < ActiveRecord::Base
|
30
|
-
# columns: id, user_id, title, body, created_at, updated_at
|
31
|
-
belongs_to :user
|
32
|
-
end
|
33
|
-
```
|
34
|
-
|
35
|
-
## 1. Basic serialization
|
36
|
-
|
37
|
-
### #serializable_hash
|
38
|
-
|
39
|
-
- or #as_json, #to_json
|
40
|
-
|
41
|
-
#### ActiveModelSerializer
|
42
|
-
|
43
|
-
```rb
|
44
|
-
# Infer and use by "#{MODEL_NAME}Serializer" in app/serializers/user_serializer.rb
|
45
|
-
class UserSerializer < ActiveModel::Serializer
|
46
|
-
type :user
|
47
|
-
attributes :id, :created_at, :updated_at
|
48
|
-
end
|
49
|
-
|
50
|
-
# serialze
|
51
|
-
user = User.create!
|
52
|
-
ActiveModelSerializers::SerializableResource.new(
|
53
|
-
user
|
54
|
-
).serializable_hash
|
55
|
-
# => {
|
56
|
-
# user: {
|
57
|
-
# id: id,
|
58
|
-
# created_at: created_at,
|
59
|
-
# updated_at: updated_at
|
60
|
-
# }
|
61
|
-
# }
|
62
|
-
```
|
63
|
-
|
64
|
-
#### Alba
|
65
|
-
|
66
|
-
```rb
|
67
|
-
# Infer and use by "#{MODEL_NAME}Resource"
|
68
|
-
# In app/resources/user_resource.rb
|
69
|
-
class UserResource
|
70
|
-
include Alba::Resource
|
71
|
-
attributes :id, :created_at, :updated_at
|
72
|
-
end
|
73
|
-
|
74
|
-
# serialze
|
75
|
-
user = User.create!
|
76
|
-
UserResource.new(user).serializable_hash
|
77
|
-
# => {
|
78
|
-
# id: id,
|
79
|
-
# created_at: created_at,
|
80
|
-
# updated_at: updated_at,
|
81
|
-
# }
|
82
|
-
|
83
|
-
# If want `user key`
|
84
|
-
class UserResource
|
85
|
-
include Alba::Resource
|
86
|
-
root_key :user # Call root_key method like ActiveModel::Serializer#type
|
87
|
-
attributes :id, :created_at, :updated_at
|
88
|
-
end
|
89
|
-
|
90
|
-
# serialze
|
91
|
-
user = User.create!
|
92
|
-
JSON.parse UserResource.new(user).serialize # !!!!serializable_hash does not support root key!!! Must use JSON.parse and serialize
|
93
|
-
# => {
|
94
|
-
# "user"=>{
|
95
|
-
# "id"=>id,
|
96
|
-
# "created_at"=>created_at,
|
97
|
-
# "updated_at"=>updated_at
|
98
|
-
# }
|
99
|
-
# }
|
100
|
-
# If want symbolize keys with #deep_symbolize_keys in Rails
|
101
|
-
user = User.create!
|
102
|
-
JSON.parse(UserResource.new(user).serialize).deep_symbolize_keys
|
103
|
-
# => {
|
104
|
-
# user: {
|
105
|
-
# id: id,
|
106
|
-
# created_at: created_at,
|
107
|
-
# updated_at: updated_at
|
108
|
-
# }
|
109
|
-
# }
|
110
|
-
```
|
111
|
-
|
112
|
-
## 2. Complex serialization
|
113
|
-
|
114
|
-
### Serialize collections
|
115
|
-
|
116
|
-
#### ActiveModelSerializer
|
117
|
-
|
118
|
-
```rb
|
119
|
-
class UserSerializer < ActiveModel::Serializer
|
120
|
-
type :user
|
121
|
-
attributes :id, :created_at, :updated_at
|
122
|
-
end
|
123
|
-
3.times { User.create! }
|
124
|
-
users = User.limit 3
|
125
|
-
ActiveModelSerializers::SerializableResource.new(
|
126
|
-
users,
|
127
|
-
adapter: :attributes # Comment out this line if you want users key
|
128
|
-
# Want to specified key to call with root: args
|
129
|
-
).serializable_hash
|
130
|
-
# => [{:id=>1, :created_at=>created_at, :updated_at=>updated_at},
|
131
|
-
# {:id=>2, :created_at=>created_at, :updated_at=>updated_at},
|
132
|
-
# {:id=>3, :created_at=>created_at, :updated_at=>updated_at}]
|
133
|
-
```
|
134
|
-
|
135
|
-
#### Alba
|
136
|
-
|
137
|
-
```rb
|
138
|
-
class UserResource
|
139
|
-
include Alba::Resource
|
140
|
-
attributes :id, :created_at, :updated_at
|
141
|
-
end
|
142
|
-
3.times { User.create! }
|
143
|
-
users = User.limit 3
|
144
|
-
UserResource.new(users).serializable_hash
|
145
|
-
# =>[{:id=>1, :created_at=>created_at, :updated_at=>updated_at},
|
146
|
-
# {:id=>2, :created_at=>created_at, :updated_at=>updated_at},
|
147
|
-
# {:id=>3, :created_at=>created_at, :updated_at=>updated_at}]
|
148
|
-
# or
|
149
|
-
JSON.parse UserResource.new(users).serialize(root_key: :users)
|
150
|
-
# => {"users"=>
|
151
|
-
# [{"id"=>1, "created_at"=>created_at, "updated_at"=>updated_at},
|
152
|
-
# {"id"=>2, "created_at"=>created_at, "updated_at"=>updated_at},
|
153
|
-
# {"id"=>3, "created_at"=>created_at, "updated_at"=>updated_at}]}
|
154
|
-
```
|
155
|
-
|
156
|
-
### Nested serialization
|
157
|
-
|
158
|
-
#### ActiveModelSerializer
|
159
|
-
|
160
|
-
```rb
|
161
|
-
class ProfileSerializer < ActiveModel::Serializer
|
162
|
-
type :profile
|
163
|
-
attributes :email
|
164
|
-
end
|
165
|
-
|
166
|
-
class ArticleSerializer < ActiveModel::Serializer
|
167
|
-
type :article
|
168
|
-
attributes :title, :body
|
169
|
-
end
|
170
|
-
|
171
|
-
class UserSerializer < ActiveModel::Serializer
|
172
|
-
type :user
|
173
|
-
attributes :id, :created_at, :updated_at
|
174
|
-
has_one :profile, serializer: ProfileSerializer # For has_one relation
|
175
|
-
has_many :articles, serializer: ArticleSerializer # For has_many relation
|
176
|
-
end
|
177
|
-
user = User.create!
|
178
|
-
user.create_profile! email: email
|
179
|
-
user.articles.create! title: title, body: body
|
180
|
-
ActiveModelSerializers::SerializableResource.new(
|
181
|
-
user
|
182
|
-
).serializable_hash
|
183
|
-
# => {
|
184
|
-
# :user=> {
|
185
|
-
# :id=>1,
|
186
|
-
# :created_at=>created_at,
|
187
|
-
# :updated_at=>updated_at,
|
188
|
-
# :profile=> {
|
189
|
-
# :email=>email
|
190
|
-
# },
|
191
|
-
# :articles => [
|
192
|
-
# {
|
193
|
-
# :title=>title,
|
194
|
-
# :body=>body
|
195
|
-
# }
|
196
|
-
# ]
|
197
|
-
# }
|
198
|
-
# }
|
199
|
-
```
|
200
|
-
|
201
|
-
#### Alba
|
202
|
-
|
203
|
-
```rb
|
204
|
-
class ProfileResource
|
205
|
-
include Alba::Resource
|
206
|
-
root_key :profile
|
207
|
-
attributes :email
|
208
|
-
end
|
209
|
-
|
210
|
-
class ArticleResource
|
211
|
-
include Alba::Resource
|
212
|
-
root_key :article
|
213
|
-
attributes :title, :body
|
214
|
-
end
|
215
|
-
|
216
|
-
class UserResource
|
217
|
-
include Alba::Resource
|
218
|
-
root_key :user
|
219
|
-
attributes :id, :created_at, :updated_at
|
220
|
-
one :profile, resource: ProfileResource # For has_one relation
|
221
|
-
many :articles, resource: ArticleResource # For has_many relation
|
222
|
-
end
|
223
|
-
|
224
|
-
user = User.create!
|
225
|
-
user.craete_profile! email: email
|
226
|
-
user.articles.create! title: title, body: body
|
227
|
-
UserResource.new(user).serializable_hash
|
228
|
-
# => {
|
229
|
-
# :id=>1,
|
230
|
-
# :created_at=>created_at,
|
231
|
-
# :updated_at=>updated_at,
|
232
|
-
# :profile=> {
|
233
|
-
# :email=>email
|
234
|
-
# },
|
235
|
-
# :articles => [
|
236
|
-
# {
|
237
|
-
# :title=>title,
|
238
|
-
# :body=>body
|
239
|
-
# }
|
240
|
-
# ]
|
241
|
-
# }
|
242
|
-
```
|
243
|
-
|
244
|
-
### Serialize with custom serializer
|
245
|
-
|
246
|
-
#### ActiveModelSerializer
|
247
|
-
|
248
|
-
```rb
|
249
|
-
class CustomUserSerializer < ActiveModel::Serializer
|
250
|
-
type :user
|
251
|
-
attribute :email do
|
252
|
-
object.profile.email
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
# serialze
|
257
|
-
user = User.create!
|
258
|
-
user.craete_profile! email: email
|
259
|
-
ActiveModelSerializers::SerializableResource.new(
|
260
|
-
user,
|
261
|
-
serializer: ::CustomUserSerializer # Call with serializer arg
|
262
|
-
).serializable_hash
|
263
|
-
# => {
|
264
|
-
# user: {
|
265
|
-
# email: email
|
266
|
-
# }
|
267
|
-
# }
|
268
|
-
```
|
269
|
-
|
270
|
-
#### Alba
|
271
|
-
|
272
|
-
```rb
|
273
|
-
class CustomUserResource
|
274
|
-
include Alba::Resource
|
275
|
-
root_key :user
|
276
|
-
attribute :email do
|
277
|
-
object.profile.email
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
# serialze
|
282
|
-
user = User.create!
|
283
|
-
user.craete_profile! email: email
|
284
|
-
CustomUserResource.new(user).serializable_hash
|
285
|
-
# => {
|
286
|
-
# email: email
|
287
|
-
# }
|
288
|
-
```
|
289
|
-
|
290
|
-
### Passing arbitrary options to a serializer
|
291
|
-
|
292
|
-
#### ActiveModelSerializer
|
293
|
-
|
294
|
-
```rb
|
295
|
-
class UserSerializer < ApplicationSerializer
|
296
|
-
type :user
|
297
|
-
attributes :id, :created_at, :updated_at
|
298
|
-
attribute :custom_params do
|
299
|
-
pp instance_options
|
300
|
-
# => given_params: { a: :b }
|
301
|
-
instance_options # Access by instance_options method
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
|
-
# serialze
|
306
|
-
user = User.create!
|
307
|
-
ActiveModelSerializers::SerializableResource.new(
|
308
|
-
user,
|
309
|
-
given_params: { a: :b } # Give with your favorite keyword argument
|
310
|
-
).serializable_hash
|
311
|
-
# => {
|
312
|
-
# :id=>1,
|
313
|
-
# :created_at=>created_at,
|
314
|
-
# :updated_at=>updated_at,
|
315
|
-
# :custom_params=>{
|
316
|
-
# :given_params=>{
|
317
|
-
# :a=>:b
|
318
|
-
# }
|
319
|
-
# }
|
320
|
-
# }
|
321
|
-
```
|
322
|
-
|
323
|
-
#### Alba
|
324
|
-
|
325
|
-
```rb
|
326
|
-
class UserResource
|
327
|
-
include Alba::Resource
|
328
|
-
root_key :user
|
329
|
-
attributes :id, :created_at, :updated_at
|
330
|
-
attribute :custom_params do
|
331
|
-
pp params
|
332
|
-
# => { :a=>:b }
|
333
|
-
params
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
# serialze
|
338
|
-
user = User.create!
|
339
|
-
UserResource.new(
|
340
|
-
user,
|
341
|
-
params: { a: :b } # Give with :params keyword argument
|
342
|
-
).serializable_hash
|
343
|
-
# => {
|
344
|
-
# :id=>1,
|
345
|
-
# :created_at=>created_at,
|
346
|
-
# :updated_at=>updated_at,
|
347
|
-
# :custom_params=>{
|
348
|
-
# :a=>:b
|
349
|
-
# }
|
350
|
-
# }
|
351
|
-
```
|
352
|
-
|
353
|
-
## 3. Unsupported features
|
354
|
-
|
355
|
-
- [RelationshipLinks](https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/howto/add_relationship_links.md)
|
356
|
-
- [PaginationLinks](https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/howto/add_pagination_links.md)
|
357
|
-
- [Logging](https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/logging.md)
|
358
|
-
- [Caching](https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/caching.md)
|
359
|
-
- [Rendering](https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/rendering.md)
|