alba 3.5.0 → 3.7.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 +27 -0
- data/README.md +96 -40
- 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 +90 -17
- data/lib/alba/typed_attribute.rb +2 -0
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +51 -32
- metadata +5 -53
- 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,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)
|
@@ -1,237 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Upgrading from Jbuilder
|
3
|
-
---
|
4
|
-
|
5
|
-
<!-- @format -->
|
6
|
-
|
7
|
-
This guide is aimed at helping Jbuilder users transition to Alba, and it consists of four parts:
|
8
|
-
|
9
|
-
1. Basic serialization
|
10
|
-
2. Complex serialization
|
11
|
-
3. Key transformation
|
12
|
-
4. Unsupported features
|
13
|
-
|
14
|
-
## Example class
|
15
|
-
|
16
|
-
This example will also be replaced by ActiveRecord.
|
17
|
-
|
18
|
-
```rb
|
19
|
-
class User
|
20
|
-
attr_reader :id, :created_at, :updated_at
|
21
|
-
attr_accessor :profile, :articles
|
22
|
-
|
23
|
-
def initialize(id)
|
24
|
-
@id = id
|
25
|
-
@created_at = Time.now
|
26
|
-
@updated_at = Time.now
|
27
|
-
@articles = []
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class Profile
|
32
|
-
attr_reader :email
|
33
|
-
|
34
|
-
def initialize(email)
|
35
|
-
@email = email
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
class Article
|
40
|
-
attr_accessor :title, :body
|
41
|
-
|
42
|
-
def initialize(title, body)
|
43
|
-
@title = title
|
44
|
-
@body = body
|
45
|
-
end
|
46
|
-
end
|
47
|
-
```
|
48
|
-
|
49
|
-
## 1. Basic serialization
|
50
|
-
|
51
|
-
#### Jbuilder
|
52
|
-
|
53
|
-
```rb
|
54
|
-
# show.json.jbuilder
|
55
|
-
# With block
|
56
|
-
@user = User.new(id)
|
57
|
-
json.user do |user|
|
58
|
-
user.id @user.id
|
59
|
-
user.created_at @user.created_at
|
60
|
-
user.updated_at @user.updated_at
|
61
|
-
end
|
62
|
-
# => '{"user":{"id":id, "created_at": created_at, "updated_at": updated_at}'
|
63
|
-
# or #extract!
|
64
|
-
json.extract! @user, :id, :created_at, :updated_at
|
65
|
-
# => '{"id":id, "created_at": created_at, "updated_at": updated_at}'
|
66
|
-
```
|
67
|
-
|
68
|
-
#### Alba
|
69
|
-
|
70
|
-
```rb
|
71
|
-
# With block
|
72
|
-
user = User.new(id)
|
73
|
-
Alba.serialize(user, root_key: :user) do
|
74
|
-
attributes :id, :created_at, :updated_at
|
75
|
-
end
|
76
|
-
# => '{"user":{"id":id, "created_at": created_at, "updated_at": updated_at}'
|
77
|
-
# or with resourceClass.
|
78
|
-
# Infer and use by "#{MODEL_NAME}Resource"
|
79
|
-
class UserResource
|
80
|
-
include Alba::Resource
|
81
|
-
root_key :user
|
82
|
-
attributes :id, :created_at, :updated_at
|
83
|
-
end
|
84
|
-
UserResource.new(user).serialize
|
85
|
-
# => '{"user":{"id":id, "created_at": created_at, "updated_at": updated_at}'
|
86
|
-
```
|
87
|
-
|
88
|
-
## 2. Complex serialization
|
89
|
-
|
90
|
-
### Serialize collections
|
91
|
-
|
92
|
-
#### Jbuilder
|
93
|
-
|
94
|
-
```rb
|
95
|
-
@users = ids.map { |id| User.new(id) }
|
96
|
-
# index.json.jbuilder
|
97
|
-
json.array! @users, :id, :created_at, :updated_at
|
98
|
-
# => '[{"id":id, "created_at": created_at, "updated_at": updated_at}, {"id":id, "created_at": created_at, "updated_at": updated_at}, {"id":id, "created_at": created_at, "updated_at": updated_at}]'
|
99
|
-
```
|
100
|
-
|
101
|
-
#### Alba
|
102
|
-
|
103
|
-
```rb
|
104
|
-
class UserResource
|
105
|
-
include Alba::Resource
|
106
|
-
root_key :user
|
107
|
-
attributes :id, :created_at, :updated_at
|
108
|
-
end
|
109
|
-
users = ids.map { |id| User.new(id) }
|
110
|
-
UserResource.new(users).serialize
|
111
|
-
# => '[{"id":id, "created_at": created_at, "updated_at": updated_at}, {"id":id, "created_at": created_at, "updated_at": updated_at}, {"id":id, "created_at": created_at, "updated_at": updated_at}]'
|
112
|
-
|
113
|
-
```
|
114
|
-
|
115
|
-
### Nested serialization
|
116
|
-
|
117
|
-
#### Jbuilder
|
118
|
-
|
119
|
-
```rb
|
120
|
-
# show.json.jbuilder
|
121
|
-
@user = User.new(id)
|
122
|
-
@user.profile = Profile.new(email)
|
123
|
-
@user.articles = [Article.new(title, body)]
|
124
|
-
json.user do |user|
|
125
|
-
user.id @user.id
|
126
|
-
user.created_at @user.created_at
|
127
|
-
user.updated_at @user.updated_at
|
128
|
-
json.profile do
|
129
|
-
json.email @user.profile.email
|
130
|
-
end
|
131
|
-
json.articles do
|
132
|
-
json.array! @user.articles, :title, :body
|
133
|
-
end
|
134
|
-
end
|
135
|
-
# => '{"user":{"id":id, "created_at": created_at, "updated_at": updated_at, "profile": {"email": email}, articles: [{"title": title, "body": body}]}'
|
136
|
-
# or #merge!
|
137
|
-
profile_hash = { profile: { email: @user.profile.email } }
|
138
|
-
articles_hash = { articles: @user.articles.map { |article| { title: article.title, body: article.body } } }
|
139
|
-
json.user do |user|
|
140
|
-
user.id @user.id
|
141
|
-
user.created_at @user.created_at
|
142
|
-
user.updated_at @user.updated_at
|
143
|
-
json.merge! profile_hash
|
144
|
-
json.merge! articles_hash
|
145
|
-
end
|
146
|
-
# => '{"user":{"id":id, "created_at": created_at, "updated_at": updated_at, "profile": {"email": email}, articles: [{"title": title, "body": body}]}'
|
147
|
-
# or #partial!
|
148
|
-
# profiles/_profile.json.jbuilder
|
149
|
-
json.profile do
|
150
|
-
json.email @profile.email
|
151
|
-
end
|
152
|
-
# articles/_article.json.jbuilder
|
153
|
-
json.extract! article, :title, :body
|
154
|
-
# user/show.json.jbuilder
|
155
|
-
json.user do |user|
|
156
|
-
user.id @user.id
|
157
|
-
user.created_at @user.created_at
|
158
|
-
user.updated_at @user.updated_at
|
159
|
-
json.partial! @user.profile, as: :profile
|
160
|
-
json.articles @user.articles do |article|
|
161
|
-
json.partial! article, partial: 'articles/article'
|
162
|
-
end
|
163
|
-
end
|
164
|
-
```
|
165
|
-
|
166
|
-
#### Alba
|
167
|
-
|
168
|
-
```rb
|
169
|
-
# With ResourceClass by each resources
|
170
|
-
class ProfileResource
|
171
|
-
include Alba::Resource
|
172
|
-
root_key :profile
|
173
|
-
attributes :email
|
174
|
-
end
|
175
|
-
class ArticleResource
|
176
|
-
include Alba::Resource
|
177
|
-
root_key :article
|
178
|
-
attributes :title, :body
|
179
|
-
end
|
180
|
-
class UserResource
|
181
|
-
include Alba::Resource
|
182
|
-
root_key :user
|
183
|
-
attributes :id, :created_at, :updated_at
|
184
|
-
one :profile, resource: ProfileResource
|
185
|
-
many :articles, resource: ArticleResource
|
186
|
-
end
|
187
|
-
user = User.new(id)
|
188
|
-
user.profile = Profile.new(email)
|
189
|
-
user.articles = [Article.new(title, body)]
|
190
|
-
UserResource.new(user).serialize
|
191
|
-
# => '{"user":{"id":id, "created_at": created_at, "updated_at": updated_at, "profile": {"email": email}, articles: [{"title": title, "body": body}]}'
|
192
|
-
|
193
|
-
# or #attribute
|
194
|
-
class UserResource
|
195
|
-
include Alba::Resource
|
196
|
-
root_key :user
|
197
|
-
attributes :id, :created_at, :updated_at
|
198
|
-
|
199
|
-
attribute :profile do
|
200
|
-
{
|
201
|
-
email: object.profile.email # Can access to received resource by #object method
|
202
|
-
}
|
203
|
-
end
|
204
|
-
|
205
|
-
attribute :articles do
|
206
|
-
object.articles.map do |article|
|
207
|
-
{
|
208
|
-
title: article.title,
|
209
|
-
body: article.body,
|
210
|
-
}
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
user = User.new(id)
|
215
|
-
user.profile = Profile.new(email)
|
216
|
-
UserResource.new(user).serialize
|
217
|
-
# => '{"user":{"id":id, "created_at": created_at, "updated_at": updated_at, "profile": {"email": email}, articles: [{"title": title, "body": body}]}'
|
218
|
-
```
|
219
|
-
|
220
|
-
## 3. Key transformation
|
221
|
-
|
222
|
-
See the README for more information, but it's possible to migrate the `Jbuilder.key_format!` behavior with the `transform_keys` macro.
|
223
|
-
|
224
|
-
```rb
|
225
|
-
class UserResource
|
226
|
-
include Alba::Resource
|
227
|
-
root_key :user
|
228
|
-
attributes :id, :created_at, :updated_at
|
229
|
-
|
230
|
-
transform_keys :lower_camel
|
231
|
-
end
|
232
|
-
```
|
233
|
-
|
234
|
-
## 4. Unsupported features
|
235
|
-
|
236
|
-
- Jbuilder#ignore_nil!
|
237
|
-
- Jbuilder#cache!
|
data/docs/rails.md
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Alba for Rails
|
3
|
-
author: OKURA Masafumi
|
4
|
-
---
|
5
|
-
|
6
|
-
# Alba for Rails
|
7
|
-
|
8
|
-
While Alba is NOT designed for Rails specifically, you can definitely use Alba with Rails. This document describes in detail how to use Alba with Rails to be more productive.
|
9
|
-
|
10
|
-
## Initializer
|
11
|
-
|
12
|
-
You might want to add some configurations to initializer file such as `alba.rb` with something like below:
|
13
|
-
|
14
|
-
```ruby
|
15
|
-
# alba.rb
|
16
|
-
Alba.backend = :active_support
|
17
|
-
Alba.inflector = :active_support
|
18
|
-
```
|
19
|
-
|
20
|
-
You can also use `:oj_rails` for backend if you prefer using Oj.
|
21
|
-
|
22
|
-
Alba 2.2 introduced new Rails integration so that you don't have to add initializer file for setting inflector. You still need to add initializer file if you want to set backend or configure inflector with something different from `active_support`.
|
23
|
-
|
24
|
-
## Rendering JSON
|
25
|
-
|
26
|
-
You can render JSON with Rails in two ways. One way is to pass JSON String.
|
27
|
-
|
28
|
-
```ruby
|
29
|
-
render json: FooResource.new(foo).serialize
|
30
|
-
```
|
31
|
-
|
32
|
-
But you can also render JSON passing `Alba::Resource` object. Rails automatically calls `to_json` on a resource.
|
33
|
-
|
34
|
-
```ruby
|
35
|
-
render json: FooResource.new(foo)
|
36
|
-
```
|
37
|
-
|
38
|
-
Note that almost all options given to this `render` are ignored. The only exceptions are `layout`, `prefixes`, `template` and `status`.
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
# This `only` option is ignored
|
42
|
-
render json: FooResource.new(foo), only: [:id]
|
43
|
-
|
44
|
-
# This is OK
|
45
|
-
render json: FooResource.new(foo), status: 200
|
46
|
-
```
|
47
|
-
|
48
|
-
### Shorthand for rendering JSON with Alba
|
49
|
-
|
50
|
-
Now you can render JSON with shorthand.
|
51
|
-
|
52
|
-
First, using `render json: serialize(target)` renders JSON for given target object. You can pass `with: SomeSerializer` option to render with `SomeSerializer` in this case. If you skip `with` option Alba tries to find the correct serialize automatically.
|
53
|
-
|
54
|
-
There is a shorter version: `render_serialized_json(target)`. It also accepts `with` option.
|
55
|
-
|
56
|
-
It's recommended to use `with` option now since it cannot automatically find correct serializers sometimes.
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source 'https://rubygems.org'
|
4
|
-
|
5
|
-
gem 'minitest', '~> 5.14' # For test
|
6
|
-
gem 'rake', '~> 13.0' # For test and automation
|
7
|
-
gem 'rubocop', '>= 0.79.0', require: false # For lint
|
8
|
-
gem 'rubocop-minitest', '~> 0.11.0', require: false # For lint
|
9
|
-
gem 'rubocop-performance', '~> 1.10.1', require: false # For lint
|
10
|
-
gem 'rubocop-rake', '>= 0.5.1', require: false # For lint
|
11
|
-
gem 'rubocop-sensible', '~> 0.3.0', require: false # For lint
|
12
|
-
gem 'simplecov', '~> 0.21.0', require: false # For test coverage
|
13
|
-
gem 'simplecov-cobertura', require: false # For test coverage
|
14
|
-
gem 'yard', require: false # For documentation
|
15
|
-
|
16
|
-
platforms :ruby do
|
17
|
-
gem 'oj', '~> 3.11', require: false # For backend
|
18
|
-
gem 'ruby-prof', require: false # For performance profiling
|
19
|
-
end
|
data/gemfiles/without_oj.gemfile
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source 'https://rubygems.org'
|
4
|
-
|
5
|
-
gem 'activesupport', require: false # For backend
|
6
|
-
gem 'minitest', '~> 5.14' # For test
|
7
|
-
gem 'rake', '~> 13.0' # For test and automation
|
8
|
-
gem 'rubocop', '>= 0.79.0', require: false # For lint
|
9
|
-
gem 'rubocop-minitest', '~> 0.11.0', require: false # For lint
|
10
|
-
gem 'rubocop-performance', '~> 1.10.1', require: false # For lint
|
11
|
-
gem 'rubocop-rake', '>= 0.5.1', require: false # For lint
|
12
|
-
gem 'rubocop-sensible', '~> 0.3.0', require: false # For lint
|
13
|
-
gem 'simplecov', '~> 0.21.0', require: false # For test coverage
|
14
|
-
gem 'simplecov-cobertura', require: false # For test coverage
|
15
|
-
gem 'yard', require: false # For documentation
|
16
|
-
|
17
|
-
platforms :ruby do
|
18
|
-
gem 'ruby-prof', require: false # For performance profiling
|
19
|
-
end
|
data/logo/alba-card.png
DELETED
Binary file
|
data/logo/alba-sign.png
DELETED
Binary file
|
data/logo/alba-typography.png
DELETED
Binary file
|