active_model_serializers 0.9.0 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +113 -0
- data/README.md +112 -19
- data/lib/action_controller/serialization.rb +35 -8
- data/lib/action_controller/serialization_test_case.rb +4 -4
- data/lib/active_model/array_serializer.rb +13 -10
- data/lib/active_model/default_serializer.rb +2 -6
- data/lib/active_model/serializable.rb +30 -11
- data/lib/active_model/serializable/utils.rb +16 -0
- data/lib/active_model/serializer.rb +90 -40
- data/lib/active_model/serializer/{associations.rb → association.rb} +8 -52
- data/lib/active_model/serializer/association/has_many.rb +39 -0
- data/lib/active_model/serializer/association/has_one.rb +25 -0
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +1 -1
- data/lib/active_model/serializer/railtie.rb +12 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -1
- data/lib/active_model_serializers/model/caching.rb +25 -0
- data/test/benchmark/app.rb +60 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_active_record.rb +41 -0
- data/test/benchmark/setup.rb +75 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
- data/test/fixtures/active_record.rb +4 -0
- data/test/fixtures/poro.rb +149 -1
- data/test/fixtures/template.html.erb +1 -0
- data/test/integration/action_controller/namespaced_serialization_test.rb +105 -0
- data/test/integration/action_controller/serialization_test.rb +5 -5
- data/test/integration/action_controller/serialization_test_case_test.rb +10 -0
- data/test/integration/active_record/active_record_test.rb +19 -2
- data/test/test_app.rb +3 -0
- data/test/tmp/app/assets/javascripts/accounts.js +2 -0
- data/test/tmp/app/assets/stylesheets/accounts.css +4 -0
- data/test/tmp/app/controllers/accounts_controller.rb +2 -0
- data/test/tmp/app/helpers/accounts_helper.rb +2 -0
- data/test/tmp/app/serializers/account_serializer.rb +3 -0
- data/test/tmp/config/routes.rb +1 -0
- data/test/unit/active_model/array_serializer/options_test.rb +16 -0
- data/test/unit/active_model/array_serializer/serialization_test.rb +18 -1
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +15 -0
- data/test/unit/active_model/serializer/associations_test.rb +30 -0
- data/test/unit/active_model/serializer/attributes_test.rb +16 -0
- data/test/unit/active_model/serializer/config_test.rb +3 -0
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +189 -0
- data/test/unit/active_model/serializer/has_many_test.rb +52 -17
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +27 -0
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +196 -0
- data/test/unit/active_model/serializer/has_one_test.rb +46 -0
- data/test/unit/active_model/serializer/options_test.rb +27 -0
- data/test/unit/active_model/serializer/url_helpers_test.rb +35 -0
- data/test/unit/active_model/serilizable_test.rb +50 -0
- metadata +98 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e9f526564b9458ceea3d9391ddbf81cf996ff713dbb217bcec1c22931b4cd028
|
4
|
+
data.tar.gz: ac5641428e5b17ffa605bc7337f8f77000354a2a47f1a5ed7244ab50cdca84b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af47a8298fbc563716f0c7b4a9e0d92b85243ead1b0ccac561703d17f09574a3e65f280adedbb24ee7d65c0634c4ca343c58aa665628ff75d7e220b89025e91d
|
7
|
+
data.tar.gz: 6974a26799a967fbb8b172092be014deb775909a52cf10cf4e7dc88b5c05c96f6b6acbb3d245f9ad9032c743c2048c1afd8335d3a899c3111f2d6b48015800f7
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,110 @@
|
|
1
|
+
## 0.09.x
|
2
|
+
|
3
|
+
### [0-9-stable](https://github.com/rails-api/active_model_serializers/compare/v0.9.8...0-9-stable)
|
4
|
+
|
5
|
+
### [v0.9.8 (2020-12-10)](https://github.com/rails-api/active_model_serializers/compare/v0.9.7...v0.9.8)
|
6
|
+
|
7
|
+
- [#2373](https://github.com/rails-api/active_model_serializers/pull/2373) Fix Rails 6.0 deprecation warnings. (@supremebeing7)
|
8
|
+
|
9
|
+
### [v0.9.7 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.9.6...v0.9.7)
|
10
|
+
|
11
|
+
- [#2080](https://github.com/rails-api/active_model_serializers/pull/2080) remove `{ payload: nil }` from `!serialize.active_model_serializers` ActiveSupport::Notification. `payload` never had a value. Changes, for example `{ serializer: 'ActiveModel::DefaultSerializer', payload: nil }` to be `{ serializer: 'ActiveModel::DefaultSerializer' }` (@yosiat)
|
12
|
+
|
13
|
+
### [v0.9.6 (2017-01-10)](https://github.com/rails-api/active_model_serializers/compare/v0.9.5...v0.9.6)
|
14
|
+
|
15
|
+
- [#2008](https://github.com/rails-api/active_model_serializers/pull/2008) Fix warning on Thor. (@kirs)
|
16
|
+
|
17
|
+
### [v0.9.5 (2016-03-30)](https://github.com/rails-api/active_model_serializers/compare/v0.9.4...v0.9.5)
|
18
|
+
|
19
|
+
- [#1607](https://github.com/rails-api/active_model_serializers/pull/1607) Merge multiple nested associations. (@Hirtenknogger)
|
20
|
+
|
21
|
+
### [v0.9.4 (2016-01-05)](https://github.com/rails-api/active_model_serializers/compare/v0.9.3...v0.9.4)
|
22
|
+
|
23
|
+
- [#752](https://github.com/rails-api/active_model_serializers/pull/752) Tiny improvement of README 0-9-stable (@basiam)
|
24
|
+
- [#749](https://github.com/rails-api/active_model_serializers/pull/749) remove trailing whitespace (@shwoodard)
|
25
|
+
- [#717](https://github.com/rails-api/active_model_serializers/pull/717) fixed issue with rendering Hash which appears in rails 4.2.0.beta4 (@kurko, @greshny)
|
26
|
+
- [#790](https://github.com/rails-api/active_model_serializers/pull/790) pass context to ArraySerializer (@lanej)
|
27
|
+
- [#797](https://github.com/rails-api/active_model_serializers/pull/797) Fix and test for #490 (@afn)
|
28
|
+
- [#813](https://github.com/rails-api/active_model_serializers/pull/813) Allow to define custom serializer for given class (@jtomaszewski)
|
29
|
+
- [#841](https://github.com/rails-api/active_model_serializers/pull/841) Fix issue with embedding multiple associations under the same root key (@antstorm)
|
30
|
+
- [#748](https://github.com/rails-api/active_model_serializers/pull/748) Propagate serialization_options across associations (@raphaelpereira)
|
31
|
+
|
32
|
+
### [v0.9.3 (2015/01/21 20:29 +00:00)](https://github.com/rails-api/active_model_serializers/compare/v0.9.2...v0.9.3)
|
33
|
+
|
34
|
+
Features:
|
35
|
+
- [#774](https://github.com/rails-api/active_model_serializers/pull/774) Fix nested include attributes (@nhocki)
|
36
|
+
- [#771](https://github.com/rails-api/active_model_serializers/pull/771) Make linked resource type names consistent with root names (@sweatypitts)
|
37
|
+
- [#696](https://github.com/rails-api/active_model_serializers/pull/696) Explicitly set serializer for associations (@ggordon)
|
38
|
+
- [#700](https://github.com/rails-api/active_model_serializers/pull/700) sparse fieldsets (@arenoir)
|
39
|
+
- [#768](https://github.com/rails-api/active_model_serializers/pull/768) Adds support for `meta` and `meta_key` attribute (@kurko)
|
40
|
+
|
41
|
+
### [v0.9.2](https://github.com/rails-api/active_model_serializers/compare/v0.9.1...v0.9.2)
|
42
|
+
|
43
|
+
### [v0.9.1 (2014/12/04 11:54 +00:00)](https://github.com/rails-api/active_model_serializers/compare/v0.9.0...v0.9.1)
|
44
|
+
|
45
|
+
- [#707](https://github.com/rails-api/active_model_serializers/pull/707) A Friendly Note on Which AMS Version to Use (@jherdman)
|
46
|
+
- [#730](https://github.com/rails-api/active_model_serializers/pull/730) Fixes nested has_many links in JSONAPI (@kurko)
|
47
|
+
- [#718](https://github.com/rails-api/active_model_serializers/pull/718) Allow overriding the adapter with render option (@ggordon)
|
48
|
+
- [#720](https://github.com/rails-api/active_model_serializers/pull/720) Rename attribute with :key (0.8.x compatibility) (@ggordon)
|
49
|
+
- [#728](https://github.com/rails-api/active_model_serializers/pull/728) Use type as key for linked resources (@kurko)
|
50
|
+
- [#729](https://github.com/rails-api/active_model_serializers/pull/729) Use the new beta build env on Travis (@joshk)
|
51
|
+
- [#703](https://github.com/rails-api/active_model_serializers/pull/703) Support serializer and each_serializer options in renderer (@ggordon, @mieko)
|
52
|
+
- [#727](https://github.com/rails-api/active_model_serializers/pull/727) Includes links inside of linked resources (@kurko)
|
53
|
+
- [#726](https://github.com/rails-api/active_model_serializers/pull/726) Bugfix: include nested has_many associations (@kurko)
|
54
|
+
- [#722](https://github.com/rails-api/active_model_serializers/pull/722) Fix infinite recursion (@ggordon)
|
55
|
+
- [#1](https://github.com/rails-api/active_model_serializers/pull/1) Allow for the implicit use of ArraySerializer when :each_serializer is specified (@mieko)
|
56
|
+
- [#692](https://github.com/rails-api/active_model_serializers/pull/692) Include 'linked' member for json-api collections (@ggordon)
|
57
|
+
- [#714](https://github.com/rails-api/active_model_serializers/pull/714) Define as_json instead of to_json (@guilleiguaran)
|
58
|
+
- [#710](https://github.com/rails-api/active_model_serializers/pull/710) JSON-API: Don't include linked section if associations are empty (@guilleiguaran)
|
59
|
+
- [#711](https://github.com/rails-api/active_model_serializers/pull/711) Fixes rbx gems bundling on TravisCI (@kurko)
|
60
|
+
- [#709](https://github.com/rails-api/active_model_serializers/pull/709) Add type key when association name is different than object type (@guilleiguaran)
|
61
|
+
- [#708](https://github.com/rails-api/active_model_serializers/pull/708) Handle correctly null associations (@guilleiguaran)
|
62
|
+
- [#691](https://github.com/rails-api/active_model_serializers/pull/691) Fix embed option for associations (@jacob-s-son)
|
63
|
+
- [#689](https://github.com/rails-api/active_model_serializers/pull/689) Fix support for custom root in JSON-API adapter (@guilleiguaran)
|
64
|
+
- [#685](https://github.com/rails-api/active_model_serializers/pull/685) Serialize ids as strings in JSON-API adapter (@guilleiguaran)
|
65
|
+
- [#684](https://github.com/rails-api/active_model_serializers/pull/684) Refactor adapters to implement support for array serialization (@guilleiguaran)
|
66
|
+
- [#682](https://github.com/rails-api/active_model_serializers/pull/682) Include root by default in JSON-API serializers (@guilleiguaran)
|
67
|
+
- [#625](https://github.com/rails-api/active_model_serializers/pull/625) Add DSL for urls (@JordanFaust)
|
68
|
+
- [#677](https://github.com/rails-api/active_model_serializers/pull/677) Add support for embed: :ids option for in associations (@guilleiguaran)
|
69
|
+
- [#681](https://github.com/rails-api/active_model_serializers/pull/681) Check superclasses for Serializers (@quainjn)
|
70
|
+
- [#680](https://github.com/rails-api/active_model_serializers/pull/680) Add support for root keys (@NullVoxPopuli)
|
71
|
+
- [#675](https://github.com/rails-api/active_model_serializers/pull/675) Support Rails 4.2.0 (@tricknotes)
|
72
|
+
- [#667](https://github.com/rails-api/active_model_serializers/pull/667) Require only activemodel instead of full rails (@guilleiguaran)
|
73
|
+
- [#653](https://github.com/rails-api/active_model_serializers/pull/653) Add "_test" suffix to JsonApi::HasManyTest filename. (@alexgenco)
|
74
|
+
- [#631](https://github.com/rails-api/active_model_serializers/pull/631) Update build badge URL (@craiglittle)
|
75
|
+
|
76
|
+
### [v0.9.0](https://github.com/rails-api/active_model_serializers/compare/v0.9.0.alpha1...v0.9.0)
|
77
|
+
|
78
|
+
### [0.9.0.alpha1 - January 7, 2014](https://github.com/rails-api/active_model_serializers/compare/d72b66d4c...v0.9.0.alpha1)
|
79
|
+
|
80
|
+
### 0.9.0.pre
|
81
|
+
|
82
|
+
* The following methods were removed
|
83
|
+
- Model#active\_model\_serializer
|
84
|
+
- Serializer#include!
|
85
|
+
- Serializer#include?
|
86
|
+
- Serializer#attr\_disabled=
|
87
|
+
- Serializer#cache
|
88
|
+
- Serializer#perform\_caching
|
89
|
+
- Serializer#schema (needs more discussion)
|
90
|
+
- Serializer#attribute
|
91
|
+
- Serializer#include\_#{name}? (filter method added)
|
92
|
+
- Serializer#attributes (took a hash)
|
93
|
+
|
94
|
+
* The following things were added
|
95
|
+
- Serializer#filter method
|
96
|
+
- CONFIG object
|
97
|
+
|
98
|
+
* Remove support for ruby 1.8 versions.
|
99
|
+
|
100
|
+
* Require rails >= 3.2.
|
101
|
+
|
102
|
+
* Serializers for associations are being looked up in a parent serializer's namespace first. Same with controllers' namespaces.
|
103
|
+
|
104
|
+
* Added a "prefix" option in case you want to use a different version of serializer.
|
105
|
+
|
106
|
+
* Serializers default namespace can be set in `default_serializer_options` and inherited by associations.
|
107
|
+
|
1
108
|
# VERSION 0.9.0.pre
|
2
109
|
|
3
110
|
* The following methods were removed
|
@@ -20,6 +127,12 @@
|
|
20
127
|
|
21
128
|
* Require rails >= 3.2.
|
22
129
|
|
130
|
+
* Serializers for associations are being looked up in a parent serializer's namespace first. Same with controllers' namespaces.
|
131
|
+
|
132
|
+
* Added a "prefix" option in case you want to use a different version of serializer.
|
133
|
+
|
134
|
+
* Serializers default namespace can be set in `default_serializer_options` and inherited by associations.
|
135
|
+
|
23
136
|
# VERSION 0.8.1
|
24
137
|
|
25
138
|
* Fix bug whereby a serializer using 'options' would blow up.
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png?branch=0-9-stable)](https://travis-ci.org/rails-api/active_model_serializers)
|
2
|
-
[![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers)
|
1
|
+
[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png?branch=0-9-stable)](https://travis-ci.org/rails-api/active_model_serializers)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers)
|
3
3
|
|
4
4
|
# ActiveModel::Serializers
|
5
5
|
|
6
6
|
## Purpose
|
7
7
|
|
8
|
-
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
|
9
|
-
Objects that respond to read\_attribute\_for\_serialization
|
8
|
+
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
|
9
|
+
Objects that respond to read\_attribute\_for\_serialization
|
10
10
|
(including `ActiveModel` and `ActiveRecord` objects) are supported.
|
11
11
|
|
12
12
|
Serializers know about both a model and the `current_user`, so you can
|
@@ -85,7 +85,7 @@ it exists, use it to serialize the `Post`.
|
|
85
85
|
|
86
86
|
This also works with `respond_with`, which uses `to_json` under the hood. Also
|
87
87
|
note that any options passed to `render :json` will be passed to your
|
88
|
-
serializer and available as `@
|
88
|
+
serializer and available as `@serialization_options` inside.
|
89
89
|
|
90
90
|
To specify a custom serializer for an object, you can specify the
|
91
91
|
serializer when you render the object:
|
@@ -94,6 +94,17 @@ serializer when you render the object:
|
|
94
94
|
render json: @post, serializer: FancyPostSerializer
|
95
95
|
```
|
96
96
|
|
97
|
+
### Use serialization outside of ActionController::Base
|
98
|
+
|
99
|
+
When controller does not inherit from ActionController::Base,
|
100
|
+
include Serialization module manually:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class ApplicationController < ActionController::API
|
104
|
+
include ActionController::Serialization
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
97
108
|
## Arrays
|
98
109
|
|
99
110
|
In your controllers, when you use `render :json` for an array of objects, AMS will
|
@@ -229,13 +240,25 @@ ActiveModel::Serializer.setup do |config|
|
|
229
240
|
config.key_format = :lower_camel
|
230
241
|
end
|
231
242
|
|
232
|
-
class BlogLowerCamelSerializer < ActiveModel::Serializer
|
243
|
+
class BlogLowerCamelSerializer < ActiveModel::Serializer
|
233
244
|
format_keys :lower_camel
|
234
245
|
end
|
235
246
|
|
236
247
|
BlogSerializer.new(object, key_format: :lower_camel)
|
237
248
|
```
|
238
249
|
|
250
|
+
## Changing the default association key type
|
251
|
+
|
252
|
+
You can specify that serializers use unsuffixed names as association keys by default.
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
ActiveModel::Serializer.setup do |config|
|
256
|
+
config.default_key_type = :name
|
257
|
+
end
|
258
|
+
```
|
259
|
+
|
260
|
+
This will build association keys like `comments` or `author` instead of `comment_ids` or `author_id`.
|
261
|
+
|
239
262
|
## Getting the old version
|
240
263
|
|
241
264
|
If you find that your project is already relying on the old rails to_json
|
@@ -281,7 +304,7 @@ Since this shadows any attribute named `object`, you can include them through `o
|
|
281
304
|
|
282
305
|
```ruby
|
283
306
|
class VersionSerializer < ActiveModel::Serializer
|
284
|
-
|
307
|
+
attributes :version_object
|
285
308
|
|
286
309
|
def version_object
|
287
310
|
object.object
|
@@ -316,6 +339,7 @@ And it's also safe to mutate keys argument by doing keys.delete(:author)
|
|
316
339
|
in case you want to avoid creating two extra arrays. Note that if you do an
|
317
340
|
in-place modification, you still need to return the modified array.
|
318
341
|
|
342
|
+
### Alias Attribute
|
319
343
|
If you would like the key in the outputted JSON to be different from its name
|
320
344
|
in ActiveRecord, you can declare the attribute with the different name
|
321
345
|
and redefine that method:
|
@@ -354,7 +378,7 @@ The above usage of `:meta` will produce the following:
|
|
354
378
|
If you would like to change the meta key name you can use the `:meta_key` option:
|
355
379
|
|
356
380
|
```ruby
|
357
|
-
render json: @posts, serializer: CustomArraySerializer,
|
381
|
+
render json: @posts, serializer: CustomArraySerializer, meta_object: { total: 10 }, meta_key: :meta_object
|
358
382
|
```
|
359
383
|
|
360
384
|
The above usage of `:meta_key` will produce the following:
|
@@ -467,9 +491,6 @@ You may also use the `:serializer` option to specify a custom serializer class a
|
|
467
491
|
|
468
492
|
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
|
469
493
|
|
470
|
-
NOTE: polymorphic was removed because was only supported for has\_one
|
471
|
-
associations and is in the TODO list of the project.
|
472
|
-
|
473
494
|
## Embedding Associations
|
474
495
|
|
475
496
|
By default, associations will be embedded inside the serialized object. So if
|
@@ -516,15 +537,15 @@ Now, any associations will be supplied as an Array of IDs:
|
|
516
537
|
}
|
517
538
|
```
|
518
539
|
|
519
|
-
You may also choose to embed the IDs by the association's name underneath
|
520
|
-
`
|
540
|
+
You may also choose to embed the IDs by the association's name underneath a
|
541
|
+
`key` for the resource. For example, say we want to change `comment_ids`
|
521
542
|
to `comments` underneath a `links` key:
|
522
543
|
|
523
544
|
```ruby
|
524
545
|
class PostSerializer < ActiveModel::Serializer
|
525
546
|
attributes :id, :title, :body
|
526
547
|
|
527
|
-
has_many :comments, embed: ids, embed_namespace: :links
|
548
|
+
has_many :comments, embed: :ids, key: :comments, embed_namespace: :links
|
528
549
|
end
|
529
550
|
```
|
530
551
|
|
@@ -615,7 +636,7 @@ the root document (say, `linked`), you can specify an `embed_in_root_key`:
|
|
615
636
|
|
616
637
|
```ruby
|
617
638
|
class PostSerializer < ActiveModel::Serializer
|
618
|
-
embed:
|
639
|
+
embed :ids, include: true, embed_in_root_key: :linked
|
619
640
|
|
620
641
|
attributes: :id, :title, :body
|
621
642
|
has_many :comments, :tags
|
@@ -646,8 +667,8 @@ The above would yield the following JSON document:
|
|
646
667
|
}
|
647
668
|
```
|
648
669
|
|
649
|
-
When side-loading data, your serializer cannot have the `{ root: false }` option,
|
650
|
-
as this would lead to invalid JSON. If you do not have a root key, the `include`
|
670
|
+
When side-loading data, your serializer cannot have the `{ root: false }` option,
|
671
|
+
as this would lead to invalid JSON. If you do not have a root key, the `include`
|
651
672
|
instruction will be ignored
|
652
673
|
|
653
674
|
You can also specify a different root for the embedded objects than the key
|
@@ -686,7 +707,7 @@ class PostSerializer < ActiveModel::Serializer
|
|
686
707
|
embed :ids, include: true
|
687
708
|
|
688
709
|
attributes :id, :title, :body
|
689
|
-
has_many :comments,
|
710
|
+
has_many :comments, key: :external_id
|
690
711
|
end
|
691
712
|
```
|
692
713
|
|
@@ -714,6 +735,78 @@ data looking for information, is extremely useful.
|
|
714
735
|
If you are mostly working with the data in simple scenarios and manually making
|
715
736
|
Ajax requests, you probably just want to use the default embedded behavior.
|
716
737
|
|
738
|
+
|
739
|
+
## Embedding Polymorphic Associations
|
740
|
+
|
741
|
+
Because we need both the id and the type to be able to identify a polymorphic associated model, these are serialized in a slightly different format than common ones.
|
742
|
+
|
743
|
+
When embedding entire objects:
|
744
|
+
|
745
|
+
```ruby
|
746
|
+
class PostSerializer < ActiveModel::Serializer
|
747
|
+
attributes :id, :title
|
748
|
+
has_many :attachments, polymorphic: true
|
749
|
+
end
|
750
|
+
```
|
751
|
+
|
752
|
+
```json
|
753
|
+
{
|
754
|
+
"post": {
|
755
|
+
"id": 1,
|
756
|
+
"title": "New post",
|
757
|
+
"attachments": [
|
758
|
+
{
|
759
|
+
"type": "image",
|
760
|
+
"image": {
|
761
|
+
"id": 3,
|
762
|
+
"name": "logo",
|
763
|
+
"url": "http://images.com/logo.jpg"
|
764
|
+
}
|
765
|
+
},
|
766
|
+
{
|
767
|
+
"type": "video",
|
768
|
+
"video": {
|
769
|
+
"id": 12,
|
770
|
+
"uid": "XCSSMDFWW",
|
771
|
+
"source": "youtube"
|
772
|
+
}
|
773
|
+
}
|
774
|
+
]
|
775
|
+
}
|
776
|
+
}
|
777
|
+
```
|
778
|
+
|
779
|
+
When embedding ids:
|
780
|
+
|
781
|
+
```ruby
|
782
|
+
class PostSerializer < ActiveModel::Serializer
|
783
|
+
embed :ids
|
784
|
+
|
785
|
+
attributes :id, :title
|
786
|
+
has_many :attachments, polymorphic: true
|
787
|
+
end
|
788
|
+
```
|
789
|
+
|
790
|
+
```json
|
791
|
+
{
|
792
|
+
"post": {
|
793
|
+
"id": 1,
|
794
|
+
"title": "New post",
|
795
|
+
"attachment_ids": [
|
796
|
+
{
|
797
|
+
"type": "image",
|
798
|
+
"id": 12
|
799
|
+
},
|
800
|
+
{
|
801
|
+
"type": "video",
|
802
|
+
"id": 3
|
803
|
+
}
|
804
|
+
]
|
805
|
+
}
|
806
|
+
}
|
807
|
+
```
|
808
|
+
|
809
|
+
|
717
810
|
## Customizing Scope
|
718
811
|
|
719
812
|
In a serializer, `current_user` is the current authorization scope which the controller
|
@@ -839,7 +932,7 @@ now generates:
|
|
839
932
|
class PostSerializer < ApplicationSerializer
|
840
933
|
attributes :id
|
841
934
|
end
|
842
|
-
|
935
|
+
```
|
843
936
|
|
844
937
|
# Design and Implementation Guidelines
|
845
938
|
|
@@ -45,18 +45,40 @@ module ActionController
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
|
49
|
-
|
48
|
+
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
49
|
+
define_method renderer_method do |resource, options|
|
50
|
+
serializer = build_json_serializer(resource, options)
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
if serializer
|
53
|
+
super(serializer, options)
|
54
|
+
else
|
55
|
+
super(resource, options)
|
56
|
+
end
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
58
60
|
private
|
59
61
|
|
62
|
+
def namespace_for_serializer
|
63
|
+
@namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
|
64
|
+
end
|
65
|
+
|
66
|
+
def namespace_for_class(klass)
|
67
|
+
if Module.method_defined?(:module_parent)
|
68
|
+
klass.module_parent
|
69
|
+
else
|
70
|
+
klass.parent
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def default_serializer(resource)
|
75
|
+
options = {}.tap do |o|
|
76
|
+
o[:namespace] = namespace_for_serializer if namespace_for_serializer
|
77
|
+
end
|
78
|
+
|
79
|
+
ActiveModel::Serializer.serializer_for(resource, options)
|
80
|
+
end
|
81
|
+
|
60
82
|
def default_serializer_options
|
61
83
|
{}
|
62
84
|
end
|
@@ -68,10 +90,15 @@ module ActionController
|
|
68
90
|
|
69
91
|
def build_json_serializer(resource, options = {})
|
70
92
|
options = default_serializer_options.merge(options)
|
93
|
+
@namespace_for_serializer = options.fetch(:namespace, nil)
|
71
94
|
|
72
|
-
if serializer = options.fetch(:serializer,
|
95
|
+
if serializer = options.fetch(:serializer, default_serializer(resource))
|
73
96
|
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
74
|
-
|
97
|
+
|
98
|
+
if resource.respond_to?(:to_ary)
|
99
|
+
options[:resource_name] = controller_name
|
100
|
+
options[:namespace] = namespace_for_serializer if namespace_for_serializer
|
101
|
+
end
|
75
102
|
|
76
103
|
serializer.new(resource, options)
|
77
104
|
end
|
@@ -3,11 +3,11 @@ module ActionController
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
setup :
|
7
|
-
teardown :
|
6
|
+
setup :setup_serialization_subscriptions
|
7
|
+
teardown :teardown_serialization_subscriptions
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def setup_serialization_subscriptions
|
11
11
|
@serializers = Hash.new(0)
|
12
12
|
|
13
13
|
ActiveSupport::Notifications.subscribe("!serialize.active_model_serializers") do |name, start, finish, id, payload|
|
@@ -16,7 +16,7 @@ module ActionController
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def teardown_serialization_subscriptions
|
20
20
|
ActiveSupport::Notifications.unsubscribe("!serialize.active_model_serializers")
|
21
21
|
end
|
22
22
|
|