Skip to content

Commit

Permalink
Merge pull request #850 from mateomurphy/association-override-fix
Browse files Browse the repository at this point in the history
Use association value for determining serializer used
  • Loading branch information
guilleiguaran committed Mar 22, 2015
2 parents 9cf45e7 + ca985e1 commit e901a21
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ def each_association(&block)
self.class._associations.dup.each do |name, association_options|
next unless object

association = object.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(
association_value,
Expand Down
10 changes: 8 additions & 2 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ def test_render_with_cache_enable
id: 1,
body: 'ZOMG A COMMENT' }
],
blog: nil,
blog: {
id: 999,
name: 'Custom blog'
},
author: {
id: 1,
name: 'Joao Moura.'
Expand Down Expand Up @@ -190,7 +193,10 @@ def test_render_with_cache_enable_and_expired
id: 1,
body: 'ZOMG A COMMENT' }
],
blog: nil,
blog: {
id: 999,
name: 'Custom blog'
},
author: {
id: 1,
name: 'Joao Moura.'
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_include_nil_author
serializer = PostSerializer.new(@anonymous_post)
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
Expand Down
5 changes: 4 additions & 1 deletion test/adapter/json/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def test_include_multiple_posts
id: 1,
name: "Steve K."
},
blog: nil
blog: {
id: 999,
name: "Custom blog"
}
}]
assert_equal expected, @adapter.serializable_hash
end
Expand Down
4 changes: 2 additions & 2 deletions test/adapter/json_api/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_include_nil_author
serializer = PostSerializer.new(@anonymous_post)
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

def test_include_type_for_association_when_different_than_name
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_include_linked_resources_with_type_name
id: "43",
links: {
comments: [],
blog: nil,
blog: "999",
author: nil
}
}]
Expand Down
4 changes: 2 additions & 2 deletions test/adapter/json_api/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def setup
def test_include_multiple_posts
assert_equal([
{ 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])
end

def test_limiting_fields
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
assert_equal([
{ 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])
end

Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_include_multiple_posts_and_linked
body: "Body",
links: {
comments: [],
blog: nil,
blog: "999",
author: "1"
}
}
Expand Down

0 comments on commit e901a21

Please sign in to comment.