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
data/bin/serve_benchmark
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
set -e
|
3
|
+
|
4
|
+
case "$1" in
|
5
|
+
|
6
|
+
start)
|
7
|
+
config="${CONFIG_RU:-test/benchmark/config.ru}"
|
8
|
+
bundle exec ruby -Ilib -S rackup "$config" --daemonize --pid tmp/benchmark_app.pid --warn --server webrick
|
9
|
+
until [ -f 'tmp/benchmark_app.pid' ]; do
|
10
|
+
sleep 0.1 # give it time to start.. I don't know a better way
|
11
|
+
done
|
12
|
+
cat tmp/benchmark_app.pid
|
13
|
+
true
|
14
|
+
;;
|
15
|
+
|
16
|
+
stop)
|
17
|
+
if [ -f 'tmp/benchmark_app.pid' ]; then
|
18
|
+
kill -TERM $(cat tmp/benchmark_app.pid)
|
19
|
+
else
|
20
|
+
echo 'No pidfile'
|
21
|
+
false
|
22
|
+
fi
|
23
|
+
;;
|
24
|
+
|
25
|
+
status)
|
26
|
+
if [ -f 'tmp/benchmark_app.pid' ]; then
|
27
|
+
kill -0 $(cat tmp/benchmark_app.pid)
|
28
|
+
[ "$?" -eq 0 ]
|
29
|
+
else
|
30
|
+
echo 'No pidfile'
|
31
|
+
false
|
32
|
+
fi
|
33
|
+
;;
|
34
|
+
|
35
|
+
*)
|
36
|
+
echo "Usage: $0 [start|stop|status]"
|
37
|
+
;;
|
38
|
+
|
39
|
+
esac
|
data/docs/ARCHITECTURE.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
[Back to Guides](README.md)
|
2
2
|
|
3
|
+
This document focuses on architecture the 0.10.x version of ActiveModelSerializers. If you are interested in the architecture of the 0.8 or 0.9 versions,
|
4
|
+
please refer to the [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md) or
|
5
|
+
[0.9 README](https://github.com/rails-api/active_model_serializers/blob/0-9-stable/README.md).
|
6
|
+
|
7
|
+
The original design is also available [here](https://github.com/rails-api/active_model_serializers/blob/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e/README.textile).
|
8
|
+
|
3
9
|
# ARCHITECTURE
|
4
10
|
|
5
11
|
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb)
|
@@ -17,7 +23,7 @@ serializer. For example, the `Attributes` example represents each serializer as
|
|
17
23
|
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
|
18
24
|
API](http://jsonapi.org/) document.
|
19
25
|
|
20
|
-
The **`
|
26
|
+
The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
|
21
27
|
to an object that responds to `to_json`, and `as_json`. It is used in the controller to
|
22
28
|
encapsulate the serialization resource when rendered. However, it can also be used on its own
|
23
29
|
to serialize a resource outside of a controller, as well.
|
@@ -56,16 +62,16 @@ High-level overview:
|
|
56
62
|
- `:each_serializer` specifies the serializer for each resource in the collection.
|
57
63
|
- For a single resource, the `:serializer` option is the resource serializer.
|
58
64
|
- Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
|
59
|
-
[`
|
65
|
+
[`ADAPTER_OPTION_KEYS`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/serializable_resource.rb#L5).
|
60
66
|
The remaining options are serializer options.
|
61
67
|
|
62
68
|
Details:
|
63
69
|
|
64
70
|
1. **ActionController::Serialization**
|
65
|
-
1. `serializable_resource =
|
71
|
+
1. `serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)`
|
66
72
|
1. `options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
|
67
|
-
The
|
68
|
-
1. **
|
73
|
+
The `adapter_opts` keys are defined in `ActiveModelSerializers::SerializableResource::ADAPTER_OPTION_KEYS`.
|
74
|
+
1. **ActiveModelSerializers::SerializableResource**
|
69
75
|
1. `if serializable_resource.serializer?` (there is a serializer for the resource, and an adapter is used.)
|
70
76
|
- Where `serializer?` is `use_adapter? && !!(serializer)`
|
71
77
|
- Where `use_adapter?`: 'True when no explicit adapter given, or explicit value is truthy (non-nil);
|
@@ -88,7 +94,7 @@ Details:
|
|
88
94
|
## What does a 'serializable resource' look like?
|
89
95
|
|
90
96
|
- An `ActiveRecord::Base` object.
|
91
|
-
- Any Ruby object
|
97
|
+
- Any Ruby object that passes the
|
92
98
|
[Lint](http://www.rubydoc.info/github/rails-api/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
|
93
99
|
[code](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb).
|
94
100
|
|
@@ -116,5 +122,5 @@ render json: MyModel.new(level: 'awesome'), adapter: :json
|
|
116
122
|
would be serialized the same as
|
117
123
|
|
118
124
|
```ruby
|
119
|
-
|
125
|
+
ActiveModelSerializers::SerializableResource.new(MyModel.new(level: 'awesome'), adapter: :json).as_json
|
120
126
|
```
|
data/docs/README.md
CHANGED
@@ -13,8 +13,11 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
|
|
13
13
|
- [Rendering](general/rendering.md)
|
14
14
|
- [Caching](general/caching.md)
|
15
15
|
- [Logging](general/logging.md)
|
16
|
+
- [Deserialization](general/deserialization.md)
|
16
17
|
- [Instrumentation](general/instrumentation.md)
|
17
|
-
-
|
18
|
+
- JSON API
|
19
|
+
- [Schema](jsonapi/schema.md)
|
20
|
+
- [Errors](jsonapi/errors.md)
|
18
21
|
- [ARCHITECTURE](ARCHITECTURE.md)
|
19
22
|
|
20
23
|
## How to
|
@@ -23,6 +26,7 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
|
|
23
26
|
- [How to add pagination links](howto/add_pagination_links.md)
|
24
27
|
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
|
25
28
|
- [Testing ActiveModelSerializers](howto/test.md)
|
29
|
+
- [Passing Arbitrary Options](howto/passing_arbitrary_options.md)
|
26
30
|
|
27
31
|
## Integrations
|
28
32
|
|
data/docs/STYLE.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# STYLE
|
2
|
+
|
3
|
+
## Code and comments
|
4
|
+
|
5
|
+
- We are actively working to identify tasks under the label [**Good for New
|
6
|
+
Contributors**](https://github.com/rails-api/active_model_serializers/labels/Good%20for%20New%20Contributors).
|
7
|
+
- [Changelog
|
8
|
+
Missing](https://github.com/rails-api/active_model_serializers/issues?q=label%3A%22Changelog+Missing%22+is%3Aclosed) is
|
9
|
+
an easy way to help out.
|
10
|
+
|
11
|
+
- [Fix a bug](https://github.com/rails-api/active_model_serializers/labels/Ready%20for%20PR).
|
12
|
+
- Ready for PR - A well defined bug, needs someone to PR a fix.
|
13
|
+
- Bug - Anything that is broken.
|
14
|
+
- Regression - A bug that did not exist in previous versions and isn't a new feature (applied in tandem with Bug).
|
15
|
+
- Performance - A performance related issue. We could track this as a bug, but usually these would have slightly lower priority than standard bugs.
|
16
|
+
|
17
|
+
- [Develop new features](https://github.com/rails-api/active_model_serializers/labels/Feature).
|
18
|
+
|
19
|
+
- [Improve code quality](https://codeclimate.com/github/rails-api/active_model_serializers/code?sort=smell_count&sort_direction=desc).
|
20
|
+
|
21
|
+
- [Improve amount of code exercised by tests](https://codeclimate.com/github/rails-api/active_model_serializers/coverage?sort=covered_percent&sort_direction=asc).
|
22
|
+
|
23
|
+
- [Fix RuboCop (Style) TODOS](https://github.com/rails-api/active_model_serializers/blob/master/.rubocop_todo.yml).
|
24
|
+
- Delete and offsense, run `rake rubocop` (or possibly `rake rubocop:auto_correct`),
|
25
|
+
and [submit a PR](CONTRIBUTING.md#submitting-a-pull-request-pr).
|
26
|
+
|
27
|
+
- We are also encouraging comments to substantial changes (larger than bugfixes and simple features) under an
|
28
|
+
"RFC" (Request for Comments) process before we start active development.
|
29
|
+
Look for the [**RFC**](https://github.com/rails-api/active_model_serializers/labels/RFC) label.
|
30
|
+
|
31
|
+
|
32
|
+
## Pull requests
|
33
|
+
|
34
|
+
- If the tests pass and the pull request looks good, a maintainer will merge it.
|
35
|
+
- If the pull request needs to be changed,
|
36
|
+
- you can change it by updating the branch you generated the pull request from
|
37
|
+
- either by adding more commits, or
|
38
|
+
- by force pushing to it
|
39
|
+
- A maintainer can make any changes themselves and manually merge the code in.
|
40
|
+
|
41
|
+
## Commit messages
|
42
|
+
|
43
|
+
- [A Note About Git Commit Messages](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
|
44
|
+
- [http://stopwritingramblingcommitmessages.com/](http://stopwritingramblingcommitmessages.com/)
|
45
|
+
- [ThoughtBot style guide](https://github.com/thoughtbot/guides/tree/master/style#git)
|
46
|
+
|
47
|
+
#### About Pull Requests (PR's)
|
48
|
+
|
49
|
+
- [Using Pull Requests](https://help.github.com/articles/using-pull-requests)
|
50
|
+
- [Github pull requests made easy](http://www.element84.com/github-pull-requests-made-easy.html)
|
51
|
+
- [Exercism Git Workflow](http://help.exercism.io/git-workflow.html).
|
52
|
+
- [Level up your Git](http://rakeroutes.com/blog/deliberate-git/)
|
53
|
+
- [All Your Open Source Code Are Belong To Us](http://www.benjaminfleischer.com/2013/07/30/all-your-open-source-code-are-belong-to-us/)
|
54
|
+
|
55
|
+
## Issue Labeling
|
56
|
+
|
57
|
+
ActiveModelSerializers uses a subset of [StandardIssueLabels](https://github.com/wagenet/StandardIssueLabels) for Github Issues. You can [see our labels here](https://github.com/rails-api/active_model_serializers/labels).
|
58
|
+
|
data/docs/general/adapters.md
CHANGED
@@ -11,7 +11,7 @@ It should be set only once, preferably at initialization.
|
|
11
11
|
For example:
|
12
12
|
|
13
13
|
```ruby
|
14
|
-
ActiveModelSerializers.config.adapter =
|
14
|
+
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::JsonApi
|
15
15
|
```
|
16
16
|
|
17
17
|
or
|
@@ -43,6 +43,28 @@ Use either the `JSON` or `JSON API` adapters if you want the response document t
|
|
43
43
|
It's the default adapter, it generates a json response without a root key.
|
44
44
|
Doesn't follow any specific convention.
|
45
45
|
|
46
|
+
##### Example output
|
47
|
+
|
48
|
+
```json
|
49
|
+
{
|
50
|
+
"title": "Title 1",
|
51
|
+
"body": "Body 1",
|
52
|
+
"publish_at": "2020-03-16T03:55:25.291Z",
|
53
|
+
"author": {
|
54
|
+
"first_name": "Bob",
|
55
|
+
"last_name": "Jones"
|
56
|
+
},
|
57
|
+
"comments": [
|
58
|
+
{
|
59
|
+
"body": "cool"
|
60
|
+
},
|
61
|
+
{
|
62
|
+
"body": "awesome"
|
63
|
+
}
|
64
|
+
]
|
65
|
+
}
|
66
|
+
```
|
67
|
+
|
46
68
|
### JSON
|
47
69
|
|
48
70
|
The response document always with a root key.
|
@@ -51,11 +73,72 @@ The root key **can't be overridden**, and will be derived from the resource bein
|
|
51
73
|
|
52
74
|
Doesn't follow any specific convention.
|
53
75
|
|
76
|
+
##### Example output
|
77
|
+
|
78
|
+
```json
|
79
|
+
{
|
80
|
+
"post": {
|
81
|
+
"title": "Title 1",
|
82
|
+
"body": "Body 1",
|
83
|
+
"publish_at": "2020-03-16T03:55:25.291Z",
|
84
|
+
"author": {
|
85
|
+
"first_name": "Bob",
|
86
|
+
"last_name": "Jones"
|
87
|
+
},
|
88
|
+
"comments": [{
|
89
|
+
"body": "cool"
|
90
|
+
}, {
|
91
|
+
"body": "awesome"
|
92
|
+
}]
|
93
|
+
}
|
94
|
+
}
|
95
|
+
```
|
96
|
+
|
54
97
|
### JSON API
|
55
98
|
|
56
99
|
This adapter follows **version 1.0** of the [format specified](../jsonapi/schema.md) in
|
57
100
|
[jsonapi.org/format](http://jsonapi.org/format).
|
58
101
|
|
102
|
+
##### Example output
|
103
|
+
|
104
|
+
```json
|
105
|
+
{
|
106
|
+
"data": {
|
107
|
+
"id": "1337",
|
108
|
+
"type": "posts",
|
109
|
+
"attributes": {
|
110
|
+
"title": "Title 1",
|
111
|
+
"body": "Body 1",
|
112
|
+
"publish-at": "2020-03-16T03:55:25.291Z"
|
113
|
+
},
|
114
|
+
"relationships": {
|
115
|
+
"author": {
|
116
|
+
"data": {
|
117
|
+
"id": "1",
|
118
|
+
"type": "authors"
|
119
|
+
}
|
120
|
+
},
|
121
|
+
"comments": {
|
122
|
+
"data": [{
|
123
|
+
"id": "7",
|
124
|
+
"type": "comments"
|
125
|
+
}, {
|
126
|
+
"id": "12",
|
127
|
+
"type": "comments"
|
128
|
+
}]
|
129
|
+
}
|
130
|
+
},
|
131
|
+
"links": {
|
132
|
+
"post-authors": "https://example.com/post_authors"
|
133
|
+
},
|
134
|
+
"meta": {
|
135
|
+
"rating": 5,
|
136
|
+
"favorite-count": 10
|
137
|
+
}
|
138
|
+
}
|
139
|
+
}
|
140
|
+
```
|
141
|
+
|
59
142
|
#### Included
|
60
143
|
|
61
144
|
It will include the associated resources in the `"included"` member
|
@@ -117,46 +200,46 @@ The default adapter can be configured, as above, to use any class given to it.
|
|
117
200
|
|
118
201
|
An adapter may also be specified, e.g. when rendering, as a class or as a symbol.
|
119
202
|
If a symbol, then the adapter must be, e.g. `:great_example`,
|
120
|
-
`
|
203
|
+
`ActiveModelSerializers::Adapter::GreatExample`, or registered.
|
121
204
|
|
122
205
|
There are two ways to register an adapter:
|
123
206
|
|
124
|
-
1) The simplest, is to subclass `
|
125
|
-
register the `Example::UsefulAdapter` as
|
207
|
+
1) The simplest, is to subclass `ActiveModelSerializers::Adapter::Base`, e.g. the below will
|
208
|
+
register the `Example::UsefulAdapter` as `"example/useful_adapter"`.
|
126
209
|
|
127
210
|
```ruby
|
128
211
|
module Example
|
129
|
-
class UsefulAdapter <
|
212
|
+
class UsefulAdapter < ActiveModelSerializers::Adapter::Base
|
130
213
|
end
|
131
214
|
end
|
132
215
|
```
|
133
216
|
|
134
|
-
You'll notice that the name it registers is the
|
217
|
+
You'll notice that the name it registers is the underscored namespace and class.
|
135
218
|
|
136
|
-
Under the covers, when the `
|
137
|
-
the subclass as `register(
|
219
|
+
Under the covers, when the `ActiveModelSerializers::Adapter::Base` is subclassed, it registers
|
220
|
+
the subclass as `register("example/useful_adapter", Example::UsefulAdapter)`
|
138
221
|
|
139
222
|
2) Any class can be registered as an adapter by calling `register` directly on the
|
140
|
-
`
|
223
|
+
`ActiveModelSerializers::Adapter` class. e.g., the below registers `MyAdapter` as
|
141
224
|
`:special_adapter`.
|
142
225
|
|
143
226
|
```ruby
|
144
227
|
class MyAdapter; end
|
145
|
-
|
228
|
+
ActiveModelSerializers::Adapter.register(:special_adapter, MyAdapter)
|
146
229
|
```
|
147
230
|
|
148
231
|
### Looking up an adapter
|
149
232
|
|
150
233
|
| Method | Return value |
|
151
234
|
| :------------ |:---------------|
|
152
|
-
| `
|
153
|
-
| `
|
154
|
-
| `
|
155
|
-
| `
|
156
|
-
| `
|
235
|
+
| `ActiveModelSerializers::Adapter.adapter_map` | A Hash of all known adapters `{ adapter_name => adapter_class }` |
|
236
|
+
| `ActiveModelSerializers::Adapter.adapters` | A (sorted) Array of all known `adapter_names` |
|
237
|
+
| `ActiveModelSerializers::Adapter.lookup(name_or_klass)` | The `adapter_class`, else raises an `ActiveModelSerializers::Adapter::UnknownAdapter` error |
|
238
|
+
| `ActiveModelSerializers::Adapter.adapter_class(adapter)` | Delegates to `ActiveModelSerializers::Adapter.lookup(adapter)` |
|
239
|
+
| `ActiveModelSerializers::Adapter.configured_adapter` | A convenience method for `ActiveModelSerializers::Adapter.lookup(config.adapter)` |
|
157
240
|
|
158
241
|
The registered adapter name is always a String, but may be looked up as a Symbol or String.
|
159
242
|
Helpfully, the Symbol or String is underscored, so that `get(:my_adapter)` and `get("MyAdapter")`
|
160
243
|
may both be used.
|
161
244
|
|
162
|
-
For more information, see [the Adapter class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/
|
245
|
+
For more information, see [the Adapter class on GitHub](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/adapter.rb)
|
@@ -2,26 +2,99 @@
|
|
2
2
|
|
3
3
|
# Configuration Options
|
4
4
|
|
5
|
-
The following configuration options can be set on
|
6
|
-
preferably inside an initializer.
|
5
|
+
The following configuration options can be set on
|
6
|
+
`ActiveModelSerializers.config`, preferably inside an initializer.
|
7
7
|
|
8
8
|
## General
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
##### adapter
|
11
|
+
|
12
|
+
The [adapter](adapters.md) to use.
|
13
|
+
|
14
|
+
Possible values:
|
15
|
+
|
16
|
+
- `:attributes` (default)
|
17
|
+
- `:json`
|
18
|
+
- `:json_api`
|
19
|
+
|
20
|
+
##### serializer_lookup_enabled
|
21
|
+
|
22
|
+
Enable automatic serializer lookup.
|
23
|
+
|
24
|
+
Possible values:
|
25
|
+
|
26
|
+
- `true` (default)
|
27
|
+
- `false`
|
28
|
+
|
29
|
+
When `false`, serializers must be explicitly specified.
|
30
|
+
|
31
|
+
##### key_transform
|
32
|
+
|
33
|
+
The [key transform](key_transforms.md) to use.
|
34
|
+
|
35
|
+
|
36
|
+
| Option | Result |
|
37
|
+
|----|----|
|
38
|
+
| `:camel` | ExampleKey |
|
39
|
+
| `:camel_lower` | exampleKey |
|
40
|
+
| `:dash` | example-key |
|
41
|
+
| `:unaltered` | the original, unaltered key |
|
42
|
+
| `:underscore` | example_key |
|
43
|
+
| `nil` | use the adapter default |
|
44
|
+
|
45
|
+
Each adapter has a default key transform configured:
|
46
|
+
|
47
|
+
| Adapter | Default Key Transform |
|
48
|
+
|----|----|
|
49
|
+
| `Json` | `:unaltered` |
|
50
|
+
| `JsonApi` | `:dash` |
|
51
|
+
|
52
|
+
`config.key_transform` is a global override of the adapter default. Adapters
|
53
|
+
still prefer the render option `:key_transform` over this setting.
|
54
|
+
|
12
55
|
|
13
56
|
## JSON API
|
14
57
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
58
|
+
|
59
|
+
##### jsonapi_resource_type
|
60
|
+
|
61
|
+
Sets whether the [type](http://jsonapi.org/format/#document-resource-identifier-objects)
|
62
|
+
of the resource should be `singularized` or `pluralized` when it is not
|
63
|
+
[explicitly specified by the serializer](https://github.com/rails-api/active_model_serializers/blob/master/docs/general/serializers.md#type)
|
64
|
+
|
65
|
+
Possible values:
|
66
|
+
|
67
|
+
- `:singular`
|
68
|
+
- `:plural` (default)
|
69
|
+
|
70
|
+
##### jsonapi_include_toplevel_object
|
71
|
+
|
72
|
+
Include a [top level jsonapi member](http://jsonapi.org/format/#document-jsonapi-object)
|
73
|
+
in the response document.
|
74
|
+
|
75
|
+
Possible values:
|
76
|
+
|
77
|
+
- `true`
|
78
|
+
- `false` (default)
|
79
|
+
|
80
|
+
##### jsonapi_version
|
81
|
+
|
82
|
+
The latest version of the spec to which the API conforms.
|
83
|
+
|
84
|
+
Default: `'1.0'`.
|
85
|
+
|
86
|
+
*Used when `jsonapi_include_toplevel_object` is `true`*
|
87
|
+
|
88
|
+
##### jsonapi_toplevel_meta
|
89
|
+
|
90
|
+
Optional top-level metadata. Not included if empty.
|
91
|
+
|
92
|
+
Default: `{}`.
|
93
|
+
|
94
|
+
*Used when `jsonapi_include_toplevel_object` is `true`*
|
95
|
+
|
24
96
|
|
25
97
|
## Hooks
|
26
98
|
|
27
|
-
To run a hook when ActiveModelSerializers is loaded, use
|
99
|
+
To run a hook when ActiveModelSerializers is loaded, use
|
100
|
+
`ActiveSupport.on_load(:action_controller) do end`
|