active_model_serializers 0.10.9 → 0.10.13
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/CHANGELOG.md +41 -2
- data/README.md +18 -19
- data/lib/action_controller/serialization.rb +9 -1
- data/lib/active_model/serializer/collection_serializer.rb +11 -2
- data/lib/active_model/serializer/concerns/caching.rb +2 -1
- data/lib/active_model/serializer/fieldset.rb +1 -1
- data/lib/active_model/serializer/reflection.rb +4 -4
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +13 -4
- data/lib/active_model_serializers/adapter/attributes.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +5 -1
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
- data/lib/active_model_serializers/model/caching.rb +26 -0
- metadata +20 -292
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -109
- data/.simplecov +0 -110
- data/.travis.yml +0 -63
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -74
- data/Rakefile +0 -76
- data/active_model_serializers.gemspec +0 -64
- data/appveyor.yml +0 -28
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/rubocop +0 -38
- data/bin/serve_benchmark +0 -39
- data/docs/README.md +0 -41
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -269
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -185
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -21
- data/docs/general/rendering.md +0 -293
- data/docs/general/serializers.md +0 -495
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -140
- data/docs/howto/add_root_key.md +0 -62
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -66
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -73
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -147
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/jsonapi/schema.md +0 -151
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/test/action_controller/adapter_selector_test.rb +0 -64
- data/test/action_controller/explicit_serializer_test.rb +0 -137
- data/test/action_controller/json/include_test.rb +0 -248
- data/test/action_controller/json_api/deserialization_test.rb +0 -114
- data/test/action_controller/json_api/errors_test.rb +0 -42
- data/test/action_controller/json_api/fields_test.rb +0 -68
- data/test/action_controller/json_api/linked_test.rb +0 -204
- data/test/action_controller/json_api/pagination_test.rb +0 -126
- data/test/action_controller/json_api/transform_test.rb +0 -191
- data/test/action_controller/lookup_proc_test.rb +0 -51
- data/test/action_controller/namespace_lookup_test.rb +0 -234
- data/test/action_controller/serialization_scope_name_test.rb +0 -237
- data/test/action_controller/serialization_test.rb +0 -480
- data/test/active_model_serializers/adapter_for_test.rb +0 -210
- data/test/active_model_serializers/json_pointer_test.rb +0 -24
- data/test/active_model_serializers/logging_test.rb +0 -79
- data/test/active_model_serializers/model_test.rb +0 -144
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
- data/test/active_model_serializers/test/schema_test.rb +0 -133
- data/test/active_model_serializers/test/serializer_test.rb +0 -64
- data/test/active_record_test.rb +0 -11
- data/test/adapter/attributes_test.rb +0 -42
- data/test/adapter/deprecation_test.rb +0 -102
- data/test/adapter/json/belongs_to_test.rb +0 -47
- data/test/adapter/json/collection_test.rb +0 -106
- data/test/adapter/json/has_many_test.rb +0 -55
- data/test/adapter/json/transform_test.rb +0 -95
- data/test/adapter/json_api/belongs_to_test.rb +0 -157
- data/test/adapter/json_api/collection_test.rb +0 -98
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -98
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
- data/test/adapter/json_api/has_many_test.rb +0 -175
- data/test/adapter/json_api/has_one_test.rb +0 -82
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -415
- data/test/adapter/json_api/links_test.rb +0 -112
- data/test/adapter/json_api/pagination_links_test.rb +0 -208
- data/test/adapter/json_api/parse_test.rb +0 -139
- data/test/adapter/json_api/relationship_test.rb +0 -399
- data/test/adapter/json_api/resource_meta_test.rb +0 -102
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
- data/test/adapter/json_api/transform_test.rb +0 -514
- data/test/adapter/json_api/type_test.rb +0 -195
- data/test/adapter/json_test.rb +0 -48
- data/test/adapter/null_test.rb +0 -24
- data/test/adapter/polymorphic_test.rb +0 -220
- data/test/adapter_test.rb +0 -69
- data/test/array_serializer_test.rb +0 -24
- data/test/benchmark/app.rb +0 -67
- data/test/benchmark/benchmarking_support.rb +0 -69
- data/test/benchmark/bm_active_record.rb +0 -83
- data/test/benchmark/bm_adapter.rb +0 -40
- data/test/benchmark/bm_caching.rb +0 -121
- data/test/benchmark/bm_lookup_chain.rb +0 -85
- data/test/benchmark/bm_transform.rb +0 -47
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -85
- data/test/benchmark/fixtures.rb +0 -221
- data/test/cache_test.rb +0 -717
- data/test/collection_serializer_test.rb +0 -129
- data/test/fixtures/active_record.rb +0 -115
- data/test/fixtures/poro.rb +0 -227
- data/test/generators/scaffold_controller_generator_test.rb +0 -26
- data/test/generators/serializer_generator_test.rb +0 -77
- data/test/grape_test.rb +0 -198
- data/test/lint_test.rb +0 -51
- data/test/logger_test.rb +0 -22
- data/test/poro_test.rb +0 -11
- data/test/serializable_resource_test.rb +0 -81
- data/test/serializers/association_macros_test.rb +0 -39
- data/test/serializers/associations_test.rb +0 -520
- data/test/serializers/attribute_test.rb +0 -155
- data/test/serializers/attributes_test.rb +0 -54
- data/test/serializers/caching_configuration_test_isolated.rb +0 -172
- data/test/serializers/configuration_test.rb +0 -34
- data/test/serializers/fieldset_test.rb +0 -16
- data/test/serializers/meta_test.rb +0 -204
- data/test/serializers/options_test.rb +0 -34
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
- data/test/serializers/reflection_test.rb +0 -481
- data/test/serializers/root_test.rb +0 -23
- data/test/serializers/serialization_test.rb +0 -57
- data/test/serializers/serializer_for_test.rb +0 -138
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -86
- data/test/support/rails5_shims.rb +0 -55
- data/test/support/rails_app.rb +0 -40
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -81
- data/test/test_helper.rb +0 -72
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 94ecdeeabcad7f33e6b2891f2354edeb1fdabf444d9938014d839b554f0a5ab7
|
|
4
|
+
data.tar.gz: cd43b9ceddf4920feb4efae6c5d0748bbb6e37111328c015cdcca06ba8f9d86d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 93b28755ff8f015277534dfc971cab0ef5bf30745bebb34c90e2f6e17c1cb611ef1100ba9e484ebdd1dc415d2c9d1c7e715984a1052e75daada8f712b1126850
|
|
7
|
+
data.tar.gz: 86d257304d0e58b87e82a8c32f55a7722ffdc568dd394ac93214dfe382bf8ff27c8bed24c11f6a948184a16a67ad5fa7bbaf98f2443b2d11c017ce5463c58947
|
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.13...0-10-stable)
|
|
4
4
|
|
|
5
5
|
Breaking changes:
|
|
6
6
|
|
|
@@ -10,8 +10,47 @@ Fixes:
|
|
|
10
10
|
|
|
11
11
|
Misc:
|
|
12
12
|
|
|
13
|
-
### [v0.10.
|
|
13
|
+
### [v0.10.13 (2022-01013)](https://github.com/rails-api/active_model_serializers/compare/v0.10.12...v0.10.13)
|
|
14
|
+
|
|
15
|
+
Fixes:
|
|
16
|
+
|
|
17
|
+
- [#2399](https://github.com/rails-api/active_model_serializers/pull/2399) Handles edge case where requested current_page > total_pages (@f3z0)
|
|
18
|
+
|
|
19
|
+
### [v0.10.12 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.10.11...v0.10.12)
|
|
20
|
+
|
|
21
|
+
Fixes:
|
|
22
|
+
|
|
23
|
+
- [#2398](https://github.com/rails-api/active_model_serializers/pull/2398) Update rails dependency to < 6.2 (@ritikesh)
|
|
24
|
+
|
|
25
|
+
### [v0.10.11 (2020-12-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.10...v0.10.11)
|
|
26
|
+
|
|
27
|
+
Features:
|
|
28
|
+
|
|
29
|
+
- [#2361](https://github.com/rails-api/active_model_serializers/pull/2361) Add `ActiveModelSerializers.config.use_sha1_digests` to allow customization of the hashing algorithm used for serializer caching (@alexzherdev)
|
|
14
30
|
|
|
31
|
+
Fixes:
|
|
32
|
+
|
|
33
|
+
- [#2344](https://github.com/rails-api/active_model_serializers/pull/2344) Fixes #2341 introduced since #2223 (@wasifhossain)
|
|
34
|
+
- [#2395](https://github.com/rails-api/active_model_serializers/pull/2395) remove explicit require for thread_safe (@ritikesh)
|
|
35
|
+
|
|
36
|
+
### [v0.10.10 (2019-07-13)](https://github.com/rails-api/active_model_serializers/compare/v0.10.9...v0.10.10)
|
|
37
|
+
|
|
38
|
+
Fixes:
|
|
39
|
+
|
|
40
|
+
- [#2319](https://github.com/rails-api/active_model_serializers/pull/2319) Fixes #2316. (@kylekeesling)
|
|
41
|
+
- Fix Rails 6.0 deprication warnings
|
|
42
|
+
- update test fixture schema to use `timestamps` instead of `timestamp`
|
|
43
|
+
- [#2223](https://github.com/rails-api/active_model_serializers/pull/2223) Support Fieldset in Attributes/JSON adapters documented in [docs/general/fields.md](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/fields.md) that worked partially before (@bf4)
|
|
44
|
+
- [#2337](https://github.com/rails-api/active_model_serializers/pull/2337) fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank (@InteNs)
|
|
45
|
+
- Fixes incorrect json-api generation when `jsonapi_use_foreign_key_on_belongs_to_relationship` is `true` and the relationship is blank
|
|
46
|
+
- [#2172](https://github.com/rails-api/active_model_serializers/pull/2172) Preserve the namespace when falling back to a superclass serializer
|
|
47
|
+
|
|
48
|
+
Misc:
|
|
49
|
+
|
|
50
|
+
- [#2327](https://github.com/rails-api/active_model_serializers/pull/2327) Add support for Ruby 2.6 on Travis CI (@wasifhossain)
|
|
51
|
+
- [#2304](https://github.com/rails-api/active_model_serializers/pull/2304) Slim down bundled gem by excluding test files and docs (@greysteil)
|
|
52
|
+
|
|
53
|
+
### [v0.10.9 (2019-02-08)](https://github.com/rails-api/active_model_serializers/compare/v0.10.8...v0.10.9)
|
|
15
54
|
|
|
16
55
|
Fixes:
|
|
17
56
|
|
data/README.md
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
<tr>
|
|
5
5
|
<td>Build Status</td>
|
|
6
6
|
<td>
|
|
7
|
-
<a href="https://travis-ci.org/rails-api/active_model_serializers"><img src="https://travis-ci.org/rails-api/active_model_serializers.svg?branch=
|
|
8
|
-
<a href="https://ci.appveyor.com/project/
|
|
7
|
+
<a href="https://travis-ci.org/rails-api/active_model_serializers"><img src="https://api.travis-ci.org/rails-api/active_model_serializers.svg?branch=0-10-stable" alt="Build Status" ></a>
|
|
8
|
+
<a href="https://ci.appveyor.com/project/bf4/active-model-serializers/branch/0-10-stable"><img src="https://ci.appveyor.com/api/projects/status/x6xdjydutm54gvyt/branch/master?svg=true" alt="Build status"></a>
|
|
9
9
|
</td>
|
|
10
10
|
</tr>
|
|
11
11
|
<tr>
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
<td>
|
|
14
14
|
<a href="https://codeclimate.com/github/rails-api/active_model_serializers"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/gpa.svg" alt="Code Quality"></a>
|
|
15
15
|
<a href="https://codebeat.co/projects/github-com-rails-api-active_model_serializers"><img src="https://codebeat.co/badges/a9ab35fa-8b5a-4680-9d4e-a81f9a55ebcd" alt="codebeat" ></a>
|
|
16
|
-
<a href="https://codeclimate.com/github/rails-api/active_model_serializers/coverage"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/coverage.svg" alt="Test Coverage"></a>
|
|
17
16
|
</td>
|
|
18
17
|
</tr>
|
|
19
18
|
<tr>
|
|
@@ -41,7 +40,7 @@ these methods to the adapter.)
|
|
|
41
40
|
|
|
42
41
|
By default ActiveModelSerializers will use the **Attributes Adapter** (no JSON root).
|
|
43
42
|
But we strongly advise you to use **JsonApi Adapter**, which
|
|
44
|
-
follows 1.0 of the format specified in [jsonapi.org/format](
|
|
43
|
+
follows 1.0 of the format specified in [jsonapi.org/format](https://jsonapi.org/format).
|
|
45
44
|
Check how to change the adapter in the sections below.
|
|
46
45
|
|
|
47
46
|
`0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
|
|
@@ -75,9 +74,9 @@ More information is available in the [Guides](docs) and
|
|
|
75
74
|
If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new)
|
|
76
75
|
and see our [contributing guide](CONTRIBUTING.md).
|
|
77
76
|
|
|
78
|
-
If you have a question, please [post to Stack Overflow](
|
|
77
|
+
If you have a question, please [post to Stack Overflow](https://stackoverflow.com/questions/tagged/active-model-serializers).
|
|
79
78
|
|
|
80
|
-
If you'd like to chat, we have a [community slack](
|
|
79
|
+
If you'd like to chat, we have a [community slack](https://amserializers.herokuapp.com).
|
|
81
80
|
|
|
82
81
|
Thanks!
|
|
83
82
|
|
|
@@ -87,14 +86,14 @@ If you're reading this at https://github.com/rails-api/active_model_serializers
|
|
|
87
86
|
reading documentation for our `master`, which may include features that have not
|
|
88
87
|
been released yet. Please see below for the documentation relevant to you.
|
|
89
88
|
|
|
90
|
-
- [0.10 (
|
|
91
|
-
- [0.10.
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
- [0.10 (0-10-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-10-stable)
|
|
90
|
+
- [0.10.10 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.10)
|
|
91
|
+
- [](https://www.rubydoc.info/gems/active_model_serializers/0.10.10)
|
|
92
|
+
- [Guides](docs)
|
|
94
93
|
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
|
95
|
-
|
|
94
|
+
- [](https://www.rubydoc.info/gems/active_model_serializers/0.9.7)
|
|
96
95
|
- [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
|
97
|
-
|
|
96
|
+
- [](https://www.rubydoc.info/gems/active_model_serializers/0.8.4)
|
|
98
97
|
|
|
99
98
|
|
|
100
99
|
## High-level behavior
|
|
@@ -171,12 +170,12 @@ The original design is also available [here](https://github.com/rails-api/active
|
|
|
171
170
|
|
|
172
171
|
### ActiveModel::Serializer
|
|
173
172
|
|
|
174
|
-
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/
|
|
173
|
+
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/serialization.rb)
|
|
175
174
|
and exposes an `attributes` method, among a few others.
|
|
176
175
|
It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
|
|
177
176
|
It requires an adapter to transform its attributes into a JSON document; it cannot be serialized itself.
|
|
178
177
|
It may be useful to think of it as a
|
|
179
|
-
[presenter](
|
|
178
|
+
[presenter](https://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters).
|
|
180
179
|
|
|
181
180
|
#### ActiveModel::CollectionSerializer
|
|
182
181
|
|
|
@@ -188,7 +187,7 @@ and, if there is no serializer, primitives.
|
|
|
188
187
|
The **`ActiveModelSerializers::Adapter::Base`** describes the structure of the JSON document generated from a
|
|
189
188
|
serializer. For example, the `Attributes` example represents each serializer as its
|
|
190
189
|
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
|
|
191
|
-
API](
|
|
190
|
+
API](https://jsonapi.org/) document.
|
|
192
191
|
|
|
193
192
|
### ActiveModelSerializers::SerializableResource
|
|
194
193
|
|
|
@@ -229,7 +228,7 @@ High-level overview:
|
|
|
229
228
|
- `:each_serializer` specifies the serializer for each resource in the collection.
|
|
230
229
|
- For a **single resource**, the `:serializer` option is the resource serializer.
|
|
231
230
|
- Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
|
|
232
|
-
[`ADAPTER_OPTION_KEYS`](
|
|
231
|
+
[`ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
|
|
233
232
|
The remaining options are serializer options.
|
|
234
233
|
|
|
235
234
|
Details:
|
|
@@ -254,7 +253,7 @@ Details:
|
|
|
254
253
|
2. `adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)`
|
|
255
254
|
1. **ActiveModel::Serializer::CollectionSerializer#new**
|
|
256
255
|
1. If the `serializer_instance` was a `CollectionSerializer` and the `:serializer` serializer_opts
|
|
257
|
-
is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/
|
|
256
|
+
is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/lib/active_model/serializer/collection_serializer.rb#L77-L79).
|
|
258
257
|
1. **ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
|
|
259
258
|
resource as defined by the serializer.
|
|
260
259
|
|
|
@@ -270,7 +269,7 @@ to know about, but not part of ActiveModelSerializers.)
|
|
|
270
269
|
[(code)](lib/active_model/serializer/lint.rb).
|
|
271
270
|
|
|
272
271
|
ActiveModelSerializers provides a
|
|
273
|
-
[`ActiveModelSerializers::Model`](
|
|
272
|
+
[`ActiveModelSerializers::Model`](lib/active_model_serializers/model.rb),
|
|
274
273
|
which is a simple serializable PORO (Plain-Old Ruby Object).
|
|
275
274
|
|
|
276
275
|
`ActiveModelSerializers::Model` may be used either as a reference implementation, or in production code.
|
|
@@ -298,7 +297,7 @@ ActiveModelSerializers::SerializableResource.new(MyModel.new(level: 'awesome'),
|
|
|
298
297
|
|
|
299
298
|
## Semantic Versioning
|
|
300
299
|
|
|
301
|
-
This project adheres to [semver](
|
|
300
|
+
This project adheres to [semver](https://semver.org/)
|
|
302
301
|
|
|
303
302
|
## Contributing
|
|
304
303
|
|
|
@@ -23,7 +23,15 @@ module ActionController
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def namespace_for_serializer
|
|
26
|
-
@namespace_for_serializer ||= self.class
|
|
26
|
+
@namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def namespace_for_class(klass)
|
|
30
|
+
if Module.method_defined?(:module_parent)
|
|
31
|
+
klass.module_parent
|
|
32
|
+
else
|
|
33
|
+
klass.parent
|
|
34
|
+
end
|
|
27
35
|
end
|
|
28
36
|
|
|
29
37
|
def serialization_scope
|
|
@@ -48,8 +48,11 @@ module ActiveModel
|
|
|
48
48
|
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
|
|
49
49
|
# 4. key may be nil for empty collection and no serializer option
|
|
50
50
|
key &&= key.pluralize
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
if raise_cannot_infer_root_key_error?
|
|
52
|
+
# 5. fail if the key cannot be determined
|
|
53
|
+
key || fail(CannotInferRootKeyError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
|
|
54
|
+
end
|
|
55
|
+
key
|
|
53
56
|
end
|
|
54
57
|
# rubocop:enable Metrics/CyclomaticComplexity
|
|
55
58
|
|
|
@@ -60,12 +63,18 @@ module ActiveModel
|
|
|
60
63
|
object.respond_to?(:size)
|
|
61
64
|
end
|
|
62
65
|
|
|
66
|
+
class CannotInferRootKeyError < StandardError; end
|
|
67
|
+
|
|
63
68
|
protected
|
|
64
69
|
|
|
65
70
|
attr_reader :serializers, :options
|
|
66
71
|
|
|
67
72
|
private
|
|
68
73
|
|
|
74
|
+
def raise_cannot_infer_root_key_error?
|
|
75
|
+
ActiveModelSerializers.config.raise_cannot_infer_root_key_error
|
|
76
|
+
end
|
|
77
|
+
|
|
69
78
|
def serializers_from_resources
|
|
70
79
|
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
|
71
80
|
object.map do |resource|
|
|
@@ -56,7 +56,8 @@ module ActiveModel
|
|
|
56
56
|
def digest_caller_file(caller_line)
|
|
57
57
|
serializer_file_path = caller_line[CALLER_FILE]
|
|
58
58
|
serializer_file_contents = IO.read(serializer_file_path)
|
|
59
|
-
Digest::MD5
|
|
59
|
+
algorithm = ActiveModelSerializers.config.use_sha1_digests ? Digest::SHA1 : Digest::MD5
|
|
60
|
+
algorithm.hexdigest(serializer_file_contents)
|
|
60
61
|
rescue TypeError, Errno::ENOENT
|
|
61
62
|
warn <<-EOF.strip_heredoc
|
|
62
63
|
Cannot digest non-existent file: '#{caller_line}'.
|
|
@@ -87,8 +87,8 @@ module ActiveModel
|
|
|
87
87
|
# meta ids: ids
|
|
88
88
|
# end
|
|
89
89
|
# end
|
|
90
|
-
def link(name, value = nil)
|
|
91
|
-
options[:links][name] = block_given? ?
|
|
90
|
+
def link(name, value = nil, &block)
|
|
91
|
+
options[:links][name] = block_given? ? block : value
|
|
92
92
|
:nil
|
|
93
93
|
end
|
|
94
94
|
|
|
@@ -102,8 +102,8 @@ module ActiveModel
|
|
|
102
102
|
# href object.blog.id.to_s
|
|
103
103
|
# meta(id: object.blog.id)
|
|
104
104
|
# end
|
|
105
|
-
def meta(value = nil)
|
|
106
|
-
options[:meta] = block_given? ?
|
|
105
|
+
def meta(value = nil, &block)
|
|
106
|
+
options[:meta] = block_given? ? block : value
|
|
107
107
|
:nil
|
|
108
108
|
end
|
|
109
109
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'thread_safe'
|
|
4
3
|
require 'jsonapi/include_directive'
|
|
5
4
|
require 'active_model/serializer/collection_serializer'
|
|
6
5
|
require 'active_model/serializer/array_serializer'
|
|
@@ -73,7 +72,7 @@ module ActiveModel
|
|
|
73
72
|
# Used to cache serializer name => serializer class
|
|
74
73
|
# when looked up by Serializer.get_serializer_for.
|
|
75
74
|
def self.serializers_cache
|
|
76
|
-
@serializers_cache ||=
|
|
75
|
+
@serializers_cache ||= Concurrent::Map.new
|
|
77
76
|
end
|
|
78
77
|
|
|
79
78
|
# @api private
|
|
@@ -94,7 +93,7 @@ module ActiveModel
|
|
|
94
93
|
if serializer_class
|
|
95
94
|
serializer_class
|
|
96
95
|
elsif klass.superclass
|
|
97
|
-
get_serializer_for(klass.superclass)
|
|
96
|
+
get_serializer_for(klass.superclass, namespace)
|
|
98
97
|
else
|
|
99
98
|
nil # No serializer found
|
|
100
99
|
end
|
|
@@ -147,6 +146,8 @@ module ActiveModel
|
|
|
147
146
|
config.jsonapi_include_toplevel_object = false
|
|
148
147
|
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
|
|
149
148
|
config.include_data_default = true
|
|
149
|
+
# Raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError when cannot infer root key from collection type
|
|
150
|
+
config.raise_cannot_infer_root_key_error = true
|
|
150
151
|
|
|
151
152
|
# For configuring how serializers are found.
|
|
152
153
|
# This should be an array of procs.
|
|
@@ -365,6 +366,9 @@ module ActiveModel
|
|
|
365
366
|
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
|
|
366
367
|
adapter_options ||= {}
|
|
367
368
|
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
|
|
369
|
+
if (fieldset = adapter_options[:fieldset])
|
|
370
|
+
options[:fields] = fieldset.fields_for(json_key)
|
|
371
|
+
end
|
|
368
372
|
resource = attributes_hash(adapter_options, options, adapter_instance)
|
|
369
373
|
relationships = associations_hash(adapter_options, options, adapter_instance)
|
|
370
374
|
resource.merge(relationships)
|
|
@@ -379,7 +383,12 @@ module ActiveModel
|
|
|
379
383
|
|
|
380
384
|
# Used by adapter as resource root.
|
|
381
385
|
def json_key
|
|
382
|
-
root || _type ||
|
|
386
|
+
root || _type ||
|
|
387
|
+
begin
|
|
388
|
+
object.class.model_name.to_s.underscore
|
|
389
|
+
rescue ArgumentError
|
|
390
|
+
'anonymous_object'
|
|
391
|
+
end
|
|
383
392
|
end
|
|
384
393
|
|
|
385
394
|
def read_attribute_for_serialization(attr)
|
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
module ActiveModelSerializers
|
|
4
4
|
module Adapter
|
|
5
5
|
class Attributes < Base
|
|
6
|
+
def initialize(*)
|
|
7
|
+
super
|
|
8
|
+
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
|
|
9
|
+
end
|
|
10
|
+
|
|
6
11
|
def serializable_hash(options = nil)
|
|
7
12
|
options = serialization_options(options)
|
|
8
13
|
options[:fields] ||= instance_options[:fields]
|
|
@@ -10,6 +15,22 @@ module ActiveModelSerializers
|
|
|
10
15
|
|
|
11
16
|
self.class.transform_key_casing!(serialized_hash, instance_options)
|
|
12
17
|
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def fields_to_fieldset(fields)
|
|
22
|
+
return fields if fields.nil?
|
|
23
|
+
resource_fields = []
|
|
24
|
+
relationship_fields = {}
|
|
25
|
+
fields.each do |field|
|
|
26
|
+
case field
|
|
27
|
+
when Symbol, String then resource_fields << field
|
|
28
|
+
when Hash then relationship_fields.merge!(field)
|
|
29
|
+
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
relationship_fields.merge!(serializer.json_key.to_sym => resource_fields)
|
|
33
|
+
end
|
|
13
34
|
end
|
|
14
35
|
end
|
|
15
36
|
end
|
|
@@ -55,11 +55,15 @@ module ActiveModelSerializers
|
|
|
55
55
|
|
|
56
56
|
def prev_page_url
|
|
57
57
|
return nil if collection.current_page == FIRST_PAGE
|
|
58
|
+
if collection.current_page > collection.total_pages
|
|
59
|
+
return url_for_page(collection.total_pages)
|
|
60
|
+
end
|
|
58
61
|
url_for_page(collection.current_page - FIRST_PAGE)
|
|
59
62
|
end
|
|
60
63
|
|
|
61
64
|
def next_page_url
|
|
62
|
-
return nil if collection.total_pages == 0 ||
|
|
65
|
+
return nil if collection.total_pages == 0 ||
|
|
66
|
+
collection.current_page >= collection.total_pages
|
|
63
67
|
url_for_page(collection.next_page)
|
|
64
68
|
end
|
|
65
69
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module ActiveModelSerializers
|
|
3
|
+
class Model
|
|
4
|
+
module Caching
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
attr_writer :updated_at
|
|
9
|
+
attributes :id
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Defaults to the downcased model name and updated_at
|
|
13
|
+
def cache_key
|
|
14
|
+
ActiveSupport::Cache.expand_cache_key([
|
|
15
|
+
self.class.model_name.name.downcase,
|
|
16
|
+
"#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
|
|
17
|
+
].compact)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Defaults to the time the serializer file was modified.
|
|
21
|
+
def updated_at
|
|
22
|
+
defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|