Use association value for determining serializer used

Ensures overridden association value works when orignal association does not return a result.
This commit is contained in:
Mateo Murphy 2015-03-19 23:28:00 -04:00
parent 9058d5fce7
commit ca985e1afd
7 changed files with 20 additions and 11 deletions

View File

@ -181,9 +181,9 @@ module ActiveModel
self.class._associations.dup.each do |name, association_options| self.class._associations.dup.each do |name, association_options|
next unless object next unless object
association = object.send(name)
association_value = send(name) association_value = send(name)
serializer_class = ActiveModel::Serializer.serializer_for(association, association_options)
serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
serializer = serializer_class.new( serializer = serializer_class.new(
association_value, association_value,

View File

@ -159,7 +159,10 @@ module ActionController
id: 1, id: 1,
body: 'ZOMG A COMMENT' } body: 'ZOMG A COMMENT' }
], ],
blog: nil, blog: {
id: 999,
name: 'Custom blog'
},
author: { author: {
id: 1, id: 1,
name: 'Joao Moura.' name: 'Joao Moura.'
@ -190,7 +193,10 @@ module ActionController
id: 1, id: 1,
body: 'ZOMG A COMMENT' } body: 'ZOMG A COMMENT' }
], ],
blog: nil, blog: {
id: 999,
name: 'Custom blog'
},
author: { author: {
id: 1, id: 1,
name: 'Joao Moura.' name: 'Joao Moura.'

View File

@ -32,7 +32,7 @@ module ActiveModel
serializer = PostSerializer.new(@anonymous_post) serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: nil, author: nil}, adapter.serializable_hash) assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}, adapter.serializable_hash)
end end
end end
end end

View File

@ -45,7 +45,10 @@ module ActiveModel
id: 1, id: 1,
name: "Steve K." name: "Steve K."
}, },
blog: nil blog: {
id: 999,
name: "Custom blog"
}
}] }]
assert_equal expected, @adapter.serializable_hash assert_equal expected, @adapter.serializable_hash
end end

View File

@ -67,7 +67,7 @@ module ActiveModel
serializer = PostSerializer.new(@anonymous_post) serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
assert_equal({comments: [], blog: nil, author: nil}, adapter.serializable_hash[:posts][:links]) assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:posts][:links])
end end
def test_include_type_for_association_when_different_than_name def test_include_type_for_association_when_different_than_name
@ -116,7 +116,7 @@ module ActiveModel
id: "43", id: "43",
links: { links: {
comments: [], comments: [],
blog: nil, blog: "999",
author: nil author: nil
} }
}] }]

View File

@ -27,7 +27,7 @@ module ActiveModel
def test_include_multiple_posts def test_include_multiple_posts
assert_equal([ assert_equal([
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } }, { title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } },
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: nil, author: "1" } } { title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "1" } }
], @adapter.serializable_hash[:posts]) ], @adapter.serializable_hash[:posts])
end end
@ -35,7 +35,7 @@ module ActiveModel
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title']) @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
assert_equal([ assert_equal([
{ title: "Hello!!", links: { comments: [], blog: "999", author: "1" } }, { title: "Hello!!", links: { comments: [], blog: "999", author: "1" } },
{ title: "New Post", links: { comments: [], blog: nil, author: "1" } } { title: "New Post", links: { comments: [], blog: "999", author: "1" } }
], @adapter.serializable_hash[:posts]) ], @adapter.serializable_hash[:posts])
end end

View File

@ -170,7 +170,7 @@ module ActiveModel
body: "Body", body: "Body",
links: { links: {
comments: [], comments: [],
blog: nil, blog: "999",
author: "1" author: "1"
} }
} }