active_model_serializers 0.10.2 → 0.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +8 -1
- data/CHANGELOG.md +45 -3
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -1
- data/README.md +5 -2
- data/active_model_serializers.gemspec +2 -2
- data/docs/ARCHITECTURE.md +6 -7
- data/docs/README.md +2 -0
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +64 -0
- data/docs/general/rendering.md +35 -1
- data/docs/general/serializers.md +35 -5
- data/docs/howto/add_pagination_links.md +2 -2
- data/docs/howto/add_relationship_links.md +137 -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 +9 -2
- data/docs/howto/passing_arbitrary_options.md +2 -2
- 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 +41 -24
- data/lib/action_controller/serialization.rb +9 -0
- data/lib/active_model/serializer.rb +19 -22
- data/lib/active_model/serializer/association.rb +19 -4
- data/lib/active_model/serializer/collection_serializer.rb +8 -5
- data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +8 -5
- data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +0 -0
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +5 -1
- data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +24 -1
- data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
- data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
- data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
- data/lib/active_model/serializer/reflection.rb +37 -21
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -0
- data/lib/active_model_serializers/adapter/attributes.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api.rb +15 -22
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
- data/lib/active_model_serializers/key_transform.rb +4 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +1 -1
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/generators/rails/serializer_generator.rb +1 -1
- data/test/action_controller/json_api/fields_test.rb +57 -0
- data/test/action_controller/json_api/transform_test.rb +3 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +226 -0
- data/test/active_model_serializers/key_transform_test.rb +32 -0
- data/test/active_model_serializers/model_test.rb +11 -0
- data/test/adapter/attributes_test.rb +43 -0
- data/test/adapter/json/transform_test.rb +1 -1
- data/test/adapter/json_api/fields_test.rb +4 -3
- data/test/adapter/json_api/has_many_test.rb +21 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +166 -0
- data/test/adapter/json_api/linked_test.rb +24 -6
- data/test/adapter/json_api/links_test.rb +1 -1
- data/test/adapter/json_api/pagination_links_test.rb +17 -2
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +20 -0
- data/test/adapter/json_api/transform_test.rb +4 -3
- 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 +1 -1
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/cache_test.rb +42 -4
- data/test/collection_serializer_test.rb +1 -1
- data/test/fixtures/poro.rb +44 -41
- data/test/generators/serializer_generator_test.rb +22 -5
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +97 -22
- data/test/serializers/attribute_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +3 -3
- data/test/serializers/serializer_for_with_namespace_test.rb +87 -0
- data/test/support/serialization_testing.rb +16 -0
- data/test/test_helper.rb +1 -0
- metadata +35 -14
- data/test/adapter/json_api/relationships_test.rb +0 -204
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c59f59688a0f1097d51eecd56caa45fc9ccec6e0
|
4
|
+
data.tar.gz: 5907cb2c75b806f2ac46b9457a33e898d10dc4a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 540883101bd1555c3697cae38658e773c726a42d8195d6f9f59f928cfe3bf5105c91f3c8fd48c88e83e4d2f288ebd03b07f74d9747ca66fc281d50f82b2983eb
|
7
|
+
data.tar.gz: 208b3eb1c115cb2c18dce647371e9748576bee1a440c4e7b9edc1e5730e145096bb722c999cd37b620fe03b306dde0da012799d092e11c6377bd1a2c14e990b2
|
data/.travis.yml
CHANGED
@@ -20,7 +20,8 @@ cache:
|
|
20
20
|
|
21
21
|
script:
|
22
22
|
- bundle exec rake ci
|
23
|
-
|
23
|
+
after_success:
|
24
|
+
- codeclimate-test-reporter
|
24
25
|
env:
|
25
26
|
global:
|
26
27
|
- "JRUBY_OPTS='--dev -J-Xmx1024M --debug'"
|
@@ -38,6 +39,12 @@ matrix:
|
|
38
39
|
env: RAILS_VERSION=master
|
39
40
|
- rvm: jruby-head
|
40
41
|
env: RAILS_VERSION=master
|
42
|
+
- rvm: 2.1
|
43
|
+
env: RAILS_VERSION=5.0
|
44
|
+
- rvm: jruby-9.0.4.0
|
45
|
+
env: RAILS_VERSION=5.0
|
46
|
+
- rvm: jruby-head
|
47
|
+
env: RAILS_VERSION=5.0
|
41
48
|
allow_failures:
|
42
49
|
- rvm: ruby-head
|
43
50
|
- rvm: jruby-head
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,6 @@
|
|
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.3...master)
|
4
4
|
|
5
5
|
Breaking changes:
|
6
6
|
|
@@ -10,10 +10,52 @@ Fixes:
|
|
10
10
|
|
11
11
|
Misc:
|
12
12
|
|
13
|
-
### [v0.10.
|
13
|
+
### [v0.10.3 (2016-11-21)](https://github.com/rails-api/active_model_serializers/compare/v0.10.2...v0.10.3)
|
14
14
|
|
15
15
|
Fixes:
|
16
|
-
|
16
|
+
|
17
|
+
- [#1973](https://github.com/rails-api/active_model_serializers/pull/1973) Fix namespace lookup for collections and has_many relationships (@groyoh)
|
18
|
+
- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
|
19
|
+
- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
|
20
|
+
- [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino)
|
21
|
+
- [#1930](https://github.com/rails-api/active_model_serializers/pull/1930) Ensure valid jsonapi when relationship has no links or data (@richmolj)
|
22
|
+
|
23
|
+
Features:
|
24
|
+
|
25
|
+
- [#1757](https://github.com/rails-api/active_model_serializers/pull/1757) Make serializer lookup chain configurable. (@NullVoxPopuli)
|
26
|
+
- [#1968](https://github.com/rails-api/active_model_serializers/pull/1968) (@NullVoxPopuli)
|
27
|
+
- Add controller namespace to default controller lookup
|
28
|
+
- Provide a `namespace` render option
|
29
|
+
- document how set the namespace in the controller for implicit lookup.
|
30
|
+
- [#1791](https://github.com/rails-api/active_model_serializers/pull/1791) (@bf4, @youroff, @NullVoxPopuli)
|
31
|
+
- Added `jsonapi_namespace_separator` config option.
|
32
|
+
- [#1889](https://github.com/rails-api/active_model_serializers/pull/1889) Support key transformation for Attributes adapter (@iancanderson, @danbee)
|
33
|
+
- [#1917](https://github.com/rails-api/active_model_serializers/pull/1917) Add `jsonapi_pagination_links_enabled` configuration option (@richmolj)
|
34
|
+
- [#1797](https://github.com/rails-api/active_model_serializers/pull/1797) Only include 'relationships' when sideloading (@richmolj)
|
35
|
+
|
36
|
+
Fixes:
|
37
|
+
|
38
|
+
- [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh)
|
39
|
+
- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene)
|
40
|
+
|
41
|
+
Misc:
|
42
|
+
- [#1767](https://github.com/rails-api/active_model_serializers/pull/1767) Replace raising/rescuing `CollectionSerializer::NoSerializerError`,
|
43
|
+
throw/catch `:no_serializer`. (@bf4)
|
44
|
+
- [#1839](https://github.com/rails-api/active_model_serializers/pull/1839) `fields` tests demonstrating usage for both attributes and relationships. (@NullVoxPopuli)
|
45
|
+
- [#1812](https://github.com/rails-api/active_model_serializers/pull/1812) add a code of conduct (@corainchicago)
|
46
|
+
|
47
|
+
- [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz)
|
48
|
+
|
49
|
+
- [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@ScottKbka)
|
50
|
+
- [#1909](https://github.com/rails-api/active_model_serializers/pull/1909) Add documentation for relationship links. (@vasilakisfil, @NullVoxPopuli)
|
51
|
+
- [#1959](https://github.com/rails-api/active_model_serializers/pull/1959) Add documentation for root. (@shunsuke227ono)
|
52
|
+
- [#1967](https://github.com/rails-api/active_model_serializers/pull/1967) Improve type method documentation. (@yukideluxe)
|
53
|
+
|
54
|
+
### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
|
55
|
+
|
56
|
+
Fixes:
|
57
|
+
- [#1814](https://github.com/rails-api/active_model_serializers/pull/1814) Ensuring read_multi works with fragment cache
|
58
|
+
- [#1848](https://github.com/rails-api/active_model_serializers/pull/1848) Redefine associations on inherited serializers. (@EhsanYousefi)
|
17
59
|
|
18
60
|
Misc:
|
19
61
|
- [#1808](https://github.com/rails-api/active_model_serializers/pull/1808) Adds documentation for `fields` option. (@luizkowalski)
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting one of the owners listed at https://rubygems.org/gems/active_model_serializers. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
CHANGED
@@ -38,13 +38,16 @@ gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
|
|
38
38
|
|
39
39
|
group :bench do
|
40
40
|
# https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76
|
41
|
-
gem 'benchmark-ips', require: false, group: :development
|
41
|
+
gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development
|
42
42
|
end
|
43
43
|
|
44
44
|
group :test do
|
45
45
|
gem 'sqlite3', platform: (@windows_platforms + [:ruby])
|
46
46
|
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
|
47
47
|
gem 'codeclimate-test-reporter', require: false
|
48
|
+
gem 'm', '~> 1.5'
|
49
|
+
gem 'pry', '~> 0.10'
|
50
|
+
gem 'pry-byebug', '~> 3.4', platform: :ruby
|
48
51
|
end
|
49
52
|
|
50
53
|
group :development, :test do
|
data/README.md
CHANGED
@@ -84,9 +84,8 @@ If you'd like to chat, we have a [community slack](http://amserializers.herokuap
|
|
84
84
|
Thanks!
|
85
85
|
|
86
86
|
## Documentation
|
87
|
-
|
88
87
|
- [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
|
89
|
-
- [](http://www.rubydoc.info/
|
88
|
+
- [](http://www.rubydoc.info/gems/active_model_serializers/0.10.2)
|
90
89
|
- [Guides](docs)
|
91
90
|
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
92
91
|
- [](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
|
@@ -159,6 +158,10 @@ serializer.associations
|
|
159
158
|
```
|
160
159
|
See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for more information.
|
161
160
|
|
161
|
+
## Semantic Versioning
|
162
|
+
|
163
|
+
This project adheres to [semver](http://semver.org/)
|
164
|
+
|
162
165
|
## Contributing
|
163
166
|
|
164
167
|
See [CONTRIBUTING.md](CONTRIBUTING.md)
|
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
# 'rack'
|
32
32
|
# 'rack-test', '~> 0.6.2'
|
33
33
|
|
34
|
-
spec.
|
34
|
+
spec.add_development_dependency 'railties', rails_versions
|
35
35
|
# 'activesupport', rails_versions
|
36
36
|
# 'actionpack', rails_versions
|
37
37
|
# 'rake', '>= 0.8.7'
|
@@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
|
|
42
42
|
# 'minitest'
|
43
43
|
# 'thread_safe'
|
44
44
|
|
45
|
-
spec.add_runtime_dependency 'jsonapi', '
|
45
|
+
spec.add_runtime_dependency 'jsonapi', '0.1.1.beta2'
|
46
46
|
|
47
47
|
spec.add_development_dependency 'activerecord', rails_versions
|
48
48
|
# arel
|
data/docs/ARCHITECTURE.md
CHANGED
@@ -15,7 +15,7 @@ It requires an adapter to transform its attributes into a JSON document; it cann
|
|
15
15
|
It may be useful to think of it as a
|
16
16
|
[presenter](http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters).
|
17
17
|
|
18
|
-
The **`ActiveModel::
|
18
|
+
The **`ActiveModel::CollectionSerializer`** represents a collection of resources as serializers
|
19
19
|
and, if there is no serializer, primitives.
|
20
20
|
|
21
21
|
The **`ActiveModel::Adapter`** describes the structure of the JSON document generated from a
|
@@ -42,10 +42,9 @@ it is not modified.
|
|
42
42
|
Internally, if no serializer can be found in the controller, the resource is not decorated by
|
43
43
|
ActiveModelSerializers.
|
44
44
|
|
45
|
-
If the collection serializer (
|
46
|
-
identify a serializer for a resource in its collection, it
|
47
|
-
|
48
|
-
the association value directly:
|
45
|
+
If the collection serializer (CollectionSerializer) cannot
|
46
|
+
identify a serializer for a resource in its collection, it throws [`:no_serializer`](https://github.com/rails-api/active_model_serializers/issues/1191#issuecomment-142327128).
|
47
|
+
For example, when caught by `Reflection#build_association`, the association value is set directly:
|
49
48
|
|
50
49
|
```ruby
|
51
50
|
reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
|
@@ -85,8 +84,8 @@ Details:
|
|
85
84
|
1. The serializer and adapter are created as
|
86
85
|
1. `serializer_instance = serializer.new(resource, serializer_opts)`
|
87
86
|
2. `adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)`
|
88
|
-
1. **ActiveModel::Serializer::
|
89
|
-
1. If the `serializer_instance` was a `
|
87
|
+
1. **ActiveModel::Serializer::CollectionSerializer#new**
|
88
|
+
1. If the `serializer_instance` was a `CollectionSerializer` and the `:serializer` serializer_opts
|
90
89
|
is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/a54d237e2828fe6bab1ea5dfe6360d4ecc8214cd/lib/active_model/serializer/array_serializer.rb#L14-L16).
|
91
90
|
1. **ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
|
92
91
|
resource as defined by the serializer.
|
data/docs/README.md
CHANGED
@@ -24,10 +24,12 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
|
|
24
24
|
|
25
25
|
- [How to add root key](howto/add_root_key.md)
|
26
26
|
- [How to add pagination links](howto/add_pagination_links.md)
|
27
|
+
- [How to add relationship links](howto/add_relationship_links.md)
|
27
28
|
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
|
28
29
|
- [Testing ActiveModelSerializers](howto/test.md)
|
29
30
|
- [Passing Arbitrary Options](howto/passing_arbitrary_options.md)
|
30
31
|
- [How to serialize a Plain-Old Ruby Object (PORO)](howto/serialize_poro.md)
|
32
|
+
- [How to upgrade from `0.8` to `0.10` safely](howto/upgrade_from_0_8_to_0_10.md)
|
31
33
|
|
32
34
|
## Integrations
|
33
35
|
|
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,6 +46,7 @@ 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
|
|
@@ -59,6 +60,56 @@ application, setting `config.key_transform` to `:unaltered` will provide a perfo
|
|
59
60
|
What relationships to serialize by default. Default: `'*'`, which includes one level of related
|
60
61
|
objects. See [includes](adapters.md#included) for more info.
|
61
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
|
+
```
|
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
|
+
|
62
113
|
## JSON API
|
63
114
|
|
64
115
|
##### jsonapi_resource_type
|
@@ -72,6 +123,19 @@ Possible values:
|
|
72
123
|
- `:singular`
|
73
124
|
- `:plural` (default)
|
74
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
|
+
|
75
139
|
##### jsonapi_include_toplevel_object
|
76
140
|
|
77
141
|
Include a [top level jsonapi member](http://jsonapi.org/format/#document-jsonapi-object)
|
data/docs/general/rendering.md
CHANGED
@@ -82,7 +82,11 @@ See [Fields](fields.md) for more information.
|
|
82
82
|
|
83
83
|
#### adapter
|
84
84
|
|
85
|
-
|
85
|
+
This option lets you explicitly set the adapter to be used by passing a registered adapter. Your options are `:attributes`, `:json`, and `:json_api`.
|
86
|
+
|
87
|
+
```
|
88
|
+
ActiveModel::Serializer.config.adapter = :json_api
|
89
|
+
```
|
86
90
|
|
87
91
|
#### key_transform
|
88
92
|
|
@@ -239,6 +243,36 @@ This will be rendered as:
|
|
239
243
|
```
|
240
244
|
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.
|
241
245
|
|
246
|
+
#### namespace
|
247
|
+
|
248
|
+
The namespace for serializer lookup is based on the controller.
|
249
|
+
|
250
|
+
To configure the implicit namespace, in your controller, create a before filter
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
before_action do
|
254
|
+
self.namespace_for_serializer = Api::V2
|
255
|
+
end
|
256
|
+
```
|
257
|
+
|
258
|
+
`namespace` can also be passed in as a render option:
|
259
|
+
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
@post = Post.first
|
263
|
+
render json: @post, namespace: Api::V2
|
264
|
+
```
|
265
|
+
|
266
|
+
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.
|
267
|
+
|
268
|
+
The `namespace` can be any object whose namespace can be represented by string interpolation (i.e. by calling to_s)
|
269
|
+
- Module `Api::V2`
|
270
|
+
- String `'Api::V2'`
|
271
|
+
- Symbol `:'Api::V2'`
|
272
|
+
|
273
|
+
Note that by using a string and symbol, Ruby will assume the namespace is defined at the top level.
|
274
|
+
|
275
|
+
|
242
276
|
#### serializer
|
243
277
|
|
244
278
|
PR please :)
|
data/docs/general/serializers.md
CHANGED
@@ -140,7 +140,7 @@ class PictureSerializer < ActiveModel::Serializer
|
|
140
140
|
end
|
141
141
|
```
|
142
142
|
|
143
|
-
For more context, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter.
|
143
|
+
You can specify the serializers by [overriding serializer_for](serializers.md#overriding-association-serializer-lookup). For more context about polymorphic relationships, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter.
|
144
144
|
|
145
145
|
### Caching
|
146
146
|
|
@@ -173,18 +173,25 @@ end
|
|
173
173
|
|
174
174
|
#### ::type
|
175
175
|
|
176
|
-
|
176
|
+
When using the `:json_api` adapter, the `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
|
177
|
+
|
178
|
+
When using the `:json` adapter, the `::type` method defines the name of the root element.
|
179
|
+
|
177
180
|
It either takes a `String` or `Symbol` as parameter.
|
178
181
|
|
179
|
-
Note: This method is useful only when using the `:json_api` adapter.
|
182
|
+
Note: This method is useful only when using the `:json_api` or `:json` adapter.
|
180
183
|
|
181
184
|
Examples:
|
182
185
|
```ruby
|
183
186
|
class UserProfileSerializer < ActiveModel::Serializer
|
184
187
|
type 'profile'
|
188
|
+
|
189
|
+
attribute :name
|
185
190
|
end
|
186
191
|
class AuthorProfileSerializer < ActiveModel::Serializer
|
187
192
|
type :profile
|
193
|
+
|
194
|
+
attribute :name
|
188
195
|
end
|
189
196
|
```
|
190
197
|
|
@@ -194,7 +201,20 @@ With the `:json_api` adapter, the previous serializers would be rendered as:
|
|
194
201
|
{
|
195
202
|
"data": {
|
196
203
|
"id": "1",
|
197
|
-
"type": "profile"
|
204
|
+
"type": "profile",
|
205
|
+
"attributes": {
|
206
|
+
"name": "Julia"
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
```
|
211
|
+
|
212
|
+
With the `:json` adapter, the previous serializer would be rendered as:
|
213
|
+
|
214
|
+
``` json
|
215
|
+
{
|
216
|
+
"profile": {
|
217
|
+
"name": "Julia"
|
198
218
|
}
|
199
219
|
}
|
200
220
|
```
|
@@ -217,7 +237,17 @@ The object being serialized.
|
|
217
237
|
|
218
238
|
#### #root
|
219
239
|
|
220
|
-
|
240
|
+
Resource root which is included in `JSON` adapter. As you can see at [Adapters Document](adapters.md), `Attribute` adapter (default) and `JSON API` adapter does not include root at top level.
|
241
|
+
By default, the resource root comes from the `model_name` of the serialized object's class.
|
242
|
+
|
243
|
+
There are several ways to specify root:
|
244
|
+
* [Overriding the root key](rendering.md#overriding-the-root-key)
|
245
|
+
* [Setting `type`](serializers.md#type)
|
246
|
+
* Specifying the `root` option, e.g. `root: 'specific_name'`, during the serializer's initialization:
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
ActiveModelSerializers::SerializableResource.new(foo, root: 'bar')
|
250
|
+
```
|
221
251
|
|
222
252
|
#### #scope
|
223
253
|
|