Skip to content

Commit e901a21

Browse files
committed
Merge pull request #850 from mateomurphy/association-override-fix
Use association value for determining serializer used
2 parents 9cf45e7 + ca985e1 commit e901a21

File tree

7 files changed

+20
-11
lines changed

7 files changed

+20
-11
lines changed

lib/active_model/serializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ def each_association(&block)
181181
self.class._associations.dup.each do |name, association_options|
182182
next unless object
183183

184-
association = object.send(name)
185184
association_value = send(name)
186-
serializer_class = ActiveModel::Serializer.serializer_for(association, association_options)
185+
186+
serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
187187

188188
serializer = serializer_class.new(
189189
association_value,

test/action_controller/serialization_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ def test_render_with_cache_enable
159159
id: 1,
160160
body: 'ZOMG A COMMENT' }
161161
],
162-
blog: nil,
162+
blog: {
163+
id: 999,
164+
name: 'Custom blog'
165+
},
163166
author: {
164167
id: 1,
165168
name: 'Joao Moura.'
@@ -190,7 +193,10 @@ def test_render_with_cache_enable_and_expired
190193
id: 1,
191194
body: 'ZOMG A COMMENT' }
192195
],
193-
blog: nil,
196+
blog: {
197+
id: 999,
198+
name: 'Custom blog'
199+
},
194200
author: {
195201
id: 1,
196202
name: 'Joao Moura.'

test/adapter/json/belongs_to_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_include_nil_author
3232
serializer = PostSerializer.new(@anonymous_post)
3333
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
3434

35-
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: nil, author: nil}, adapter.serializable_hash)
35+
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}, adapter.serializable_hash)
3636
end
3737
end
3838
end

test/adapter/json/collection_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def test_include_multiple_posts
4545
id: 1,
4646
name: "Steve K."
4747
},
48-
blog: nil
48+
blog: {
49+
id: 999,
50+
name: "Custom blog"
51+
}
4952
}]
5053
assert_equal expected, @adapter.serializable_hash
5154
end

test/adapter/json_api/belongs_to_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_include_nil_author
6767
serializer = PostSerializer.new(@anonymous_post)
6868
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
6969

70-
assert_equal({comments: [], blog: nil, author: nil}, adapter.serializable_hash[:posts][:links])
70+
assert_equal({comments: [], blog: "999", author: nil}, adapter.serializable_hash[:posts][:links])
7171
end
7272

7373
def test_include_type_for_association_when_different_than_name
@@ -116,7 +116,7 @@ def test_include_linked_resources_with_type_name
116116
id: "43",
117117
links: {
118118
comments: [],
119-
blog: nil,
119+
blog: "999",
120120
author: nil
121121
}
122122
}]

test/adapter/json_api/collection_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ def setup
2727
def test_include_multiple_posts
2828
assert_equal([
2929
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], blog: "999", author: "1" } },
30-
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: nil, author: "1" } }
30+
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "1" } }
3131
], @adapter.serializable_hash[:posts])
3232
end
3333

3434
def test_limiting_fields
3535
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
3636
assert_equal([
3737
{ title: "Hello!!", links: { comments: [], blog: "999", author: "1" } },
38-
{ title: "New Post", links: { comments: [], blog: nil, author: "1" } }
38+
{ title: "New Post", links: { comments: [], blog: "999", author: "1" } }
3939
], @adapter.serializable_hash[:posts])
4040
end
4141

test/adapter/json_api/linked_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def test_include_multiple_posts_and_linked
170170
body: "Body",
171171
links: {
172172
comments: [],
173-
blog: nil,
173+
blog: "999",
174174
author: "1"
175175
}
176176
}

0 commit comments

Comments
 (0)