Skip to content

Commit a85d6af

Browse files
committed
Merge pull request #1420 from marcgarreau/master
Adds tests and documentation for polymorphism
2 parents 54ddbe2 + 045fa9b commit a85d6af

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Misc:
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)
5253
Breaking changes:

docs/general/serializers.md

+12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ def blog
7676
end
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

test/adapter/polymorphic_test.rb

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

test/fixtures/active_record.rb

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
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
2132
end
2233

2334
module ARModels

test/fixtures/poro.rb

+18
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def cache_key
7272
end
7373
end
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+
7583
module Spam; end
7684
Spam::UnrelatedLink = Class.new(Model)
7785

@@ -233,6 +241,16 @@ def maker
233241
end
234242
end
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+
236254
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
237255
cache only: [:id]
238256
attributes :id

0 commit comments

Comments
 (0)