From ca985e1afd7cd81f12485754e740539732e7867f Mon Sep 17 00:00:00 2001 From: Mateo Murphy Date: Thu, 19 Mar 2015 23:28:00 -0400 Subject: [PATCH] Use association value for determining serializer used Ensures overridden association value works when orignal association does not return a result. --- lib/active_model/serializer.rb | 4 ++-- test/action_controller/serialization_test.rb | 10 ++++++++-- test/adapter/json/belongs_to_test.rb | 2 +- test/adapter/json/collection_test.rb | 5 ++++- test/adapter/json_api/belongs_to_test.rb | 4 ++-- test/adapter/json_api/collection_test.rb | 4 ++-- test/adapter/json_api/linked_test.rb | 2 +- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 7badb04a6..9f5b4d13c 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -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, diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index 0aeb895ae..a10cb039b 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -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.' @@ -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.' diff --git a/test/adapter/json/belongs_to_test.rb b/test/adapter/json/belongs_to_test.rb index fb73779d9..b711e20b4 100644 --- a/test/adapter/json/belongs_to_test.rb +++ b/test/adapter/json/belongs_to_test.rb @@ -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 diff --git a/test/adapter/json/collection_test.rb b/test/adapter/json/collection_test.rb index 77c43e290..b4acf6f98 100644 --- a/test/adapter/json/collection_test.rb +++ b/test/adapter/json/collection_test.rb @@ -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 diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index a235f5d4e..206c41678 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -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 @@ -116,7 +116,7 @@ def test_include_linked_resources_with_type_name id: "43", links: { comments: [], - blog: nil, + blog: "999", author: nil } }] diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index 2054af838..3f2070653 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -27,7 +27,7 @@ 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 @@ -35,7 +35,7 @@ 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 diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index f11b06e3e..728972397 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -170,7 +170,7 @@ def test_include_multiple_posts_and_linked body: "Body", links: { comments: [], - blog: nil, + blog: "999", author: "1" } }