File tree Expand file tree Collapse file tree 5 files changed +114
-0
lines changed Expand file tree Collapse file tree 5 files changed +114
-0
lines changed Original file line number Diff line number Diff line change 4747- [ #1535 ] ( https://github.com/rails-api/active_model_serializers/pull/1535 ) Move the adapter and adapter folder to
4848 active_model_serializers folder and changes the module namespace. (@domitian @bf4 )
4949- [ #1497 ] ( https://github.com/rails-api/active_model_serializers/pull/1497 ) Add JRuby-9000 to appveyor.yml(@corainchicago )
50+ - [ #1420 ] ( https://github.com/rails-api/active_model_serializers/pull/1420 ) Adds tests and documentation for polymorphism(@marcgarreau )
5051
5152### v0.10.0.rc4 (2016/01/27 11:00 +00:00)
5253Breaking changes:
Original file line number Diff line number Diff line change @@ -76,6 +76,18 @@ def blog
7676end
7777```
7878
79+ ### Polymorphic Relationships
80+
81+ Polymorphic relationships are serialized by specifying the relationship, like any other association. For example:
82+
83+ ``` ruby
84+ class PictureSerializer < ActiveModel ::Serializer
85+ has_one :imageable
86+ end
87+ ```
88+
89+ For more context, see the [ tests] ( ../../test/adapter/polymorphic_test.rb ) for each adapter.
90+
7991### Caching
8092
8193#### ::cache
Original file line number Diff line number Diff line change 1+ require 'test_helper'
2+
3+ module ActiveModel
4+ class Serializer
5+ module Adapter
6+ class PolymorphicTest < ActiveSupport ::TestCase
7+ setup do
8+ @employee = Employee . new ( id : 42 , name : 'Zoop Zoopler' , email : 'zoop@example.com' )
9+ @picture = @employee . pictures . new ( id : 1 , title : 'headshot-1.jpg' )
10+ @picture . imageable = @employee
11+
12+ @attributes_serialization = serializable ( @picture , serializer : PolymorphicBelongsToSerializer ) # uses default adapter: attributes
13+ @json_serialization = serializable ( @picture , adapter : :json , serializer : PolymorphicBelongsToSerializer )
14+ @json_api_serialization = serializable ( @picture , adapter : :json_api , serializer : PolymorphicBelongsToSerializer )
15+ end
16+
17+ def test_attributes_serialization
18+ expected =
19+ {
20+ id : 1 ,
21+ title : 'headshot-1.jpg' ,
22+ imageable : {
23+ id : 42 ,
24+ name : 'Zoop Zoopler'
25+ }
26+ }
27+
28+ assert_equal ( expected , @attributes_serialization . as_json )
29+ end
30+
31+ def test_json_serializer
32+ expected =
33+ {
34+ picture : {
35+ id : 1 ,
36+ title : 'headshot-1.jpg' ,
37+ imageable : {
38+ id : 42 ,
39+ name : 'Zoop Zoopler'
40+ }
41+ }
42+ }
43+
44+ assert_equal ( expected , @json_serialization . as_json )
45+ end
46+
47+ def test_json_api_serializer
48+ expected =
49+ {
50+ data : {
51+ id : '1' ,
52+ type : 'pictures' ,
53+ attributes : {
54+ title : 'headshot-1.jpg'
55+ } ,
56+ relationships : {
57+ imageable : {
58+ data : {
59+ id : '42' ,
60+ type : 'employees'
61+ }
62+ }
63+ }
64+ }
65+ }
66+
67+ assert_equal ( expected , @json_api_serialization . as_json )
68+ end
69+ end
70+ end
71+ end
72+ end
Original file line number Diff line number Diff line change 1818 t . references :post
1919 t . timestamp null : false
2020 end
21+ create_table :employees , force : true do |t |
22+ t . string :name
23+ t . string :email
24+ t . timestamp null : false
25+ end
26+ create_table :pictures , force : true do |t |
27+ t . string :title
28+ t . string :imageable_type
29+ t . string :imageable_id
30+ t . timestamp null : false
31+ end
2132end
2233
2334module ARModels
Original file line number Diff line number Diff line change @@ -72,6 +72,14 @@ def cache_key
7272 end
7373end
7474
75+ class Employee < ActiveRecord ::Base
76+ has_many :pictures , as : :imageable
77+ end
78+
79+ class Picture < ActiveRecord ::Base
80+ belongs_to :imageable , polymorphic : true
81+ end
82+
7583module Spam ; end
7684Spam ::UnrelatedLink = Class . new ( Model )
7785
@@ -233,6 +241,16 @@ def maker
233241 end
234242end
235243
244+ PolymorphicHasManySerializer = Class . new ( ActiveModel ::Serializer ) do
245+ attributes :id , :name
246+ end
247+
248+ PolymorphicBelongsToSerializer = Class . new ( ActiveModel ::Serializer ) do
249+ attributes :id , :title
250+
251+ has_one :imageable , serializer : PolymorphicHasManySerializer
252+ end
253+
236254Spam ::UnrelatedLinkSerializer = Class . new ( ActiveModel ::Serializer ) do
237255 cache only : [ :id ]
238256 attributes :id
You can’t perform that action at this time.
0 commit comments