active_model_serializers 0.10.0.rc4 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +19 -1
- data/.rubocop_todo.yml +30 -103
- data/.simplecov +0 -1
- data/.travis.yml +20 -8
- data/CHANGELOG.md +89 -5
- data/CONTRIBUTING.md +54 -179
- data/Gemfile +7 -2
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +27 -5
- data/Rakefile +44 -16
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +1 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +13 -7
- data/docs/README.md +5 -1
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +99 -16
- data/docs/general/configuration_options.md +87 -14
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +35 -0
- data/docs/general/instrumentation.md +1 -1
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/rendering.md +115 -13
- data/docs/general/serializers.md +138 -6
- data/docs/howto/add_pagination_links.md +36 -18
- data/docs/howto/outside_controller_use.md +4 -4
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +29 -18
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +10 -19
- data/lib/active_model/serializable_resource.rb +4 -65
- data/lib/active_model/serializer.rb +73 -18
- data/lib/active_model/serializer/adapter.rb +15 -82
- data/lib/active_model/serializer/adapter/attributes.rb +5 -56
- data/lib/active_model/serializer/adapter/base.rb +5 -47
- data/lib/active_model/serializer/adapter/json.rb +6 -12
- data/lib/active_model/serializer/adapter/json_api.rb +5 -213
- data/lib/active_model/serializer/adapter/null.rb +7 -3
- data/lib/active_model/serializer/array_serializer.rb +3 -3
- data/lib/active_model/serializer/association.rb +4 -5
- data/lib/active_model/serializer/attributes.rb +1 -1
- data/lib/active_model/serializer/caching.rb +56 -5
- data/lib/active_model/serializer/collection_serializer.rb +30 -13
- data/lib/active_model/serializer/configuration.rb +7 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/links.rb +4 -2
- data/lib/active_model/serializer/lint.rb +14 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +57 -1
- data/lib/active_model/serializer/type.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +17 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -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 +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +2 -2
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +4 -1
- data/lib/active_model_serializers/model.rb +11 -1
- data/lib/active_model_serializers/railtie.rb +9 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +24 -2
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/grape/formatters/active_model_serializers.rb +1 -1
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json_api/deserialization_test.rb +56 -3
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +10 -9
- data/test/action_controller/json_api/pagination_test.rb +2 -2
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +201 -35
- data/test/action_controller/serialization_test.rb +39 -7
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +8 -8
- data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +32 -34
- data/test/adapter/json/collection_test.rb +73 -75
- data/test/adapter/json/has_many_test.rb +36 -38
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +127 -129
- data/test/adapter/json_api/collection_test.rb +80 -82
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +68 -70
- data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
- data/test/adapter/json_api/has_many_test.rb +121 -123
- data/test/adapter/json_api/has_one_test.rb +59 -61
- data/test/adapter/json_api/json_api_test.rb +28 -30
- data/test/adapter/json_api/linked_test.rb +319 -321
- data/test/adapter/json_api/links_test.rb +75 -50
- data/test/adapter/json_api/pagination_links_test.rb +115 -82
- data/test/adapter/json_api/parse_test.rb +114 -116
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +35 -37
- data/test/adapter/null_test.rb +13 -15
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +27 -29
- data/test/array_serializer_test.rb +7 -8
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +10 -0
- data/test/fixtures/active_record.rb +12 -0
- data/test/fixtures/poro.rb +28 -3
- data/test/grape_test.rb +5 -5
- data/test/lint_test.rb +9 -0
- data/test/serializable_resource_test.rb +59 -3
- data/test/serializers/associations_test.rb +8 -8
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/meta_test.rb +74 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/support/isolated_unit.rb +3 -0
- data/test/support/rails5_shims.rb +26 -8
- data/test/support/rails_app.rb +38 -18
- data/test/support/serialization_testing.rb +5 -5
- data/test/test_helper.rb +6 -10
- metadata +132 -37
- data/docs/DESIGN.textile +7 -1
- data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
- data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
- data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
- data/test/active_model_serializers/serialization_context_test.rb +0 -18
- data/test/adapter/fragment_cache_test.rb +0 -38
- data/test/adapter/json_api/resource_type_config_test.rb +0 -71
- data/test/serializers/adapter_for_test.rb +0 -166
- data/test/serializers/cache_test.rb +0 -209
- data/test/support/simplecov.rb +0 -6
- data/test/support/stream_capture.rb +0 -50
- data/test/support/test_case.rb +0 -19
@@ -0,0 +1,100 @@
|
|
1
|
+
[Back to Guides](../README.md)
|
2
|
+
|
3
|
+
# Deserialization
|
4
|
+
|
5
|
+
This is currently an *experimental* feature. The interface may change.
|
6
|
+
|
7
|
+
## JSON API
|
8
|
+
|
9
|
+
The `ActiveModelSerializers::Deserialization` defines two methods (namely `jsonapi_parse` and `jsonapi_parse!`), which take a `Hash` or an instance of `ActionController::Parameters` representing a JSON API payload, and return a hash that can directly be used to create/update models. The bang version throws an `InvalidDocument` exception when parsing fails, whereas the "safe" version simply returns an empty hash.
|
10
|
+
|
11
|
+
- Parameters
|
12
|
+
- document: `Hash` or `ActionController::Parameters` instance
|
13
|
+
- options:
|
14
|
+
- only: `Array` of whitelisted fields
|
15
|
+
- except: `Array` of blacklisted fields
|
16
|
+
- keys: `Hash` of fields the name of which needs to be modified (e.g. `{ :author => :user, :date => :created_at }`)
|
17
|
+
|
18
|
+
Examples:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class PostsController < ActionController::Base
|
22
|
+
def create
|
23
|
+
Post.create(create_params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_params
|
27
|
+
ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:title, :content, :author])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
Given a JSON API document,
|
35
|
+
|
36
|
+
```
|
37
|
+
document = {
|
38
|
+
data: {
|
39
|
+
id: 1,
|
40
|
+
type: 'post',
|
41
|
+
attributes: {
|
42
|
+
title: 'Title 1',
|
43
|
+
date: '2015-12-20'
|
44
|
+
},
|
45
|
+
associations: {
|
46
|
+
author: {
|
47
|
+
data: {
|
48
|
+
type: 'user',
|
49
|
+
id: 2
|
50
|
+
}
|
51
|
+
},
|
52
|
+
second_author: {
|
53
|
+
data: nil
|
54
|
+
},
|
55
|
+
comments: {
|
56
|
+
data: [{
|
57
|
+
type: 'comment',
|
58
|
+
id: 3
|
59
|
+
},{
|
60
|
+
type: 'comment',
|
61
|
+
id: 4
|
62
|
+
}]
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
```
|
68
|
+
|
69
|
+
The entire document can be parsed without specifying any options:
|
70
|
+
```ruby
|
71
|
+
ActiveModelSerializers::Deserialization.jsonapi_parse(document)
|
72
|
+
#=>
|
73
|
+
# {
|
74
|
+
# title: 'Title 1',
|
75
|
+
# date: '2015-12-20',
|
76
|
+
# author_id: 2,
|
77
|
+
# second_author_id: nil
|
78
|
+
# comment_ids: [3, 4]
|
79
|
+
# }
|
80
|
+
```
|
81
|
+
|
82
|
+
and fields, relationships, and polymorphic relationships can be specified via the options:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
ActiveModelSerializers::Deserialization
|
86
|
+
.jsonapi_parse(document, only: [:title, :date, :author],
|
87
|
+
keys: { date: :published_at },
|
88
|
+
polymorphic: [:author])
|
89
|
+
#=>
|
90
|
+
# {
|
91
|
+
# title: 'Title 1',
|
92
|
+
# published_at: '2015-12-20',
|
93
|
+
# author_id: '2',
|
94
|
+
# author_type: 'user'
|
95
|
+
# }
|
96
|
+
```
|
97
|
+
|
98
|
+
## Attributes/Json
|
99
|
+
|
100
|
+
There is currently no deserialization for those adapters.
|
@@ -80,6 +80,32 @@ end
|
|
80
80
|
|
81
81
|
ActiveModelSerializers will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`).
|
82
82
|
|
83
|
+
### Extending a Base `ApplicationSerializer`
|
84
|
+
|
85
|
+
By default, new serializers descend from `ActiveModel::Serializer`. However, if
|
86
|
+
you wish to share behavior across your serializers, you can create an
|
87
|
+
`ApplicationSerializer` at `app/serializers/application_serializer.rb`:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class ApplicationSerializer < ActiveModel::Serializer
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
Then any newly-generated serializers will automatically descend from
|
95
|
+
`ApplicationSerializer`.
|
96
|
+
|
97
|
+
```
|
98
|
+
$ rails g serializer post
|
99
|
+
```
|
100
|
+
|
101
|
+
Now generates:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class PostSerializer < ApplicationSerializer
|
105
|
+
attributes :id
|
106
|
+
end
|
107
|
+
````
|
108
|
+
|
83
109
|
## Rails Integration
|
84
110
|
|
85
111
|
ActiveModelSerializers will automatically integrate with your Rails app,
|
@@ -96,3 +122,12 @@ class PostsController < ApplicationController
|
|
96
122
|
|
97
123
|
end
|
98
124
|
```
|
125
|
+
|
126
|
+
If you wish to use Rails url helpers for link generation, e.g., `link(:resources) { resources_url }`, ensure your application sets
|
127
|
+
`Rails.application.routes.default_url_options`.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
Rails.application.routes.default_url_options = {
|
131
|
+
host: 'example.com'
|
132
|
+
}
|
133
|
+
```
|
@@ -0,0 +1,40 @@
|
|
1
|
+
[Back to Guides](../README.md)
|
2
|
+
|
3
|
+
# Key Transforms
|
4
|
+
|
5
|
+
Key Transforms modify the casing of keys and keys referenced in values in
|
6
|
+
serialized responses.
|
7
|
+
|
8
|
+
Provided key transforms:
|
9
|
+
|
10
|
+
| Option | Result |
|
11
|
+
|----|----|
|
12
|
+
| `:camel` | ExampleKey |
|
13
|
+
| `:camel_lower` | exampleKey |
|
14
|
+
| `:dash` | example-key |
|
15
|
+
| `:unaltered` | the original, unaltered key |
|
16
|
+
| `:underscore` | example_key |
|
17
|
+
| `nil` | use the adapter default |
|
18
|
+
|
19
|
+
Key translation precedence is as follows:
|
20
|
+
|
21
|
+
##### Adapter option
|
22
|
+
|
23
|
+
`key_transform` is provided as an option via render.
|
24
|
+
|
25
|
+
```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower```
|
26
|
+
|
27
|
+
##### Configuration option
|
28
|
+
|
29
|
+
`key_transform` is set in `ActiveModelSerializers.config.key_transform`.
|
30
|
+
|
31
|
+
```ActiveModelSerializers.config.key_transform = :camel_lower```
|
32
|
+
|
33
|
+
##### Adapter default
|
34
|
+
|
35
|
+
Each adapter has a default transform configured:
|
36
|
+
|
37
|
+
| Adapter | Default Key Transform |
|
38
|
+
|----|----|
|
39
|
+
| `Json` | `:unaltered` |
|
40
|
+
| `JsonApi` | `:dash` |
|
data/docs/general/rendering.md
CHANGED
@@ -56,11 +56,11 @@ API for a plain-old Ruby object (PORO).
|
|
56
56
|
|
57
57
|
## SerializableResource options
|
58
58
|
|
59
|
-
The `options` hash passed to `render` or `
|
59
|
+
The `options` hash passed to `render` or `ActiveModelSerializers::SerializableResource.new(resource, options)`
|
60
60
|
are partitioned into `serializer_opts` and `adapter_opts`. `adapter_opts` are passed to new Adapters;
|
61
61
|
`serializer_opts` are passed to new Serializers.
|
62
62
|
|
63
|
-
The `adapter_opts` are specified in [
|
63
|
+
The `adapter_opts` are specified in [ActiveModelSerializers::SerializableResource::ADAPTER_OPTIONS](../../lib/active_model_serializers/serializable_resource.rb#L5).
|
64
64
|
The `serializer_opts` are the remaining options.
|
65
65
|
|
66
66
|
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
|
@@ -79,10 +79,20 @@ PR please :)
|
|
79
79
|
|
80
80
|
PR please :)
|
81
81
|
|
82
|
+
#### key_transform
|
83
|
+
|
84
|
+
```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower```
|
85
|
+
|
86
|
+
See [Key Transforms](key_transforms.md) for more informaiton.
|
87
|
+
|
82
88
|
#### meta
|
83
89
|
|
84
|
-
|
85
|
-
|
90
|
+
A `meta` member can be used to include non-standard meta-information. `meta` can
|
91
|
+
be utilized in several levels in a response.
|
92
|
+
|
93
|
+
##### Top-level
|
94
|
+
|
95
|
+
To set top-level `meta` in a response, specify it in the `render` call.
|
86
96
|
|
87
97
|
```ruby
|
88
98
|
render json: @post, meta: { total: 10 }
|
@@ -94,16 +104,106 @@ The key can be customized using `meta_key` option.
|
|
94
104
|
render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
|
95
105
|
```
|
96
106
|
|
97
|
-
`meta` will only be included in your response if you are using an Adapter that
|
98
|
-
|
107
|
+
`meta` will only be included in your response if you are using an Adapter that
|
108
|
+
supports `root`, e.g., `JsonApi` and `Json` adapters. The default adapter,
|
109
|
+
`Attributes` does not have `root`.
|
99
110
|
|
100
|
-
#### meta_key
|
101
111
|
|
102
|
-
|
112
|
+
##### Resource-level
|
113
|
+
|
114
|
+
To set resource-level `meta` in a response, define meta in a serializer with one
|
115
|
+
of the following methods:
|
116
|
+
|
117
|
+
As a single, static string.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
meta stuff: 'value'
|
121
|
+
```
|
122
|
+
|
123
|
+
As a block containing a Hash.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
meta do
|
127
|
+
{
|
128
|
+
rating: 4,
|
129
|
+
comments_count: object.comments.count
|
130
|
+
}
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
103
134
|
|
104
135
|
#### links
|
105
136
|
|
106
|
-
|
137
|
+
If you wish to use Rails url helpers for link generation, e.g., `link(:resources) { resources_url }`, ensure your application sets
|
138
|
+
`Rails.application.routes.default_url_options`.
|
139
|
+
|
140
|
+
##### Top-level
|
141
|
+
|
142
|
+
JsonApi supports a [links object](http://jsonapi.org/format/#document-links) to be specified at top-level, that you can specify in the `render`:
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
links_object = {
|
146
|
+
href: "http://example.com/api/posts",
|
147
|
+
meta: {
|
148
|
+
count: 10
|
149
|
+
}
|
150
|
+
}
|
151
|
+
render json: @posts, links: links_object
|
152
|
+
```
|
153
|
+
|
154
|
+
That's the result:
|
155
|
+
|
156
|
+
```json
|
157
|
+
{
|
158
|
+
"data": [
|
159
|
+
{
|
160
|
+
"type": "posts",
|
161
|
+
"id": "1",
|
162
|
+
"attributes": {
|
163
|
+
"title": "JSON API is awesome!",
|
164
|
+
"body": "You should be using JSON API",
|
165
|
+
"created": "2015-05-22T14:56:29.000Z",
|
166
|
+
"updated": "2015-05-22T14:56:28.000Z"
|
167
|
+
}
|
168
|
+
}
|
169
|
+
],
|
170
|
+
"links": {
|
171
|
+
"href": "http://example.com/api/posts",
|
172
|
+
"meta": {
|
173
|
+
"count": 10
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
```
|
178
|
+
|
179
|
+
This feature is specific to JsonApi, so you have to use the use the [JsonApi Adapter](adapters.md#jsonapi)
|
180
|
+
|
181
|
+
|
182
|
+
##### Resource-level
|
183
|
+
|
184
|
+
In your serializer, define each link in one of the following methods:
|
185
|
+
|
186
|
+
As a static string
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
link :link_name, 'https://example.com/resource'
|
190
|
+
```
|
191
|
+
|
192
|
+
As a block to be evaluated. When using Rails, URL helpers are available.
|
193
|
+
Ensure your application sets `Rails.application.routes.default_url_options`.
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
link :link_name_ do
|
197
|
+
"https://example.com/resource/#{object.id}"
|
198
|
+
end
|
199
|
+
|
200
|
+
link(:link_name) { "https://example.com/resource/#{object.id}" }
|
201
|
+
|
202
|
+
link(:link_name) { resource_url(object) }
|
203
|
+
|
204
|
+
link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_path: false) }
|
205
|
+
|
206
|
+
```
|
107
207
|
|
108
208
|
### serializer_opts
|
109
209
|
|
@@ -111,12 +211,14 @@ PR please :)
|
|
111
211
|
|
112
212
|
PR please :)
|
113
213
|
|
114
|
-
#### root
|
214
|
+
#### Overriding the root key
|
215
|
+
|
216
|
+
Overriding the resource root only applies when using the JSON adapter.
|
115
217
|
|
116
|
-
|
218
|
+
Normally, the resource root is derived from the class name of the resource being serialized.
|
117
219
|
e.g. `UserPostSerializer.new(UserPost.new)` will be serialized with the root `user_post` or `user_posts` according the adapter collection pluralization rules.
|
118
220
|
|
119
|
-
|
221
|
+
When using the JSON adapter in your initializer (ActiveModelSerializers.config.adapter = :json), or passing in the adapter in your render call, you can specify the root by passing it as an argument to `render`. For example:
|
120
222
|
|
121
223
|
```ruby
|
122
224
|
render json: @user_post, root: "admin_post", adapter: :json
|
@@ -130,7 +232,7 @@ This will be rendered as:
|
|
130
232
|
}
|
131
233
|
}
|
132
234
|
```
|
133
|
-
Note: the `Attributes` adapter (default) does not include a resource root.
|
235
|
+
Note: the `Attributes` adapter (default) does not include a resource root. You also will not be able to create a single top-level root if you are using the :json_api adapter.
|
134
236
|
|
135
237
|
#### serializer
|
136
238
|
|
data/docs/general/serializers.md
CHANGED
@@ -46,6 +46,16 @@ e.g.
|
|
46
46
|
has_one :bio
|
47
47
|
has_one :blog, key: :site
|
48
48
|
has_one :maker, virtual_value: { id: 1 }
|
49
|
+
|
50
|
+
has_one :blog do |serializer|
|
51
|
+
serializer.cached_blog
|
52
|
+
end
|
53
|
+
|
54
|
+
def cached_blog
|
55
|
+
cache_store.fetch("cached_blog:#{object.updated_at}") do
|
56
|
+
Blog.find(object.blog_id)
|
57
|
+
end
|
58
|
+
end
|
49
59
|
```
|
50
60
|
|
51
61
|
#### ::has_many
|
@@ -76,6 +86,18 @@ def blog
|
|
76
86
|
end
|
77
87
|
```
|
78
88
|
|
89
|
+
### Polymorphic Relationships
|
90
|
+
|
91
|
+
Polymorphic relationships are serialized by specifying the relationship, like any other association. For example:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class PictureSerializer < ActiveModel::Serializer
|
95
|
+
has_one :imageable
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
For more context, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter.
|
100
|
+
|
79
101
|
### Caching
|
80
102
|
|
81
103
|
#### ::cache
|
@@ -107,23 +129,42 @@ end
|
|
107
129
|
|
108
130
|
#### ::type
|
109
131
|
|
110
|
-
|
132
|
+
The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
|
133
|
+
It either takes a `String` or `Symbol` as parameter.
|
134
|
+
|
135
|
+
Note: This method is useful only when using the `:json_api` adapter.
|
111
136
|
|
137
|
+
Examples:
|
112
138
|
```ruby
|
113
139
|
class UserProfileSerializer < ActiveModel::Serializer
|
114
140
|
type 'profile'
|
115
141
|
end
|
142
|
+
class AuthorProfileSerializer < ActiveModel::Serializer
|
143
|
+
type :profile
|
144
|
+
end
|
116
145
|
```
|
117
146
|
|
118
|
-
|
147
|
+
With the `:json_api` adapter, the previous serializers would be rendered as:
|
119
148
|
|
120
|
-
|
149
|
+
``` json
|
150
|
+
{
|
151
|
+
"data": {
|
152
|
+
"id": "1",
|
153
|
+
"type": "profile"
|
154
|
+
}
|
155
|
+
}
|
156
|
+
```
|
157
|
+
|
158
|
+
#### ::link
|
121
159
|
|
122
160
|
```ruby
|
123
|
-
link :other, 'https://example.com/resource'
|
124
161
|
link :self do
|
125
|
-
|
162
|
+
href "https://example.com/link_author/#{object.id}"
|
126
163
|
end
|
164
|
+
link :author { link_author_url(object) }
|
165
|
+
link :link_authors { link_authors_url }
|
166
|
+
link :other, 'https://example.com/resource'
|
167
|
+
link :posts { link_author_posts_url(object) }
|
127
168
|
```
|
128
169
|
|
129
170
|
#### #object
|
@@ -136,7 +177,98 @@ PR please :)
|
|
136
177
|
|
137
178
|
#### #scope
|
138
179
|
|
139
|
-
|
180
|
+
Allows you to include in the serializer access to an external method.
|
181
|
+
|
182
|
+
It's intended to provide an authorization context to the serializer, so that
|
183
|
+
you may e.g. show an admin all comments on a post, else only published comments.
|
184
|
+
|
185
|
+
- `scope` is a method on the serializer instance that comes from `options[:scope]`. It may be nil.
|
186
|
+
- `scope_name` is an option passed to the new serializer (`options[:scope_name]`). The serializer
|
187
|
+
defines a method with that name that calls the `scope`, e.g. `def current_user; scope; end`.
|
188
|
+
Note: it does not define the method if the serializer instance responds to it.
|
189
|
+
|
190
|
+
That's a lot of words, so here's some examples:
|
191
|
+
|
192
|
+
First, let's assume the serializer is instantiated in the controller, since that's the usual scenario.
|
193
|
+
We'll refer to the serialization context as `controller`.
|
194
|
+
|
195
|
+
| options | `Serializer#scope` | method definition |
|
196
|
+
|-------- | ------------------|--------------------|
|
197
|
+
| `scope: current_user, scope_name: :current_user` | `current_user` | `Serializer#current_user` calls `controller.current_user`
|
198
|
+
| `scope: view_context, scope_name: :view_context` | `view_context` | `Serializer#view_context` calls `controller.view_context`
|
199
|
+
|
200
|
+
We can take advantage of the scope to customize the objects returned based
|
201
|
+
on the current user (scope).
|
202
|
+
|
203
|
+
For example, we can limit the posts the current user sees to those they created:
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
class PostSerializer < ActiveModel::Serializer
|
207
|
+
attributes :id, :title, :body
|
208
|
+
|
209
|
+
# scope comments to those created_by the current user
|
210
|
+
has_many :comments do
|
211
|
+
object.comments.where(created_by: current_user)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
```
|
215
|
+
|
216
|
+
Whether you write the method as above or as `object.comments.where(created_by: scope)`
|
217
|
+
is a matter of preference (assuming `scope_name` has been set).
|
218
|
+
|
219
|
+
##### Controller Authorization Context
|
220
|
+
|
221
|
+
In the controller, the scope/scope_name options are equal to
|
222
|
+
the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20),
|
223
|
+
which is `:current_user`, by default.
|
224
|
+
|
225
|
+
Specfically, the `scope_name` is defaulted to `:current_user`, and may be set as
|
226
|
+
`serialization_scope :view_context`. The `scope` is set to `send(scope_name)` when `scope_name` is
|
227
|
+
present and the controller responds to `scope_name`.
|
228
|
+
|
229
|
+
Thus, in a serializer, the controller provides `current_user` as the
|
230
|
+
current authorization scope when you call `render :json`.
|
231
|
+
|
232
|
+
**IMPORTANT**: Since the scope is set at render, you may want to customize it so that `current_user` isn't
|
233
|
+
called on every request. This was [also a problem](https://github.com/rails-api/active_model_serializers/pull/1252#issuecomment-159810477)
|
234
|
+
in [`0.9`](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope).
|
235
|
+
|
236
|
+
We can change the scope from `current_user` to `view_context`.
|
237
|
+
|
238
|
+
```diff
|
239
|
+
class SomeController < ActionController::Base
|
240
|
+
+ serialization_scope :view_context
|
241
|
+
|
242
|
+
def current_user
|
243
|
+
User.new(id: 2, name: 'Bob', admin: true)
|
244
|
+
end
|
245
|
+
|
246
|
+
def edit
|
247
|
+
user = User.new(id: 1, name: 'Pete')
|
248
|
+
render json: user, serializer: AdminUserSerializer, adapter: :json_api
|
249
|
+
end
|
250
|
+
end
|
251
|
+
```
|
252
|
+
|
253
|
+
We could then use the controller method `view_context` in our serializer, like so:
|
254
|
+
|
255
|
+
```diff
|
256
|
+
class AdminUserSerializer < ActiveModel::Serializer
|
257
|
+
attributes :id, :name, :can_edit
|
258
|
+
|
259
|
+
def can_edit?
|
260
|
+
+ view_context.current_user.admin?
|
261
|
+
end
|
262
|
+
end
|
263
|
+
```
|
264
|
+
|
265
|
+
So that when we render the `#edit` action, we'll get
|
266
|
+
|
267
|
+
```json
|
268
|
+
{"data":{"id":"1","type":"users","attributes":{"name":"Pete","can_edit":true}}}
|
269
|
+
```
|
270
|
+
|
271
|
+
Where `can_edit` is `view_context.current_user.admin?` (true).
|
140
272
|
|
141
273
|
#### #read_attribute_for_serialization(key)
|
142
274
|
|