|
1 |
| -[](https://travis-ci.org/rails-api/active_model_serializers) |
2 |
| -[](https://codeclimate.com/github/rails-api/active_model_serializers) |
| 1 | +[](https://travis-ci.org/rails-api/active_model_serializers) |
| 2 | +[](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
|
@@ -229,7 +229,7 @@ ActiveModel::Serializer.setup do |config|
|
229 | 229 | config.key_format = :lower_camel
|
230 | 230 | end
|
231 | 231 |
|
232 |
| -class BlogLowerCamelSerializer < ActiveModel::Serializer |
| 232 | +class BlogLowerCamelSerializer < ActiveModel::Serializer |
233 | 233 | format_keys :lower_camel
|
234 | 234 | end
|
235 | 235 |
|
@@ -467,9 +467,6 @@ You may also use the `:serializer` option to specify a custom serializer class a
|
467 | 467 |
|
468 | 468 | Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
|
469 | 469 |
|
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 | 470 | ## Embedding Associations
|
474 | 471 |
|
475 | 472 | By default, associations will be embedded inside the serialized object. So if
|
@@ -646,8 +643,8 @@ The above would yield the following JSON document:
|
646 | 643 | }
|
647 | 644 | ```
|
648 | 645 |
|
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` |
| 646 | +When side-loading data, your serializer cannot have the `{ root: false }` option, |
| 647 | +as this would lead to invalid JSON. If you do not have a root key, the `include` |
651 | 648 | instruction will be ignored
|
652 | 649 |
|
653 | 650 | You can also specify a different root for the embedded objects than the key
|
@@ -714,6 +711,78 @@ data looking for information, is extremely useful.
|
714 | 711 | If you are mostly working with the data in simple scenarios and manually making
|
715 | 712 | Ajax requests, you probably just want to use the default embedded behavior.
|
716 | 713 |
|
| 714 | + |
| 715 | +## Embedding Polymorphic Associations |
| 716 | + |
| 717 | +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. |
| 718 | + |
| 719 | +When embedding entire objects: |
| 720 | + |
| 721 | +```ruby |
| 722 | +class PostSerializer < ActiveModel::Serializer |
| 723 | + attributes :id, :title |
| 724 | + has_many :attachments, polymorphic: true |
| 725 | +end |
| 726 | +``` |
| 727 | + |
| 728 | +```json |
| 729 | +{ |
| 730 | + "post": { |
| 731 | + "id": 1, |
| 732 | + "title": "New post", |
| 733 | + "attachments": [ |
| 734 | + { |
| 735 | + "type": "image" |
| 736 | + "image": { |
| 737 | + "id": 3 |
| 738 | + "name": "logo" |
| 739 | + "url": "http://images.com/logo.jpg" |
| 740 | + } |
| 741 | + }, |
| 742 | + { |
| 743 | + "type": "video" |
| 744 | + "video": { |
| 745 | + "id": 12 |
| 746 | + "uid": "XCSSMDFWW" |
| 747 | + "source": "youtube" |
| 748 | + } |
| 749 | + } |
| 750 | + ] |
| 751 | + } |
| 752 | +} |
| 753 | +``` |
| 754 | + |
| 755 | +When embedding ids: |
| 756 | + |
| 757 | +```ruby |
| 758 | +class PostSerializer < ActiveModel::Serializer |
| 759 | + embed :ids |
| 760 | + |
| 761 | + attributes :id, :title |
| 762 | + has_many :attachments, polymorphic: true |
| 763 | +end |
| 764 | +``` |
| 765 | + |
| 766 | +```json |
| 767 | +{ |
| 768 | + "post": { |
| 769 | + "id": 1, |
| 770 | + "title": "New post", |
| 771 | + "attachment_ids": [ |
| 772 | + { |
| 773 | + "type": "image" |
| 774 | + "id": 12 |
| 775 | + }, |
| 776 | + { |
| 777 | + "type": "video" |
| 778 | + "id": 3 |
| 779 | + } |
| 780 | + ] |
| 781 | + } |
| 782 | +} |
| 783 | +``` |
| 784 | + |
| 785 | + |
717 | 786 | ## Customizing Scope
|
718 | 787 |
|
719 | 788 | In a serializer, `current_user` is the current authorization scope which the controller
|
|
0 commit comments