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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/README.md +5 -39
  4. data/lib/alba/association.rb +4 -1
  5. data/lib/alba/conditional_attribute.rb +11 -14
  6. data/lib/alba/layout.rb +8 -6
  7. data/lib/alba/railtie.rb +2 -2
  8. data/lib/alba/resource.rb +59 -15
  9. data/lib/alba/typed_attribute.rb +2 -0
  10. data/lib/alba/version.rb +1 -1
  11. data/lib/alba.rb +51 -32
  12. metadata +4 -52
  13. data/.codeclimate.yml +0 -12
  14. data/.editorconfig +0 -10
  15. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -26
  16. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  17. data/.github/dependabot.yml +0 -12
  18. data/.github/workflows/codeql-analysis.yml +0 -70
  19. data/.github/workflows/lint.yml +0 -17
  20. data/.github/workflows/main.yml +0 -41
  21. data/.gitignore +0 -11
  22. data/.rubocop.yml +0 -156
  23. data/.yardopts +0 -4
  24. data/CODE_OF_CONDUCT.md +0 -132
  25. data/CONTRIBUTING.md +0 -30
  26. data/Gemfile +0 -39
  27. data/HACKING.md +0 -42
  28. data/Rakefile +0 -17
  29. data/SECURITY.md +0 -12
  30. data/alba.gemspec +0 -33
  31. data/benchmark/Gemfile +0 -26
  32. data/benchmark/README.md +0 -137
  33. data/benchmark/collection.rb +0 -297
  34. data/benchmark/prep.rb +0 -56
  35. data/benchmark/single_resource.rb +0 -300
  36. data/bin/console +0 -15
  37. data/bin/setup +0 -8
  38. data/codecov.yml +0 -8
  39. data/docs/migrate_from_active_model_serializers.md +0 -359
  40. data/docs/migrate_from_jbuilder.md +0 -237
  41. data/docs/rails.md +0 -56
  42. data/gemfiles/without_active_support.gemfile +0 -19
  43. data/gemfiles/without_oj.gemfile +0 -19
  44. data/logo/alba-card.png +0 -0
  45. data/logo/alba-sign.png +0 -0
  46. data/logo/alba-typography.png +0 -0
@@ -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
@@ -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
Binary file