active_model_serializers 0.10.0 → 0.10.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -5
- data/.travis.yml +17 -5
- data/CHANGELOG.md +126 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +166 -26
- data/Rakefile +3 -32
- data/active_model_serializers.gemspec +22 -25
- data/appveyor.yml +9 -3
- data/bin/rubocop +38 -0
- data/docs/README.md +2 -1
- data/docs/general/adapters.md +29 -11
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +70 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +1 -1
- data/docs/general/logging.md +7 -0
- data/docs/general/rendering.md +62 -24
- data/docs/general/serializers.md +121 -13
- data/docs/howto/add_pagination_links.md +16 -17
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +4 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +12 -4
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/serialize_poro.md +46 -5
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +67 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +13 -3
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/association.rb +62 -10
- data/lib/active_model/serializer/belongs_to_reflection.rb +4 -3
- data/lib/active_model/serializer/collection_serializer.rb +35 -12
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +82 -115
- data/lib/active_model/serializer/error_serializer.rb +11 -7
- data/lib/active_model/serializer/errors_serializer.rb +25 -20
- data/lib/active_model/serializer/has_many_reflection.rb +3 -3
- data/lib/active_model/serializer/has_one_reflection.rb +1 -4
- data/lib/active_model/serializer/lazy_association.rb +95 -0
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +127 -67
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +296 -79
- data/lib/active_model_serializers/adapter/attributes.rb +3 -66
- data/lib/active_model_serializers/adapter/base.rb +39 -39
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +32 -9
- data/lib/active_model_serializers/adapter/json_api.rb +71 -57
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +109 -28
- data/lib/active_model_serializers/railtie.rb +3 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/active_model_serializers.rb +15 -0
- data/lib/generators/rails/resource_override.rb +1 -1
- data/lib/generators/rails/serializer_generator.rb +4 -4
- data/lib/grape/active_model_serializers.rb +7 -5
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/lib/tasks/rubocop.rake +53 -0
- data/test/action_controller/adapter_selector_test.rb +14 -5
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/errors_test.rb +8 -9
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +29 -24
- data/test/action_controller/json_api/pagination_test.rb +19 -19
- data/test/action_controller/json_api/transform_test.rb +11 -3
- 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 +12 -6
- data/test/action_controller/serialization_test.rb +12 -9
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/model_test.rb +137 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/active_model_serializers/test/schema_test.rb +3 -2
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/has_many_test.rb +10 -2
- data/test/adapter/json/transform_test.rb +15 -15
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +17 -19
- data/test/adapter/json_api/fields_test.rb +12 -3
- data/test/adapter/json_api/has_many_test.rb +49 -20
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -0
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +33 -12
- data/test/adapter/json_api/links_test.rb +4 -2
- data/test/adapter/json_api/pagination_links_test.rb +35 -8
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +27 -2
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +263 -253
- data/test/adapter/json_api/type_test.rb +1 -1
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +16 -16
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +21 -10
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +235 -69
- data/test/collection_serializer_test.rb +25 -12
- data/test/fixtures/active_record.rb +45 -10
- data/test/fixtures/poro.rb +124 -181
- data/test/generators/serializer_generator_test.rb +23 -5
- data/test/grape_test.rb +170 -56
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +18 -22
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +178 -49
- data/test/serializers/attribute_test.rb +5 -3
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/caching_configuration_test_isolated.rb +6 -6
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/options_test.rb +17 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
- data/test/serializers/reflection_test.rb +427 -0
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serialization_test.rb +2 -2
- data/test/serializers/serializer_for_test.rb +12 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +2 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +13 -0
- metadata +105 -42
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/associations.rb +0 -100
- data/lib/active_model/serializer/attributes.rb +0 -82
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/configuration.rb +0 -35
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model/serializer/links.rb +0 -35
- data/lib/active_model/serializer/meta.rb +0 -29
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model/serializer/type.rb +0 -25
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -19,33 +19,36 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
spec.executables = []
|
21
21
|
|
22
|
-
spec.required_ruby_version = '>= 2.
|
22
|
+
spec.required_ruby_version = '>= 2.1'
|
23
23
|
|
24
|
-
rails_versions = '>= 4.
|
24
|
+
rails_versions = ['>= 4.1', '< 6']
|
25
25
|
spec.add_runtime_dependency 'activemodel', rails_versions
|
26
|
-
|
27
|
-
|
26
|
+
# 'activesupport', rails_versions
|
27
|
+
# 'builder'
|
28
28
|
|
29
29
|
spec.add_runtime_dependency 'actionpack', rails_versions
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
# 'activesupport', rails_versions
|
31
|
+
# 'rack'
|
32
|
+
# 'rack-test', '~> 0.6.2'
|
33
33
|
|
34
|
-
spec.
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
spec.add_development_dependency 'railties', rails_versions
|
35
|
+
# 'activesupport', rails_versions
|
36
|
+
# 'actionpack', rails_versions
|
37
|
+
# 'rake', '>= 0.8.7'
|
38
38
|
|
39
39
|
# 'activesupport', rails_versions
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
# 'i18n,
|
41
|
+
# 'tzinfo'
|
42
|
+
# 'minitest'
|
43
|
+
# 'thread_safe'
|
44
|
+
|
45
|
+
spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.2']
|
46
|
+
spec.add_runtime_dependency 'case_transform', '>= 0.2'
|
44
47
|
|
45
48
|
spec.add_development_dependency 'activerecord', rails_versions
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
# arel
|
50
|
+
# activesupport
|
51
|
+
# activemodel
|
49
52
|
|
50
53
|
# Soft dependency for pagination
|
51
54
|
spec.add_development_dependency 'kaminari', ' ~> 0.16.3'
|
@@ -54,13 +57,7 @@ Gem::Specification.new do |spec|
|
|
54
57
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
55
58
|
spec.add_development_dependency 'simplecov', '~> 0.11'
|
56
59
|
spec.add_development_dependency 'timecop', '~> 0.7'
|
57
|
-
spec.add_development_dependency 'grape', ['>= 0.13', '< 1
|
60
|
+
spec.add_development_dependency 'grape', ['>= 0.13', '< 0.19.1']
|
58
61
|
spec.add_development_dependency 'json_schema'
|
59
62
|
spec.add_development_dependency 'rake', ['>= 10.0', '< 12.0']
|
60
|
-
|
61
|
-
spec.post_install_message = <<-EOF
|
62
|
-
NOTE: The default key case for the JsonApi adapter has changed to dashed.
|
63
|
-
See https://github.com/rails-api/active_model_serializers/blob/master/docs/general/key_transforms.md
|
64
|
-
for more information on configuring this behavior.
|
65
|
-
EOF
|
66
63
|
end
|
data/appveyor.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
version:
|
1
|
+
version: 1.0.{build}-{branch}
|
2
2
|
|
3
3
|
skip_tags: true
|
4
4
|
|
@@ -7,17 +7,23 @@ environment:
|
|
7
7
|
matrix:
|
8
8
|
- ruby_version: "Ruby21"
|
9
9
|
- ruby_version: "Ruby21-x64"
|
10
|
-
- ruby_version: "jruby-9.0.0.0"
|
11
10
|
|
12
11
|
cache:
|
13
12
|
- vendor/bundle
|
14
13
|
|
15
14
|
install:
|
16
15
|
- SET PATH=C:\%ruby_version%\bin;%PATH%
|
17
|
-
- gem
|
16
|
+
- gem update --system
|
17
|
+
- gem uninstall bundler -a -x
|
18
|
+
- gem install bundler -v 1.13.7
|
18
19
|
- bundle env
|
19
20
|
- bundle install --path=vendor/bundle --retry=3 --jobs=3
|
20
21
|
|
22
|
+
before_test:
|
23
|
+
- ruby -v
|
24
|
+
- gem -v
|
25
|
+
- bundle -v
|
26
|
+
|
21
27
|
test_script:
|
22
28
|
- bundle exec rake ci
|
23
29
|
|
data/bin/rubocop
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
#
|
3
|
+
# Usage:
|
4
|
+
# bin/rubocop [-A|-t|-h]
|
5
|
+
# bin/rubocop [file or path] [cli options]
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
# Autocorrect -A
|
9
|
+
# AutoGenConfig -t
|
10
|
+
# Usage -h,--help,help
|
11
|
+
|
12
|
+
set -e
|
13
|
+
|
14
|
+
case $1 in
|
15
|
+
-A)
|
16
|
+
echo "Rubocop autocorrect is ON" >&2
|
17
|
+
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_correct
|
18
|
+
;;
|
19
|
+
|
20
|
+
-t)
|
21
|
+
echo "Rubocop is generating a new TODO" >&2
|
22
|
+
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_gen_config
|
23
|
+
;;
|
24
|
+
|
25
|
+
-h|--help|help)
|
26
|
+
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
|
27
|
+
;;
|
28
|
+
|
29
|
+
*)
|
30
|
+
# with no args, run vanilla rubocop
|
31
|
+
# else assume we're passing in arbitrary arguments
|
32
|
+
if [ -z "$1" ]; then
|
33
|
+
bundle exec rake -f lib/tasks/rubocop.rake rubocop
|
34
|
+
else
|
35
|
+
bundle exec rubocop "$@"
|
36
|
+
fi
|
37
|
+
;;
|
38
|
+
esac
|
data/docs/README.md
CHANGED
@@ -18,16 +18,17 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
|
|
18
18
|
- JSON API
|
19
19
|
- [Schema](jsonapi/schema.md)
|
20
20
|
- [Errors](jsonapi/errors.md)
|
21
|
-
- [ARCHITECTURE](ARCHITECTURE.md)
|
22
21
|
|
23
22
|
## How to
|
24
23
|
|
25
24
|
- [How to add root key](howto/add_root_key.md)
|
26
25
|
- [How to add pagination links](howto/add_pagination_links.md)
|
26
|
+
- [How to add relationship links](howto/add_relationship_links.md)
|
27
27
|
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
|
28
28
|
- [Testing ActiveModelSerializers](howto/test.md)
|
29
29
|
- [Passing Arbitrary Options](howto/passing_arbitrary_options.md)
|
30
30
|
- [How to serialize a Plain-Old Ruby Object (PORO)](howto/serialize_poro.md)
|
31
|
+
- [How to upgrade from `0.8` to `0.10` safely](howto/upgrade_from_0_8_to_0_10.md)
|
31
32
|
|
32
33
|
## Integrations
|
33
34
|
|
data/docs/general/adapters.md
CHANGED
@@ -67,9 +67,11 @@ Doesn't follow any specific convention.
|
|
67
67
|
|
68
68
|
### JSON
|
69
69
|
|
70
|
-
The response
|
70
|
+
The json response is always rendered with a root key.
|
71
71
|
|
72
|
-
The root key
|
72
|
+
The root key can be overridden by:
|
73
|
+
* passing the `root` option in the render call. See details in the [Rendering Guides](rendering.md#overriding-the-root-key).
|
74
|
+
* setting the `type` of the serializer. See details in the [Serializers Guide](serializers.md#type).
|
73
75
|
|
74
76
|
Doesn't follow any specific convention.
|
75
77
|
|
@@ -139,18 +141,25 @@ This adapter follows **version 1.0** of the [format specified](../jsonapi/schema
|
|
139
141
|
}
|
140
142
|
```
|
141
143
|
|
142
|
-
|
144
|
+
### Include option
|
143
145
|
|
144
|
-
|
145
|
-
when the resource names are included in the `include` option.
|
146
|
-
Including nested associated resources is also supported.
|
146
|
+
Which [serializer associations](https://github.com/rails-api/active_model_serializers/blob/master/docs/general/serializers.md#associations) are rendered can be specified using the `include` option. The option usage is consistent with [the include option in the JSON API spec](http://jsonapi.org/format/#fetching-includes), and is available in all adapters.
|
147
147
|
|
148
|
+
Example of the usage:
|
148
149
|
```ruby
|
149
150
|
render json: @posts, include: ['author', 'comments', 'comments.author']
|
150
151
|
# or
|
151
152
|
render json: @posts, include: 'author,comments,comments.author'
|
152
153
|
```
|
153
154
|
|
155
|
+
The format of the `include` option can be either:
|
156
|
+
|
157
|
+
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
|
158
|
+
- an Array of Symbols and Hashes.
|
159
|
+
- a mix of both.
|
160
|
+
|
161
|
+
An empty string or an empty array will prevent rendering of any associations.
|
162
|
+
|
154
163
|
In addition, two types of wildcards may be used:
|
155
164
|
|
156
165
|
- `*` includes one level of associations.
|
@@ -162,11 +171,6 @@ These can be combined with other paths.
|
|
162
171
|
render json: @posts, include: '**' # or '*' for a single layer
|
163
172
|
```
|
164
173
|
|
165
|
-
The format of the `include` option can be either:
|
166
|
-
|
167
|
-
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
|
168
|
-
- an Array of Symbols and Hashes.
|
169
|
-
- a mix of both.
|
170
174
|
|
171
175
|
The following would render posts and include:
|
172
176
|
|
@@ -180,6 +184,20 @@ It could be combined, like above, with other paths in any combination desired.
|
|
180
184
|
render json: @posts, include: 'author.comments.**'
|
181
185
|
```
|
182
186
|
|
187
|
+
**Note:** Wildcards are ActiveModelSerializers-specific, they are not part of the JSON API spec.
|
188
|
+
|
189
|
+
The default include for the JSON API adapter is no associations. The default for the JSON and Attributes adapters is all associations.
|
190
|
+
|
191
|
+
For the JSON API adapter associated resources will be gathered in the `"included"` member. For the JSON and Attributes
|
192
|
+
adapters associated resources will be rendered among the other attributes.
|
193
|
+
|
194
|
+
Only for the JSON API adapter you can specify, which attributes of associated resources will be rendered. This feature
|
195
|
+
is called [sparse fieldset](http://jsonapi.org/format/#fetching-sparse-fieldsets):
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
render json: @posts, include: 'comments', fields: { comments: ['content', 'created_at'] }
|
199
|
+
```
|
200
|
+
|
183
201
|
##### Security Considerations
|
184
202
|
|
185
203
|
Since the included options may come from the query params (i.e. user-controller):
|
data/docs/general/caching.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
# Caching
|
4
4
|
|
5
|
+
## Warning
|
6
|
+
|
7
|
+
There is currently a problem with caching in AMS [Caching doesn't improve performance](https://github.com/rails-api/active_model_serializers/issues/1586). Adding caching _may_ slow down your application, rather than speeding it up. We suggest you benchmark any caching you implement before using in a production enviroment
|
8
|
+
|
9
|
+
___
|
10
|
+
|
5
11
|
To cache a serializer, call ```cache``` and pass its options.
|
6
12
|
The options are the same options of ```ActiveSupport::Cache::Store```, plus
|
7
13
|
a ```key``` option that will be the prefix of the object cache
|
@@ -17,7 +23,7 @@ The cache support is optimized to use the cached object in multiple request. An
|
|
17
23
|
cache(options = nil) # options: ```{key, expires_in, compress, force, race_condition_ttl}```
|
18
24
|
```
|
19
25
|
|
20
|
-
Take the example
|
26
|
+
Take the example below:
|
21
27
|
|
22
28
|
```ruby
|
23
29
|
class PostSerializer < ActiveModel::Serializer
|
@@ -46,15 +46,71 @@ Each adapter has a default key transform configured:
|
|
46
46
|
|
47
47
|
| Adapter | Default Key Transform |
|
48
48
|
|----|----|
|
49
|
+
| `Attributes` | `:unaltered` |
|
49
50
|
| `Json` | `:unaltered` |
|
50
51
|
| `JsonApi` | `:dash` |
|
51
52
|
|
52
53
|
`config.key_transform` is a global override of the adapter default. Adapters
|
53
54
|
still prefer the render option `:key_transform` over this setting.
|
54
55
|
|
56
|
+
*NOTE: Key transforms can be expensive operations. If key transforms are unnecessary for the
|
57
|
+
application, setting `config.key_transform` to `:unaltered` will provide a performance boost.*
|
55
58
|
|
56
|
-
|
59
|
+
##### default_includes
|
60
|
+
What relationships to serialize by default. Default: `'*'`, which includes one level of related
|
61
|
+
objects. See [includes](adapters.md#included) for more info.
|
62
|
+
|
63
|
+
|
64
|
+
##### serializer_lookup_chain
|
65
|
+
|
66
|
+
Configures how serializers are searched for. By default, the lookup chain is
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
ActiveModelSerializers::LookupChain::DEFAULT
|
70
|
+
```
|
71
|
+
|
72
|
+
which is shorthand for
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
[
|
76
|
+
ActiveModelSerializers::LookupChain::BY_PARENT_SERIALIZER,
|
77
|
+
ActiveModelSerializers::LookupChain::BY_NAMESPACE,
|
78
|
+
ActiveModelSerializers::LookupChain::BY_RESOURCE_NAMESPACE,
|
79
|
+
ActiveModelSerializers::LookupChain::BY_RESOURCE
|
80
|
+
]
|
81
|
+
```
|
82
|
+
|
83
|
+
Each of the array entries represent a proc. A serializer lookup proc will be yielded 3 arguments. `resource_class`, `serializer_class`, and `namespace`.
|
84
|
+
|
85
|
+
Note that:
|
86
|
+
- `resource_class` is the class of the resource being rendered
|
87
|
+
- by default `serializer_class` is `ActiveModel::Serializer`
|
88
|
+
- for association lookup it's the "parent" serializer
|
89
|
+
- `namespace` correspond to either the controller namespace or the [optionally] specified [namespace render option](./rendering.md#namespace)
|
90
|
+
|
91
|
+
An example config could be:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
ActiveModelSerializers.config.serializer_lookup_chain = [
|
95
|
+
lambda do |resource_class, serializer_class, namespace|
|
96
|
+
"API::#{namespace}::#{resource_class}"
|
97
|
+
end
|
98
|
+
]
|
99
|
+
```
|
57
100
|
|
101
|
+
If you simply want to add to the existing lookup_chain. Use `unshift`.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
ActiveModelSerializers.config.serializer_lookup_chain.unshift(
|
105
|
+
lambda do |resource_class, serializer_class, namespace|
|
106
|
+
# ...
|
107
|
+
end
|
108
|
+
)
|
109
|
+
```
|
110
|
+
|
111
|
+
See [lookup_chain.rb](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/lookup_chain.rb) for further explanations and examples.
|
112
|
+
|
113
|
+
## JSON API
|
58
114
|
|
59
115
|
##### jsonapi_resource_type
|
60
116
|
|
@@ -67,6 +123,19 @@ Possible values:
|
|
67
123
|
- `:singular`
|
68
124
|
- `:plural` (default)
|
69
125
|
|
126
|
+
##### jsonapi_namespace_separator
|
127
|
+
|
128
|
+
Sets separator string for namespaced models to render `type` attribute.
|
129
|
+
|
130
|
+
|
131
|
+
| Separator | Example: Admin::User |
|
132
|
+
|----|----|
|
133
|
+
| `'-'` (default) | 'admin-users'
|
134
|
+
| `'--'` (recommended) | 'admin--users'
|
135
|
+
|
136
|
+
See [Recommendation for dasherizing (kebab-case-ing) namespaced object, such as `Admin::User`](https://github.com/json-api/json-api/issues/850)
|
137
|
+
for more discussion.
|
138
|
+
|
70
139
|
##### jsonapi_include_toplevel_object
|
71
140
|
|
72
141
|
Include a [top level jsonapi member](http://jsonapi.org/format/#document-jsonapi-object)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
[Back to Guides](../README.md)
|
2
|
+
|
3
|
+
# Fields
|
4
|
+
|
5
|
+
If for any reason, you need to restrict the fields returned, you should use `fields` option.
|
6
|
+
|
7
|
+
For example, if you have a serializer like this
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class UserSerializer < ActiveModel::Serializer
|
11
|
+
attributes :access_token, :first_name, :last_name
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
and in a specific controller, you want to return `access_token` only, `fields` will help you:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class AnonymousController < ApplicationController
|
19
|
+
def create
|
20
|
+
render json: User.create(activation_state: 'anonymous'), fields: [:access_token], status: 201
|
21
|
+
end
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
Note that this is only valid for the `json` and `attributes` adapter. For the `json_api` adapter, you would use
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
render json: @user, fields: { users: [:access_token] }
|
29
|
+
```
|
30
|
+
|
31
|
+
Where `users` is the JSONAPI type.
|
data/docs/general/logging.md
CHANGED
@@ -12,3 +12,10 @@ You may customize the logger in an initializer, for example:
|
|
12
12
|
```ruby
|
13
13
|
ActiveModelSerializers.logger = Logger.new(STDOUT)
|
14
14
|
```
|
15
|
+
|
16
|
+
You can also disable the logger, just put this in `config/initializers/active_model_serializers.rb`:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'active_model_serializers'
|
20
|
+
ActiveSupport::Notifications.unsubscribe(ActiveModelSerializers::Logging::RENDER_EVENT)
|
21
|
+
```
|
data/docs/general/rendering.md
CHANGED
@@ -48,42 +48,36 @@ render json: @posts, serializer: CollectionSerializer, each_serializer: PostPrev
|
|
48
48
|
|
49
49
|
## Serializing non-ActiveRecord objects
|
50
50
|
|
51
|
-
|
52
|
-
[ActiveModel::Serializer::Lint::Tests](../../lib/active_model/serializer/lint.rb#L17).
|
53
|
-
|
54
|
-
See the ActiveModelSerializers::Model for a base class that implements the full
|
55
|
-
API for a plain-old Ruby object (PORO).
|
51
|
+
See [README](../../README.md#what-does-a-serializable-resource-look-like)
|
56
52
|
|
57
53
|
## SerializableResource options
|
58
54
|
|
59
|
-
|
60
|
-
are partitioned into `serializer_opts` and `adapter_opts`. `adapter_opts` are passed to new Adapters;
|
61
|
-
`serializer_opts` are passed to new Serializers.
|
62
|
-
|
63
|
-
The `adapter_opts` are specified in [ActiveModelSerializers::SerializableResource::ADAPTER_OPTIONS](../../lib/active_model_serializers/serializable_resource.rb#L5).
|
64
|
-
The `serializer_opts` are the remaining options.
|
65
|
-
|
66
|
-
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
|
67
|
-
methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
|
68
|
-
to know about, but not part of ActiveModelSerializers.)
|
69
|
-
|
70
|
-
See [ARCHITECTURE](../ARCHITECTURE.md) for more information.
|
55
|
+
See [README](../../README.md#activemodelserializersserializableresource)
|
71
56
|
|
72
57
|
### adapter_opts
|
73
58
|
|
74
59
|
#### fields
|
75
60
|
|
76
|
-
|
61
|
+
If you are using `json` or `attributes` adapter
|
62
|
+
```ruby
|
63
|
+
render json: @user, fields: [:access_token]
|
64
|
+
```
|
65
|
+
|
66
|
+
See [Fields](fields.md) for more information.
|
77
67
|
|
78
68
|
#### adapter
|
79
69
|
|
80
|
-
|
70
|
+
This option lets you explicitly set the adapter to be used by passing a registered adapter. Your options are `:attributes`, `:json`, and `:json_api`.
|
71
|
+
|
72
|
+
```
|
73
|
+
ActiveModel::Serializer.config.adapter = :json_api
|
74
|
+
```
|
81
75
|
|
82
76
|
#### key_transform
|
83
77
|
|
84
78
|
```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower```
|
85
79
|
|
86
|
-
See [Key Transforms](key_transforms.md) for more
|
80
|
+
See [Key Transforms](key_transforms.md) for more information.
|
87
81
|
|
88
82
|
#### meta
|
89
83
|
|
@@ -209,7 +203,7 @@ link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_
|
|
209
203
|
|
210
204
|
#### include
|
211
205
|
|
212
|
-
|
206
|
+
See [Adapters: Include Option](/docs/general/adapters.md#include-option).
|
213
207
|
|
214
208
|
#### Overriding the root key
|
215
209
|
|
@@ -234,17 +228,61 @@ This will be rendered as:
|
|
234
228
|
```
|
235
229
|
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.
|
236
230
|
|
231
|
+
#### namespace
|
232
|
+
|
233
|
+
The namespace for serializer lookup is based on the controller.
|
234
|
+
|
235
|
+
To configure the implicit namespace, in your controller, create a before filter
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
before_action do
|
239
|
+
self.namespace_for_serializer = Api::V2
|
240
|
+
end
|
241
|
+
```
|
242
|
+
|
243
|
+
`namespace` can also be passed in as a render option:
|
244
|
+
|
245
|
+
|
246
|
+
```ruby
|
247
|
+
@post = Post.first
|
248
|
+
render json: @post, namespace: Api::V2
|
249
|
+
```
|
250
|
+
|
251
|
+
This tells the serializer lookup to check for the existence of `Api::V2::PostSerializer`, and if any relations are rendered with `@post`, they will also utilize the `Api::V2` namespace.
|
252
|
+
|
253
|
+
The `namespace` can be any object whose namespace can be represented by string interpolation (i.e. by calling to_s)
|
254
|
+
- Module `Api::V2`
|
255
|
+
- String `'Api::V2'`
|
256
|
+
- Symbol `:'Api::V2'`
|
257
|
+
|
258
|
+
Note that by using a string and symbol, Ruby will assume the namespace is defined at the top level.
|
259
|
+
|
260
|
+
|
237
261
|
#### serializer
|
238
262
|
|
239
|
-
|
263
|
+
Specify which serializer to use if you want to use a serializer other than the default.
|
264
|
+
|
265
|
+
For a single resource:
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
@post = Post.first
|
269
|
+
render json: @post, serializer: SpecialPostSerializer
|
270
|
+
```
|
271
|
+
|
272
|
+
To specify which serializer to use on individual items in a collection (i.e., an `index` action), use `each_serializer`:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
@posts = Post.all
|
276
|
+
render json: @posts, each_serializer: SpecialPostSerializer
|
277
|
+
```
|
240
278
|
|
241
279
|
#### scope
|
242
280
|
|
243
|
-
|
281
|
+
See [Serializers: Scope](/docs/general/serializers.md#scope).
|
244
282
|
|
245
283
|
#### scope_name
|
246
284
|
|
247
|
-
|
285
|
+
See [Serializers: Scope](/docs/general/serializers.md#scope).
|
248
286
|
|
249
287
|
## Using a serializer without `render`
|
250
288
|
|