active_model_serializers 0.10.0 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +39 -2
- data/Gemfile +1 -1
- data/README.md +21 -24
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +19 -23
- data/docs/general/adapters.md +4 -2
- data/docs/general/configuration_options.md +6 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/rendering.md +7 -2
- data/docs/general/serializers.md +62 -3
- data/docs/howto/add_pagination_links.md +2 -3
- data/docs/integrations/ember-and-json-api.md +25 -10
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +4 -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/associations.rb +6 -7
- data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
- data/lib/active_model/serializer/caching.rb +67 -112
- data/lib/active_model/serializer/collection_serializer.rb +30 -10
- data/lib/active_model/serializer/configuration.rb +1 -0
- 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 +0 -3
- data/lib/active_model/serializer/has_one_reflection.rb +0 -3
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +3 -3
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +57 -15
- data/lib/active_model_serializers/adapter/attributes.rb +2 -67
- data/lib/active_model_serializers/adapter/base.rb +38 -38
- 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.rb +36 -28
- 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/model.rb +3 -1
- 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/serialization_context.rb +10 -3
- data/lib/active_model_serializers.rb +6 -0
- data/lib/generators/rails/serializer_generator.rb +3 -3
- 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/test/action_controller/adapter_selector_test.rb +4 -4
- 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 +6 -7
- 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/serialization_test.rb +10 -7
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/key_transform_test.rb +254 -252
- data/test/active_model_serializers/model_test.rb +6 -4
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/transform_test.rb +14 -14
- 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/has_many_test.rb +18 -18
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +9 -6
- data/test/adapter/json_api/links_test.rb +3 -1
- data/test/adapter/json_api/pagination_links_test.rb +19 -7
- data/test/adapter/json_api/relationships_test.rb +9 -4
- data/test/adapter/json_api/resource_identifier_test.rb +7 -2
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +251 -250
- 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/bm_caching.rb +15 -15
- data/test/benchmark/bm_transform.rb +16 -5
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +101 -45
- data/test/collection_serializer_test.rb +2 -2
- data/test/fixtures/poro.rb +8 -7
- data/test/grape_test.rb +152 -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/associations_test.rb +10 -10
- data/test/serializers/attribute_test.rb +1 -1
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +3 -1
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +0 -9
- data/test/support/serialization_testing.rb +7 -5
- metadata +54 -24
- data/.rubocop_todo.yml +0 -167
- data/lib/active_model/serializer/include_tree.rb +0 -111
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9ab72fd33fbf20e8630b9109d18edda9272658e
|
4
|
+
data.tar.gz: 8654e40594d8bf26038b329a8044dc9197bc5733
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 454504b650ab8302e6e1f87b15f952ff3068033bcaaac136764211fbcb724759b354d66b7e0b662e1e10a1b57734173b4843f96eb2344c164d568fae824da8b3
|
7
|
+
data.tar.gz: 56355761907c1ea7882d7b04dc8adcff8538f188bd418946c2493a267188cedfef4247e410206b4c479575ba926efa1259c5966e166b901eaea26c45b7d4aae8
|
data/.rubocop.yml
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
inherit_from: .rubocop_todo.yml
|
2
|
-
|
3
1
|
AllCops:
|
4
|
-
TargetRubyVersion: 2.
|
2
|
+
TargetRubyVersion: 2.1
|
5
3
|
Exclude:
|
6
4
|
- config/initializers/forbidden_yaml.rb
|
7
5
|
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
|
@@ -46,7 +44,7 @@ Style/AlignParameters:
|
|
46
44
|
EnforcedStyle: with_fixed_indentation
|
47
45
|
|
48
46
|
Style/ClassAndModuleChildren:
|
49
|
-
EnforcedStyle:
|
47
|
+
EnforcedStyle: nested
|
50
48
|
|
51
49
|
Style/Documentation:
|
52
50
|
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,44 @@
|
|
1
1
|
## 0.10.x
|
2
2
|
|
3
|
-
### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.
|
3
|
+
### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.2...master)
|
4
4
|
|
5
|
-
|
5
|
+
Breaking changes:
|
6
|
+
|
7
|
+
Features:
|
8
|
+
|
9
|
+
Fixes:
|
10
|
+
|
11
|
+
Misc:
|
12
|
+
|
13
|
+
### [v0.10.2 (2016-07-05)(unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
|
14
|
+
|
15
|
+
Fixes:
|
16
|
+
- [#1814] (https://github.com/rails-api/active_model_serializers/pull/1814) Ensuring read_multi works with fragment cache
|
17
|
+
|
18
|
+
Misc:
|
19
|
+
- [#1808](https://github.com/rails-api/active_model_serializers/pull/1808) Adds documentation for `fields` option. (@luizkowalski)
|
20
|
+
|
21
|
+
### [v0.10.1 (2016-06-16)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0...v0.10.1)
|
22
|
+
|
23
|
+
Features:
|
24
|
+
- [#1668](https://github.com/rails-api/active_model_serializers/pull/1668) Exclude nil and empty links. (@sigmike)
|
25
|
+
- [#1426](https://github.com/rails-api/active_model_serializers/pull/1426) Add ActiveModelSerializers.config.default_includes (@empact)
|
26
|
+
|
27
|
+
Fixes:
|
28
|
+
- [#1754](https://github.com/rails-api/active_model_serializers/pull/1754) Fixes #1759, Grape integration, improves serialization_context
|
29
|
+
missing error message on pagination. Document overriding CollectionSerializer#paginated?. (@bf4)
|
30
|
+
Moved serialization_context creation to Grape formatter, so resource serialization works without explicit calls to the `render` helper method.
|
31
|
+
Added Grape collection tests. (@onomated)
|
32
|
+
- [#1287](https://github.com/rails-api/active_model_serializers/pull/1287) Pass `fields` options from adapter to serializer. (@vasilakisfil)
|
33
|
+
- [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option
|
34
|
+
is set to `false`. (@groyoh)
|
35
|
+
- [#1747](https://github.com/rails-api/active_model_serializers/pull/1747) Improve jsonapi mime type registration for Rails 5 (@remear)
|
36
|
+
|
37
|
+
Misc:
|
38
|
+
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
|
39
|
+
- [#1685](https://github.com/rails-api/active_model_serializers/pull/1685) Replace `IncludeTree` with `IncludeDirective` from the jsonapi gem.
|
40
|
+
|
41
|
+
### [v0.10.0 (2016-05-17)](https://github.com/rails-api/active_model_serializers/compare/4a2d9853ba7...v0.10.0)
|
6
42
|
|
7
43
|
Breaking changes:
|
8
44
|
- [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear)
|
@@ -22,6 +58,7 @@ Fixes:
|
|
22
58
|
|
23
59
|
Misc:
|
24
60
|
- [#1673](https://github.com/rails-api/active_model_serializers/pull/1673) Adds "How to" guide on using AMS with POROs (@DrSayre)
|
61
|
+
- [#1730](https://github.com/rails-api/active_model_serializers/pull/1730) Adds documentation for overriding default serializer based on conditions (@groyoh/@cgmckeever)
|
25
62
|
|
26
63
|
### [v0.10.0.rc5 (2016-04-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc4...v0.10.0.rc5)
|
27
64
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -24,17 +24,6 @@
|
|
24
24
|
</tr>
|
25
25
|
</table>
|
26
26
|
|
27
|
-
|
28
|
-
## Documentation
|
29
|
-
|
30
|
-
- [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
|
31
|
-
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/v0.10.0)
|
32
|
-
- [Guides](docs)
|
33
|
-
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
34
|
-
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
|
35
|
-
- [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
36
|
-
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-8-stable)
|
37
|
-
|
38
27
|
## About
|
39
28
|
|
40
29
|
ActiveModelSerializers brings convention over configuration to your JSON generation.
|
@@ -50,33 +39,24 @@ resource serialization. The serialization has the `#as_json`, `#to_json` and `#s
|
|
50
39
|
methods used by the Rails JSON Renderer. (SerializableResource actually delegates
|
51
40
|
these methods to the adapter.)
|
52
41
|
|
53
|
-
By default ActiveModelSerializers will use the **Attributes Adapter
|
42
|
+
By default ActiveModelSerializers will use the **Attributes Adapter** (no JSON root).
|
54
43
|
But we strongly advise you to use **JsonApi Adapter**, which
|
55
44
|
follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
|
56
45
|
Check how to change the adapter in the sections below.
|
57
46
|
|
58
|
-
## RELEASE CANDIDATE, PLEASE READ
|
59
|
-
|
60
|
-
This is the **master** branch of ActiveModelSerializers.
|
61
|
-
|
62
|
-
It will become the `0.10.0` release when it's ready. Currently this is a release candidate.
|
63
|
-
|
64
47
|
`0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
|
65
48
|
|
66
|
-
`0.10.x`
|
49
|
+
`0.10.x` is based on the `0.8.0` code, but with a more flexible
|
67
50
|
architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
|
68
51
|
|
69
52
|
It is generally safe and recommended to use the master branch.
|
70
53
|
|
71
|
-
For more information, see the post '[The future of
|
72
|
-
AMS](https://medium.com/@joaomdmoura/the-future-of-ams-e5f9047ca7e9)'.
|
73
|
-
|
74
54
|
## Installation
|
75
55
|
|
76
56
|
Add this line to your application's Gemfile:
|
77
57
|
|
78
58
|
```
|
79
|
-
gem 'active_model_serializers'
|
59
|
+
gem 'active_model_serializers', '~> 0.10.0'
|
80
60
|
```
|
81
61
|
|
82
62
|
And then execute:
|
@@ -103,8 +83,25 @@ If you'd like to chat, we have a [community slack](http://amserializers.herokuap
|
|
103
83
|
|
104
84
|
Thanks!
|
105
85
|
|
86
|
+
## Documentation
|
87
|
+
|
88
|
+
- [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
|
89
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/v0.10.0)
|
90
|
+
- [Guides](docs)
|
91
|
+
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
92
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
|
93
|
+
- [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
94
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-8-stable)
|
95
|
+
|
96
|
+
|
106
97
|
## High-level behavior
|
107
98
|
|
99
|
+
Choose an adapter from [adapters](lib/active_model_serializers/adapter):
|
100
|
+
|
101
|
+
``` ruby
|
102
|
+
ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
|
103
|
+
```
|
104
|
+
|
108
105
|
Given a [serializable model](lib/active_model/serializer/lint.rb):
|
109
106
|
|
110
107
|
```ruby
|
@@ -162,6 +159,6 @@ serializer.associations
|
|
162
159
|
```
|
163
160
|
See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for more information.
|
164
161
|
|
165
|
-
|
162
|
+
## Contributing
|
166
163
|
|
167
164
|
See [CONTRIBUTING.md](CONTRIBUTING.md)
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ rescue LoadError
|
|
5
5
|
end
|
6
6
|
begin
|
7
7
|
require 'simplecov'
|
8
|
-
rescue LoadError
|
8
|
+
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
9
9
|
end
|
10
10
|
|
11
11
|
Bundler::GemHelper.install_tasks
|
@@ -33,7 +33,7 @@ end
|
|
33
33
|
begin
|
34
34
|
require 'rubocop'
|
35
35
|
require 'rubocop/rake_task'
|
36
|
-
rescue LoadError
|
36
|
+
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
37
37
|
else
|
38
38
|
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
|
39
39
|
require 'rbconfig'
|
@@ -100,4 +100,4 @@ else
|
|
100
100
|
end
|
101
101
|
|
102
102
|
desc 'CI test task'
|
103
|
-
task :
|
103
|
+
task ci: [:default]
|
@@ -19,33 +19,35 @@ 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
34
|
spec.add_runtime_dependency 'railties', rails_versions
|
35
|
-
|
36
|
-
|
37
|
-
|
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', '~> 0.1.1.beta2'
|
44
46
|
|
45
47
|
spec.add_development_dependency 'activerecord', rails_versions
|
46
|
-
|
47
|
-
|
48
|
-
|
48
|
+
# arel
|
49
|
+
# activesupport
|
50
|
+
# activemodel
|
49
51
|
|
50
52
|
# Soft dependency for pagination
|
51
53
|
spec.add_development_dependency 'kaminari', ' ~> 0.16.3'
|
@@ -57,10 +59,4 @@ Gem::Specification.new do |spec|
|
|
57
59
|
spec.add_development_dependency 'grape', ['>= 0.13', '< 1.0']
|
58
60
|
spec.add_development_dependency 'json_schema'
|
59
61
|
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
62
|
end
|
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
|
|
@@ -52,9 +52,14 @@ Each adapter has a default key transform configured:
|
|
52
52
|
`config.key_transform` is a global override of the adapter default. Adapters
|
53
53
|
still prefer the render option `:key_transform` over this setting.
|
54
54
|
|
55
|
+
*NOTE: Key transforms can be expensive operations. If key transforms are unnecessary for the
|
56
|
+
application, setting `config.key_transform` to `:unaltered` will provide a performance boost.*
|
55
57
|
|
56
|
-
|
58
|
+
##### default_includes
|
59
|
+
What relationships to serialize by default. Default: `'*'`, which includes one level of related
|
60
|
+
objects. See [includes](adapters.md#included) for more info.
|
57
61
|
|
62
|
+
## JSON API
|
58
63
|
|
59
64
|
##### jsonapi_resource_type
|
60
65
|
|
@@ -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/rendering.md
CHANGED
@@ -73,7 +73,12 @@ See [ARCHITECTURE](../ARCHITECTURE.md) for more information.
|
|
73
73
|
|
74
74
|
#### fields
|
75
75
|
|
76
|
-
|
76
|
+
If you are using `json` or `attributes` adapter
|
77
|
+
```ruby
|
78
|
+
render json: @user, fields: [:access_token]
|
79
|
+
```
|
80
|
+
|
81
|
+
See [Fields](fields.md) for more information.
|
77
82
|
|
78
83
|
#### adapter
|
79
84
|
|
@@ -83,7 +88,7 @@ PR please :)
|
|
83
88
|
|
84
89
|
```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower```
|
85
90
|
|
86
|
-
See [Key Transforms](key_transforms.md) for more
|
91
|
+
See [Key Transforms](key_transforms.md) for more information.
|
87
92
|
|
88
93
|
#### meta
|
89
94
|
|
data/docs/general/serializers.md
CHANGED
@@ -31,10 +31,21 @@ Serialization of the resource `title`
|
|
31
31
|
|---------------------------- |-------------|
|
32
32
|
| `attribute :title` | `{ title: 'Some Title' } `
|
33
33
|
| `attribute :title, key: :name` | `{ name: 'Some Title' } `
|
34
|
-
| `attribute
|
34
|
+
| `attribute(:title) { 'A Different Title'}` | `{ title: 'A Different Title' } `
|
35
35
|
| `attribute :title`<br>`def title 'A Different Title' end` | `{ title: 'A Different Title' }`
|
36
36
|
|
37
|
-
|
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
|
+
```
|
38
49
|
|
39
50
|
### Associations
|
40
51
|
|
@@ -255,7 +266,7 @@ In the controller, the scope/scope_name options are equal to
|
|
255
266
|
the [`serialization_scope`method](https://github.com/rails-api/active_model_serializers/blob/d02cd30fe55a3ea85e1d351b6e039620903c1871/lib/action_controller/serialization.rb#L13-L20),
|
256
267
|
which is `:current_user`, by default.
|
257
268
|
|
258
|
-
|
269
|
+
Specifically, the `scope_name` is defaulted to `:current_user`, and may be set as
|
259
270
|
`serialization_scope :view_context`. The `scope` is set to `send(scope_name)` when `scope_name` is
|
260
271
|
present and the controller responds to `scope_name`.
|
261
272
|
|
@@ -303,6 +314,38 @@ So that when we render the `#edit` action, we'll get
|
|
303
314
|
|
304
315
|
Where `can_edit` is `view_context.current_user.admin?` (true).
|
305
316
|
|
317
|
+
You can also tell what to set as `serialization_scope` for specific actions.
|
318
|
+
|
319
|
+
For example, use `admin_user` only for `Admin::PostSerializer` and `current_user` for rest.
|
320
|
+
|
321
|
+
```ruby
|
322
|
+
class PostsController < ActionController::Base
|
323
|
+
|
324
|
+
before_action only: :edit do
|
325
|
+
self.class.serialization_scope :admin_user
|
326
|
+
end
|
327
|
+
|
328
|
+
def show
|
329
|
+
render json: @post, serializer: PostSerializer
|
330
|
+
end
|
331
|
+
|
332
|
+
def edit
|
333
|
+
@post.save
|
334
|
+
render json: @post, serializer: Admin::PostSerializer
|
335
|
+
end
|
336
|
+
|
337
|
+
private
|
338
|
+
|
339
|
+
def admin_user
|
340
|
+
User.new(id: 2, name: 'Bob', admin: true)
|
341
|
+
end
|
342
|
+
|
343
|
+
def current_user
|
344
|
+
User.new(id: 2, name: 'Bob', admin: false)
|
345
|
+
end
|
346
|
+
end
|
347
|
+
```
|
348
|
+
|
306
349
|
#### #read_attribute_for_serialization(key)
|
307
350
|
|
308
351
|
The serialized value for a given key. e.g. `read_attribute_for_serialization(:title) #=> 'Hello World'`
|
@@ -370,3 +413,19 @@ class PostSerializer < ActiveModel::Serializer
|
|
370
413
|
end
|
371
414
|
end
|
372
415
|
```
|
416
|
+
|
417
|
+
## Overriding association serializer lookup
|
418
|
+
|
419
|
+
If you want to define a specific serializer lookup for your associations, you can override
|
420
|
+
the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions.
|
421
|
+
|
422
|
+
```ruby
|
423
|
+
class MySerializer < ActiveModel::Serializer
|
424
|
+
def self.serializer_for(model, options)
|
425
|
+
return SparseAdminSerializer if model.class == 'Admin'
|
426
|
+
super
|
427
|
+
end
|
428
|
+
|
429
|
+
# the rest of the serializer
|
430
|
+
end
|
431
|
+
```
|
@@ -81,7 +81,7 @@ def pagination_dict(object)
|
|
81
81
|
{
|
82
82
|
current_page: object.current_page,
|
83
83
|
next_page: object.next_page,
|
84
|
-
prev_page: object.prev_page,
|
84
|
+
prev_page: object.prev_page, # use object.previous_page when using will_paginate
|
85
85
|
total_pages: object.total_pages,
|
86
86
|
total_count: object.total_count
|
87
87
|
}
|
@@ -126,14 +126,13 @@ def meta_attributes(resource, extra_meta = {})
|
|
126
126
|
{
|
127
127
|
current_page: resource.current_page,
|
128
128
|
next_page: resource.next_page,
|
129
|
-
prev_page: resource.prev_page,
|
129
|
+
prev_page: resource.prev_page, # use resource.previous_page when using will_paginate
|
130
130
|
total_pages: resource.total_pages,
|
131
131
|
total_count: resource.total_count
|
132
132
|
}.merge(extra_meta)
|
133
133
|
end
|
134
134
|
```
|
135
135
|
|
136
|
-
|
137
136
|
### Attributes adapter
|
138
137
|
|
139
138
|
This adapter does not allow us to use `meta` key, due to that it is not possible to add pagination links.
|
@@ -17,17 +17,32 @@ To solve this, in Ember, both the adapter and the serializer will need some modi
|
|
17
17
|
|
18
18
|
### Server-Side Changes
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
First, set the adapter type in an initializer file:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# config/initializers/active_model_serializers.rb
|
24
|
+
ActiveModelSerializers.config.adapter = :json_api
|
25
|
+
```
|
26
|
+
|
27
|
+
or:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# config/initializers/active_model_serializers.rb
|
31
|
+
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::JsonApi
|
32
|
+
```
|
33
|
+
|
34
|
+
You will also want to set the `key_transform` to `:unaltered` since you will adjust the attributes in your Ember serializer to use underscores instead of dashes later. You could also use `:underscore`, but `:unaltered` is better for performance.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# config/initializers/active_model_serializers.rb
|
38
|
+
ActiveModelSerializers.config.key_transform = :unaltered
|
39
|
+
```
|
40
|
+
|
41
|
+
Lastly, in order to properly handle JSON API responses, we need to register a JSON API renderer, like so:
|
42
|
+
|
22
43
|
```ruby
|
23
|
-
|
24
|
-
|
25
|
-
text/x-json
|
26
|
-
application/json
|
27
|
-
)
|
28
|
-
|
29
|
-
Mime::Type.unregister :json
|
30
|
-
Mime::Type.register 'application/json', :json, api_mime_types
|
44
|
+
# config/initializers/active_model_serializers.rb
|
45
|
+
require 'active_model_serializers/register_jsonapi_renderer'
|
31
46
|
```
|
32
47
|
|
33
48
|
### Adapter Changes
|
data/docs/jsonapi/schema.md
CHANGED
@@ -28,7 +28,7 @@ Example supported requests
|
|
28
28
|
- Relationships
|
29
29
|
- GET /articles/1/relationships/comments
|
30
30
|
- GET /articles/1/relationships/author
|
31
|
-
- Optional: [Inclusion of related resources](http://jsonapi.org/format/#fetching-includes) `
|
31
|
+
- Optional: [Inclusion of related resources](http://jsonapi.org/format/#fetching-includes) `JSONAPI::IncludeDirective`
|
32
32
|
- GET /articles/1?`include`=comments
|
33
33
|
- GET /articles/1?`include`=comments.author
|
34
34
|
- GET /articles/1?`include`=author,comments.author
|
@@ -19,12 +19,13 @@ module ActionController
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def serialization_scope
|
22
|
-
|
23
|
-
|
22
|
+
return unless _serialization_scope && respond_to?(_serialization_scope, true)
|
23
|
+
|
24
|
+
send(_serialization_scope)
|
24
25
|
end
|
25
26
|
|
26
27
|
def get_serializer(resource, options = {})
|
27
|
-
|
28
|
+
unless use_adapter?
|
28
29
|
warn 'ActionController::Serialization#use_adapter? has been removed. '\
|
29
30
|
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
30
31
|
options[:adapter] = false
|
@@ -7,9 +7,11 @@ module ActiveModel
|
|
7
7
|
deprecate :inherited, 'ActiveModelSerializers::Adapter::Base.'
|
8
8
|
end
|
9
9
|
|
10
|
+
# :nocov:
|
10
11
|
def initialize(serializer, options = {})
|
11
12
|
super(ActiveModelSerializers::Adapter::Base.new(serializer, options))
|
12
13
|
end
|
14
|
+
# :nocov:
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
@@ -1,9 +1,12 @@
|
|
1
1
|
require 'active_model/serializer/collection_serializer'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class ArraySerializer < CollectionSerializer
|
6
|
+
class << self
|
7
|
+
extend ActiveModelSerializers::Deprecate
|
8
|
+
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
9
|
+
end
|
7
10
|
end
|
8
11
|
end
|
9
12
|
end
|
@@ -10,8 +10,6 @@ module ActiveModel
|
|
10
10
|
module Associations
|
11
11
|
extend ActiveSupport::Concern
|
12
12
|
|
13
|
-
DEFAULT_INCLUDE_TREE = ActiveModel::Serializer::IncludeTree.from_string('*')
|
14
|
-
|
15
13
|
included do
|
16
14
|
with_options instance_writer: false, instance_reader: true do |serializer|
|
17
15
|
serializer.class_attribute :_reflections
|
@@ -41,7 +39,7 @@ module ActiveModel
|
|
41
39
|
# @example
|
42
40
|
# has_many :comments, serializer: CommentSummarySerializer
|
43
41
|
#
|
44
|
-
def has_many(name, options = {}, &block)
|
42
|
+
def has_many(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
45
43
|
associate(HasManyReflection.new(name, options, block))
|
46
44
|
end
|
47
45
|
|
@@ -63,7 +61,7 @@ module ActiveModel
|
|
63
61
|
# @example
|
64
62
|
# has_one :author, serializer: AuthorSerializer
|
65
63
|
#
|
66
|
-
def has_one(name, options = {}, &block)
|
64
|
+
def has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
67
65
|
associate(HasOneReflection.new(name, options, block))
|
68
66
|
end
|
69
67
|
|
@@ -80,17 +78,18 @@ module ActiveModel
|
|
80
78
|
end
|
81
79
|
end
|
82
80
|
|
83
|
-
# @param [
|
81
|
+
# @param [JSONAPI::IncludeDirective] include_directive (defaults to the
|
82
|
+
# +default_include_directive+ config value when not provided)
|
84
83
|
# @return [Enumerator<Association>]
|
85
84
|
#
|
86
|
-
def associations(
|
85
|
+
def associations(include_directive = ActiveModelSerializers.default_include_directive)
|
87
86
|
return unless object
|
88
87
|
|
89
88
|
Enumerator.new do |y|
|
90
89
|
self.class._reflections.each do |reflection|
|
91
90
|
next if reflection.excluded?(self)
|
92
91
|
key = reflection.options.fetch(:key, reflection.name)
|
93
|
-
next unless
|
92
|
+
next unless include_directive.key?(key)
|
94
93
|
y.yield reflection.build_association(self, instance_options)
|
95
94
|
end
|
96
95
|
end
|