Skip to content

Commit

Permalink
Merge pull request #852 from mateomurphy/serializer-options-fix
Browse files Browse the repository at this point in the history
Fix options merge order in `each_association`
  • Loading branch information
guilleiguaran committed Mar 22, 2015
2 parents e901a21 + 03372ea commit b68d7f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def each_association(&block)

serializer = serializer_class.new(
association_value,
serializer_from_options(association_options).merge(options)
options.merge(serializer_from_options(association_options))
) if serializer_class

if block_given?
Expand Down
32 changes: 32 additions & 0 deletions test/action_controller/explicit_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ def render_array_using_implicit_serializer
render json: array,
each_serializer: ProfilePreviewSerializer
end

def render_array_using_explicit_serializer_and_custom_serializers
@post = Post.new(title: 'New Post', body: 'Body')
@author = Author.new(name: 'Jane Blogger')
@author.posts = [@post]
@post.author = @author
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@post.comments = [@first_comment, @second_comment]
@first_comment.post = @post
@first_comment.author = nil
@second_comment.post = @post
@second_comment.author = nil
@blog = Blog.new(id: 23, name: 'AMS Blog')
@post.blog = @blog

render json: [@post], each_serializer: PostPreviewSerializer
end
end

tests MyController
Expand Down Expand Up @@ -73,6 +91,20 @@ def test_render_array_using_explicit_serializer
assert_equal expected.to_json, @response.body
end

def test_render_array_using_explicit_serializer_and_custom_serializers
get :render_array_using_explicit_serializer_and_custom_serializers

expected = [
{ "title" => "New Post",
"body" => "Body",
"id" => assigns(:post).id,
"comments" => [{"id" => 1}, {"id" => 2}],
"author" => { "id" => assigns(:author).id }
}
]

assert_equal expected.to_json, @response.body
end
end
end
end

0 comments on commit b68d7f4

Please sign in to comment.