File tree 5 files changed +114
-0
lines changed
5 files changed +114
-0
lines changed Original file line number Diff line number Diff line change 47
47
- [ #1535 ] ( https://github.com/rails-api/active_model_serializers/pull/1535 ) Move the adapter and adapter folder to
48
48
active_model_serializers folder and changes the module namespace. (@domitian @bf4 )
49
49
- [ #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 )
50
51
51
52
### v0.10.0.rc4 (2016/01/27 11:00 +00:00)
52
53
Breaking changes:
Original file line number Diff line number Diff line change @@ -76,6 +76,18 @@ def blog
76
76
end
77
77
```
78
78
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
+
79
91
### Caching
80
92
81
93
#### ::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 :
'[email protected] ' )
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 18
18
t . references :post
19
19
t . timestamp null : false
20
20
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
21
32
end
22
33
23
34
module ARModels
Original file line number Diff line number Diff line change @@ -72,6 +72,14 @@ def cache_key
72
72
end
73
73
end
74
74
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
+
75
83
module Spam ; end
76
84
Spam ::UnrelatedLink = Class . new ( Model )
77
85
@@ -233,6 +241,16 @@ def maker
233
241
end
234
242
end
235
243
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
+
236
254
Spam ::UnrelatedLinkSerializer = Class . new ( ActiveModel ::Serializer ) do
237
255
cache only : [ :id ]
238
256
attributes :id
You can’t perform that action at this time.
0 commit comments