mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1420 from marcgarreau/master
Adds tests and documentation for polymorphism
This commit is contained in:
commit
a85d6af1c4
@ -47,6 +47,7 @@ Misc:
|
|||||||
- [#1535](https://github.com/rails-api/active_model_serializers/pull/1535) Move the adapter and adapter folder to
|
- [#1535](https://github.com/rails-api/active_model_serializers/pull/1535) Move the adapter and adapter folder to
|
||||||
active_model_serializers folder and changes the module namespace. (@domitian @bf4)
|
active_model_serializers folder and changes the module namespace. (@domitian @bf4)
|
||||||
- [#1497](https://github.com/rails-api/active_model_serializers/pull/1497) Add JRuby-9000 to appveyor.yml(@corainchicago)
|
- [#1497](https://github.com/rails-api/active_model_serializers/pull/1497) Add JRuby-9000 to appveyor.yml(@corainchicago)
|
||||||
|
- [#1420](https://github.com/rails-api/active_model_serializers/pull/1420) Adds tests and documentation for polymorphism(@marcgarreau)
|
||||||
|
|
||||||
### v0.10.0.rc4 (2016/01/27 11:00 +00:00)
|
### v0.10.0.rc4 (2016/01/27 11:00 +00:00)
|
||||||
Breaking changes:
|
Breaking changes:
|
||||||
|
|||||||
@ -76,6 +76,18 @@ def blog
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Polymorphic Relationships
|
||||||
|
|
||||||
|
Polymorphic relationships are serialized by specifying the relationship, like any other association. For example:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PictureSerializer < ActiveModel::Serializer
|
||||||
|
has_one :imageable
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
For more context, see the [tests](../../test/adapter/polymorphic_test.rb) for each adapter.
|
||||||
|
|
||||||
### Caching
|
### Caching
|
||||||
|
|
||||||
#### ::cache
|
#### ::cache
|
||||||
|
|||||||
72
test/adapter/polymorphic_test.rb
Normal file
72
test/adapter/polymorphic_test.rb
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
module Adapter
|
||||||
|
class PolymorphicTest < ActiveSupport::TestCase
|
||||||
|
setup do
|
||||||
|
@employee = Employee.new(id: 42, name: 'Zoop Zoopler', email: 'zoop@example.com')
|
||||||
|
@picture = @employee.pictures.new(id: 1, title: 'headshot-1.jpg')
|
||||||
|
@picture.imageable = @employee
|
||||||
|
|
||||||
|
@attributes_serialization = serializable(@picture, serializer: PolymorphicBelongsToSerializer) # uses default adapter: attributes
|
||||||
|
@json_serialization = serializable(@picture, adapter: :json, serializer: PolymorphicBelongsToSerializer)
|
||||||
|
@json_api_serialization = serializable(@picture, adapter: :json_api, serializer: PolymorphicBelongsToSerializer)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_attributes_serialization
|
||||||
|
expected =
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
title: 'headshot-1.jpg',
|
||||||
|
imageable: {
|
||||||
|
id: 42,
|
||||||
|
name: 'Zoop Zoopler'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_equal(expected, @attributes_serialization.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_json_serializer
|
||||||
|
expected =
|
||||||
|
{
|
||||||
|
picture: {
|
||||||
|
id: 1,
|
||||||
|
title: 'headshot-1.jpg',
|
||||||
|
imageable: {
|
||||||
|
id: 42,
|
||||||
|
name: 'Zoop Zoopler'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_equal(expected, @json_serialization.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_json_api_serializer
|
||||||
|
expected =
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
id: '1',
|
||||||
|
type: 'pictures',
|
||||||
|
attributes: {
|
||||||
|
title: 'headshot-1.jpg'
|
||||||
|
},
|
||||||
|
relationships: {
|
||||||
|
imageable: {
|
||||||
|
data: {
|
||||||
|
id: '42',
|
||||||
|
type: 'employees'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_equal(expected, @json_api_serialization.as_json)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
11
test/fixtures/active_record.rb
vendored
11
test/fixtures/active_record.rb
vendored
@ -18,6 +18,17 @@ ActiveRecord::Schema.define do
|
|||||||
t.references :post
|
t.references :post
|
||||||
t.timestamp null: false
|
t.timestamp null: false
|
||||||
end
|
end
|
||||||
|
create_table :employees, force: true do |t|
|
||||||
|
t.string :name
|
||||||
|
t.string :email
|
||||||
|
t.timestamp null: false
|
||||||
|
end
|
||||||
|
create_table :pictures, force: true do |t|
|
||||||
|
t.string :title
|
||||||
|
t.string :imageable_type
|
||||||
|
t.string :imageable_id
|
||||||
|
t.timestamp null: false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ARModels
|
module ARModels
|
||||||
|
|||||||
18
test/fixtures/poro.rb
vendored
18
test/fixtures/poro.rb
vendored
@ -72,6 +72,14 @@ Comment = Class.new(Model) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Employee < ActiveRecord::Base
|
||||||
|
has_many :pictures, as: :imageable
|
||||||
|
end
|
||||||
|
|
||||||
|
class Picture < ActiveRecord::Base
|
||||||
|
belongs_to :imageable, polymorphic: true
|
||||||
|
end
|
||||||
|
|
||||||
module Spam; end
|
module Spam; end
|
||||||
Spam::UnrelatedLink = Class.new(Model)
|
Spam::UnrelatedLink = Class.new(Model)
|
||||||
|
|
||||||
@ -233,6 +241,16 @@ VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
PolymorphicHasManySerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :id, :name
|
||||||
|
end
|
||||||
|
|
||||||
|
PolymorphicBelongsToSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :id, :title
|
||||||
|
|
||||||
|
has_one :imageable, serializer: PolymorphicHasManySerializer
|
||||||
|
end
|
||||||
|
|
||||||
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
cache only: [:id]
|
cache only: [:id]
|
||||||
attributes :id
|
attributes :id
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user