agi_active_model_serializers 0.10.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +35 -0
- data/.rubocop.yml +102 -0
- data/.simplecov +110 -0
- data/.travis.yml +51 -0
- data/CHANGELOG.md +612 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +56 -0
- data/MIT-LICENSE +22 -0
- data/README.md +307 -0
- data/Rakefile +103 -0
- data/active_model_serializers.gemspec +63 -0
- data/appveyor.yml +24 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +247 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +169 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +279 -0
- data/docs/general/serializers.md +461 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +137 -0
- data/docs/howto/add_root_key.md +55 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +65 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +32 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +144 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +66 -0
- data/lib/active_model/serializable_resource.rb +11 -0
- data/lib/active_model/serializer.rb +231 -0
- data/lib/active_model/serializer/adapter.rb +24 -0
- data/lib/active_model/serializer/adapter/attributes.rb +15 -0
- data/lib/active_model/serializer/adapter/base.rb +18 -0
- data/lib/active_model/serializer/adapter/json.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +15 -0
- data/lib/active_model/serializer/adapter/null.rb +15 -0
- data/lib/active_model/serializer/array_serializer.rb +12 -0
- data/lib/active_model/serializer/association.rb +34 -0
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_serializer.rb +87 -0
- data/lib/active_model/serializer/concerns/associations.rb +102 -0
- data/lib/active_model/serializer/concerns/attributes.rb +82 -0
- data/lib/active_model/serializer/concerns/caching.rb +292 -0
- data/lib/active_model/serializer/concerns/configuration.rb +59 -0
- data/lib/active_model/serializer/concerns/links.rb +35 -0
- data/lib/active_model/serializer/concerns/meta.rb +29 -0
- data/lib/active_model/serializer/concerns/type.rb +25 -0
- data/lib/active_model/serializer/error_serializer.rb +14 -0
- data/lib/active_model/serializer/errors_serializer.rb +32 -0
- data/lib/active_model/serializer/field.rb +90 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +7 -0
- data/lib/active_model/serializer/has_one_reflection.rb +7 -0
- data/lib/active_model/serializer/lint.rb +150 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +163 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/version.rb +5 -0
- data/lib/active_model_serializers.rb +53 -0
- data/lib/active_model_serializers/adapter.rb +98 -0
- data/lib/active_model_serializers/adapter/attributes.rb +13 -0
- data/lib/active_model_serializers/adapter/base.rb +83 -0
- data/lib/active_model_serializers/adapter/json.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api.rb +517 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +69 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +51 -0
- data/lib/active_model_serializers/adapter/null.rb +9 -0
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deprecate.rb +54 -0
- data/lib/active_model_serializers/deserialization.rb +15 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/logging.rb +122 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +71 -0
- data/lib/active_model_serializers/railtie.rb +48 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +78 -0
- data/lib/active_model_serializers/serializable_resource.rb +82 -0
- data/lib/active_model_serializers/serialization_context.rb +39 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers/test/schema.rb +138 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +15 -0
- data/lib/grape/active_model_serializers.rb +16 -0
- data/lib/grape/formatters/active_model_serializers.rb +32 -0
- data/lib/grape/helpers/active_model_serializers.rb +17 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +135 -0
- data/test/action_controller/json/include_test.rb +246 -0
- data/test/action_controller/json_api/deserialization_test.rb +112 -0
- data/test/action_controller/json_api/errors_test.rb +40 -0
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +202 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/action_controller/json_api/transform_test.rb +189 -0
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +229 -0
- data/test/action_controller/serialization_test.rb +472 -0
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/json_pointer_test.rb +22 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +69 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +63 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +71 -0
- data/test/active_model_serializers/test/schema_test.rb +131 -0
- data/test/active_model_serializers/test/serializer_test.rb +62 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/attributes_test.rb +43 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +45 -0
- data/test/adapter/json/collection_test.rb +104 -0
- data/test/adapter/json/has_many_test.rb +45 -0
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +155 -0
- data/test/adapter/json_api/collection_test.rb +96 -0
- data/test/adapter/json_api/errors_test.rb +76 -0
- data/test/adapter/json_api/fields_test.rb +96 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +43 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
- data/test/adapter/json_api/has_many_test.rb +165 -0
- data/test/adapter/json_api/has_one_test.rb +80 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +168 -0
- data/test/adapter/json_api/json_api_test.rb +33 -0
- data/test/adapter/json_api/linked_test.rb +413 -0
- data/test/adapter/json_api/links_test.rb +95 -0
- data/test/adapter/json_api/pagination_links_test.rb +193 -0
- data/test/adapter/json_api/parse_test.rb +137 -0
- data/test/adapter/json_api/relationship_test.rb +397 -0
- data/test/adapter/json_api/resource_identifier_test.rb +110 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
- data/test/adapter/json_api/transform_test.rb +512 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +46 -0
- data/test/adapter/null_test.rb +22 -0
- data/test/adapter/polymorphic_test.rb +171 -0
- data/test/adapter_test.rb +67 -0
- data/test/array_serializer_test.rb +22 -0
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +119 -0
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +45 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +83 -0
- data/test/benchmark/fixtures.rb +219 -0
- data/test/cache_test.rb +595 -0
- data/test/collection_serializer_test.rb +123 -0
- data/test/fixtures/active_record.rb +113 -0
- data/test/fixtures/poro.rb +232 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +74 -0
- data/test/grape_test.rb +178 -0
- data/test/lint_test.rb +49 -0
- data/test/logger_test.rb +20 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +79 -0
- data/test/serializers/association_macros_test.rb +37 -0
- data/test/serializers/associations_test.rb +383 -0
- data/test/serializers/attribute_test.rb +153 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +202 -0
- data/test/serializers/options_test.rb +32 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/serializers/serializer_for_test.rb +136 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +82 -0
- data/test/support/rails5_shims.rb +53 -0
- data/test/support/rails_app.rb +36 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +71 -0
- data/test/test_helper.rb +58 -0
- metadata +602 -0
@@ -0,0 +1,461 @@
|
|
1
|
+
[Back to Guides](../README.md)
|
2
|
+
|
3
|
+
# Serializers
|
4
|
+
|
5
|
+
Given a serializer class:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class SomeSerializer < ActiveModel::Serializer
|
9
|
+
end
|
10
|
+
```
|
11
|
+
|
12
|
+
The following methods may be defined in it:
|
13
|
+
|
14
|
+
### Attributes
|
15
|
+
|
16
|
+
#### ::attributes
|
17
|
+
|
18
|
+
Serialization of the resource `title` and `body`
|
19
|
+
|
20
|
+
| In Serializer | #attributes |
|
21
|
+
|---------------------------- |-------------|
|
22
|
+
| `attributes :title, :body` | `{ title: 'Some Title', body: 'Some Body' }`
|
23
|
+
| `attributes :title, :body`<br>`def body "Special #{object.body}" end` | `{ title: 'Some Title', body: 'Special Some Body' }`
|
24
|
+
|
25
|
+
|
26
|
+
#### ::attribute
|
27
|
+
|
28
|
+
Serialization of the resource `title`
|
29
|
+
|
30
|
+
| In Serializer | #attributes |
|
31
|
+
|---------------------------- |-------------|
|
32
|
+
| `attribute :title` | `{ title: 'Some Title' } `
|
33
|
+
| `attribute :title, key: :name` | `{ name: 'Some Title' } `
|
34
|
+
| `attribute(:title) { 'A Different Title'}` | `{ title: 'A Different Title' } `
|
35
|
+
| `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
|
36
|
+
|
37
|
+
An `if` or `unless` option can make an attribute conditional. It takes a symbol of a method name on the serializer, or a lambda literal.
|
38
|
+
|
39
|
+
e.g.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
attribute :private_data, if: :is_current_user?
|
43
|
+
attribute :another_private_data, if: -> { scope.admin? }
|
44
|
+
|
45
|
+
def is_current_user?
|
46
|
+
object.id == current_user.id
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
### Associations
|
51
|
+
|
52
|
+
The interface for associations is, generically:
|
53
|
+
|
54
|
+
> `association_type(association_name, options, &block)`
|
55
|
+
|
56
|
+
Where:
|
57
|
+
|
58
|
+
- `association_type` may be `has_one`, `has_many`, `belongs_to`.
|
59
|
+
- `association_name` is a method name the serializer calls.
|
60
|
+
- optional: `options` may be:
|
61
|
+
- `key:` The name used for the serialized association.
|
62
|
+
- `serializer:`
|
63
|
+
- `if:`
|
64
|
+
- `unless:`
|
65
|
+
- `virtual_value:`
|
66
|
+
- `polymorphic:` defines if polymorphic relation type should be nested in serialized association.
|
67
|
+
- optional: `&block` is a context that returns the association's attributes.
|
68
|
+
- prevents `association_name` method from being called.
|
69
|
+
- return value of block is used as the association value.
|
70
|
+
- yields the `serializer` to the block.
|
71
|
+
- `include_data false` prevents the `data` key from being rendered in the JSON API relationship.
|
72
|
+
|
73
|
+
#### ::has_one
|
74
|
+
|
75
|
+
e.g.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
has_one :bio
|
79
|
+
has_one :blog, key: :site
|
80
|
+
has_one :maker, virtual_value: { id: 1 }
|
81
|
+
|
82
|
+
has_one :blog do |serializer|
|
83
|
+
serializer.cached_blog
|
84
|
+
end
|
85
|
+
|
86
|
+
def cached_blog
|
87
|
+
cache_store.fetch("cached_blog:#{object.updated_at}") do
|
88
|
+
Blog.find(object.blog_id)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
has_one :blog, if: :show_blog?
|
95
|
+
# you can also use a string or lambda
|
96
|
+
# has_one :blog, if: 'scope.admin?'
|
97
|
+
# has_one :blog, if: -> (serializer) { serializer.scope.admin? }
|
98
|
+
# has_one :blog, if: -> { scope.admin? }
|
99
|
+
|
100
|
+
def show_blog?
|
101
|
+
scope.admin?
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
#### ::has_many
|
106
|
+
|
107
|
+
e.g.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
has_many :comments
|
111
|
+
has_many :comments, key: :reviews
|
112
|
+
has_many :comments, serializer: CommentPreviewSerializer
|
113
|
+
has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
|
114
|
+
has_many :comments, key: :last_comments do
|
115
|
+
last(1)
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
#### ::belongs_to
|
120
|
+
|
121
|
+
e.g.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
belongs_to :author, serializer: AuthorPreviewSerializer
|
125
|
+
belongs_to :author, key: :writer
|
126
|
+
belongs_to :post
|
127
|
+
belongs_to :blog
|
128
|
+
def blog
|
129
|
+
Blog.new(id: 999, name: 'Custom blog')
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
### Polymorphic Relationships
|
134
|
+
|
135
|
+
Polymorphic relationships are serialized by specifying the relationship, like any other association. For example:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
class PictureSerializer < ActiveModel::Serializer
|
139
|
+
has_one :imageable
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
You can specify the serializers by [overriding serializer_for](serializers.md#overriding-association-serializer-lookup). For more context about polymorphic relationships, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter.
|
144
|
+
|
145
|
+
### Caching
|
146
|
+
|
147
|
+
#### ::cache
|
148
|
+
|
149
|
+
e.g.
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
cache key: 'post', expires_in: 0.1, skip_digest: true
|
153
|
+
cache expires_in: 1.day, skip_digest: true
|
154
|
+
cache key: 'writer', skip_digest: true
|
155
|
+
cache only: [:name], skip_digest: true
|
156
|
+
cache except: [:content], skip_digest: true
|
157
|
+
cache key: 'blog'
|
158
|
+
cache only: [:id]
|
159
|
+
```
|
160
|
+
|
161
|
+
#### #cache_key
|
162
|
+
|
163
|
+
e.g.
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
# Uses a custom non-time-based cache key
|
167
|
+
def cache_key
|
168
|
+
"#{self.class.name.downcase}/#{self.id}"
|
169
|
+
end
|
170
|
+
```
|
171
|
+
|
172
|
+
### Other
|
173
|
+
|
174
|
+
#### ::type
|
175
|
+
|
176
|
+
When using the `:json_api` adapter, the `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
|
177
|
+
|
178
|
+
When using the `:json` adapter, the `::type` method defines the name of the root element.
|
179
|
+
|
180
|
+
It either takes a `String` or `Symbol` as parameter.
|
181
|
+
|
182
|
+
Note: This method is useful only when using the `:json_api` or `:json` adapter.
|
183
|
+
|
184
|
+
Examples:
|
185
|
+
```ruby
|
186
|
+
class UserProfileSerializer < ActiveModel::Serializer
|
187
|
+
type 'profile'
|
188
|
+
|
189
|
+
attribute :name
|
190
|
+
end
|
191
|
+
class AuthorProfileSerializer < ActiveModel::Serializer
|
192
|
+
type :profile
|
193
|
+
|
194
|
+
attribute :name
|
195
|
+
end
|
196
|
+
```
|
197
|
+
|
198
|
+
With the `:json_api` adapter, the previous serializers would be rendered as:
|
199
|
+
|
200
|
+
``` json
|
201
|
+
{
|
202
|
+
"data": {
|
203
|
+
"id": "1",
|
204
|
+
"type": "profile",
|
205
|
+
"attributes": {
|
206
|
+
"name": "Julia"
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
```
|
211
|
+
|
212
|
+
With the `:json` adapter, the previous serializer would be rendered as:
|
213
|
+
|
214
|
+
``` json
|
215
|
+
{
|
216
|
+
"profile": {
|
217
|
+
"name": "Julia"
|
218
|
+
}
|
219
|
+
}
|
220
|
+
```
|
221
|
+
|
222
|
+
#### ::link
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
link :self do
|
226
|
+
href "https://example.com/link_author/#{object.id}"
|
227
|
+
end
|
228
|
+
link :author { link_author_url(object) }
|
229
|
+
link :link_authors { link_authors_url }
|
230
|
+
link :other, 'https://example.com/resource'
|
231
|
+
link :posts { link_author_posts_url(object) }
|
232
|
+
```
|
233
|
+
|
234
|
+
#### #object
|
235
|
+
|
236
|
+
The object being serialized.
|
237
|
+
|
238
|
+
#### #root
|
239
|
+
|
240
|
+
Resource root which is included in `JSON` adapter. As you can see at [Adapters Document](adapters.md), `Attribute` adapter (default) and `JSON API` adapter does not include root at top level.
|
241
|
+
By default, the resource root comes from the `model_name` of the serialized object's class.
|
242
|
+
|
243
|
+
There are several ways to specify root:
|
244
|
+
* [Overriding the root key](rendering.md#overriding-the-root-key)
|
245
|
+
* [Setting `type`](serializers.md#type)
|
246
|
+
* Specifying the `root` option, e.g. `root: 'specific_name'`, during the serializer's initialization:
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
ActiveModelSerializers::SerializableResource.new(foo, root: 'bar')
|
250
|
+
```
|
251
|
+
|
252
|
+
#### #scope
|
253
|
+
|
254
|
+
Allows you to include in the serializer access to an external method.
|
255
|
+
|
256
|
+
It's intended to provide an authorization context to the serializer, so that
|
257
|
+
you may e.g. show an admin all comments on a post, else only published comments.
|
258
|
+
|
259
|
+
- `scope` is a method on the serializer instance that comes from `options[:scope]`. It may be nil.
|
260
|
+
- `scope_name` is an option passed to the new serializer (`options[:scope_name]`). The serializer
|
261
|
+
defines a method with that name that calls the `scope`, e.g. `def current_user; scope; end`.
|
262
|
+
Note: it does not define the method if the serializer instance responds to it.
|
263
|
+
|
264
|
+
That's a lot of words, so here's some examples:
|
265
|
+
|
266
|
+
First, let's assume the serializer is instantiated in the controller, since that's the usual scenario.
|
267
|
+
We'll refer to the serialization context as `controller`.
|
268
|
+
|
269
|
+
| options | `Serializer#scope` | method definition |
|
270
|
+
|-------- | ------------------|--------------------|
|
271
|
+
| `scope: current_user, scope_name: :current_user` | `current_user` | `Serializer#current_user` calls `controller.current_user`
|
272
|
+
| `scope: view_context, scope_name: :view_context` | `view_context` | `Serializer#view_context` calls `controller.view_context`
|
273
|
+
|
274
|
+
We can take advantage of the scope to customize the objects returned based
|
275
|
+
on the current user (scope).
|
276
|
+
|
277
|
+
For example, we can limit the posts the current user sees to those they created:
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
class PostSerializer < ActiveModel::Serializer
|
281
|
+
attributes :id, :title, :body
|
282
|
+
|
283
|
+
# scope comments to those created_by the current user
|
284
|
+
has_many :comments do
|
285
|
+
object.comments.where(created_by: current_user)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
290
|
+
Whether you write the method as above or as `object.comments.where(created_by: scope)`
|
291
|
+
is a matter of preference (assuming `scope_name` has been set).
|
292
|
+
|
293
|
+
##### Controller Authorization Context
|
294
|
+
|
295
|
+
In the controller, the scope/scope_name options are equal to
|
296
|
+
the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20),
|
297
|
+
which is `:current_user`, by default.
|
298
|
+
|
299
|
+
Specifically, the `scope_name` is defaulted to `:current_user`, and may be set as
|
300
|
+
`serialization_scope :view_context`. The `scope` is set to `send(scope_name)` when `scope_name` is
|
301
|
+
present and the controller responds to `scope_name`.
|
302
|
+
|
303
|
+
Thus, in a serializer, the controller provides `current_user` as the
|
304
|
+
current authorization scope when you call `render :json`.
|
305
|
+
|
306
|
+
**IMPORTANT**: Since the scope is set at render, you may want to customize it so that `current_user` isn't
|
307
|
+
called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477)
|
308
|
+
in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope).
|
309
|
+
|
310
|
+
We can change the scope from `current_user` to `view_context`.
|
311
|
+
|
312
|
+
```diff
|
313
|
+
class SomeController < ActionController::Base
|
314
|
+
+ serialization_scope :view_context
|
315
|
+
|
316
|
+
def current_user
|
317
|
+
User.new(id: 2, name: 'Bob', admin: true)
|
318
|
+
end
|
319
|
+
|
320
|
+
def edit
|
321
|
+
user = User.new(id: 1, name: 'Pete')
|
322
|
+
render json: user, serializer: AdminUserSerializer, adapter: :json_api
|
323
|
+
end
|
324
|
+
end
|
325
|
+
```
|
326
|
+
|
327
|
+
We could then use the controller method `view_context` in our serializer, like so:
|
328
|
+
|
329
|
+
```diff
|
330
|
+
class AdminUserSerializer < ActiveModel::Serializer
|
331
|
+
attributes :id, :name, :can_edit
|
332
|
+
|
333
|
+
def can_edit?
|
334
|
+
+ view_context.current_user.admin?
|
335
|
+
end
|
336
|
+
end
|
337
|
+
```
|
338
|
+
|
339
|
+
So that when we render the `#edit` action, we'll get
|
340
|
+
|
341
|
+
```json
|
342
|
+
{"data":{"id":"1","type":"users","attributes":{"name":"Pete","can_edit":true}}}
|
343
|
+
```
|
344
|
+
|
345
|
+
Where `can_edit` is `view_context.current_user.admin?` (true).
|
346
|
+
|
347
|
+
You can also tell what to set as `serialization_scope` for specific actions.
|
348
|
+
|
349
|
+
For example, use `admin_user` only for `Admin::PostSerializer` and `current_user` for rest.
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
class PostsController < ActionController::Base
|
353
|
+
|
354
|
+
before_action only: :edit do
|
355
|
+
self.class.serialization_scope :admin_user
|
356
|
+
end
|
357
|
+
|
358
|
+
def show
|
359
|
+
render json: @post, serializer: PostSerializer
|
360
|
+
end
|
361
|
+
|
362
|
+
def edit
|
363
|
+
@post.save
|
364
|
+
render json: @post, serializer: Admin::PostSerializer
|
365
|
+
end
|
366
|
+
|
367
|
+
private
|
368
|
+
|
369
|
+
def admin_user
|
370
|
+
User.new(id: 2, name: 'Bob', admin: true)
|
371
|
+
end
|
372
|
+
|
373
|
+
def current_user
|
374
|
+
User.new(id: 2, name: 'Bob', admin: false)
|
375
|
+
end
|
376
|
+
end
|
377
|
+
```
|
378
|
+
|
379
|
+
#### #read_attribute_for_serialization(key)
|
380
|
+
|
381
|
+
The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`
|
382
|
+
|
383
|
+
#### #links
|
384
|
+
|
385
|
+
PR please :)
|
386
|
+
|
387
|
+
#### #json_key
|
388
|
+
|
389
|
+
PR please :)
|
390
|
+
|
391
|
+
## Examples
|
392
|
+
|
393
|
+
Given two models, a `Post(title: string, body: text)` and a
|
394
|
+
`Comment(name: string, body: text, post_id: integer)`, you will have two
|
395
|
+
serializers:
|
396
|
+
|
397
|
+
```ruby
|
398
|
+
class PostSerializer < ActiveModel::Serializer
|
399
|
+
cache key: 'posts', expires_in: 3.hours
|
400
|
+
attributes :title, :body
|
401
|
+
|
402
|
+
has_many :comments
|
403
|
+
end
|
404
|
+
```
|
405
|
+
|
406
|
+
and
|
407
|
+
|
408
|
+
```ruby
|
409
|
+
class CommentSerializer < ActiveModel::Serializer
|
410
|
+
attributes :name, :body
|
411
|
+
|
412
|
+
belongs_to :post
|
413
|
+
end
|
414
|
+
```
|
415
|
+
|
416
|
+
Generally speaking, you, as a user of ActiveModelSerializers, will write (or generate) these
|
417
|
+
serializer classes.
|
418
|
+
|
419
|
+
## More Info
|
420
|
+
|
421
|
+
For more information, see [the Serializer class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer.rb)
|
422
|
+
|
423
|
+
## Overriding association methods
|
424
|
+
|
425
|
+
To override an association, call `has_many`, `has_one` or `belongs_to` with a block:
|
426
|
+
|
427
|
+
```ruby
|
428
|
+
class PostSerializer < ActiveModel::Serializer
|
429
|
+
has_many :comments do
|
430
|
+
object.comments.active
|
431
|
+
end
|
432
|
+
end
|
433
|
+
```
|
434
|
+
|
435
|
+
## Overriding attribute methods
|
436
|
+
|
437
|
+
To override an attribute, call `attribute` with a block:
|
438
|
+
|
439
|
+
```ruby
|
440
|
+
class PostSerializer < ActiveModel::Serializer
|
441
|
+
attribute :body do
|
442
|
+
object.body.downcase
|
443
|
+
end
|
444
|
+
end
|
445
|
+
```
|
446
|
+
|
447
|
+
## Overriding association serializer lookup
|
448
|
+
|
449
|
+
If you want to define a specific serializer lookup for your associations, you can override
|
450
|
+
the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions.
|
451
|
+
|
452
|
+
```ruby
|
453
|
+
class MySerializer < ActiveModel::Serializer
|
454
|
+
def self.serializer_for(model, options)
|
455
|
+
return SparseAdminSerializer if model.class == 'Admin'
|
456
|
+
super
|
457
|
+
end
|
458
|
+
|
459
|
+
# the rest of the serializer
|
460
|
+
end
|
461
|
+
```
|