Skip to content

added option to include nested associations #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ def serializable_hash(options = {})
array_serializer = association
@hash[name] = array_serializer.map do |item|
cache_check(item) do
item.attributes(opts)
opts[:include_nested_associations] ?
self.class.new(item).serializable_hash :
item.attributes(opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a good time to extract add_association to its own method, like the json_api adapter has

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end
end
else
if association && association.object
@hash[name] = cache_check(association) do
association.attributes(options)
opts[:include_nested_associations] ?
self.class.new(association).serializable_hash :
association.attributes(options)
end
elsif opts[:virtual_value]
@hash[name] = opts[:virtual_value]
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ def custom_options
end
end

SimpleAuthorSerializer = Class.new(ActiveModel::Serializer) do
attributes :name

has_one :bio
end

PostSerializerWithNestedAssociations = Class.new(ActiveModel::Serializer) do
attributes :id, :title, :body

has_many :comments
has_one :author, serializer: SimpleAuthorSerializer, include_nested_associations: true
end

SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
has_many :related
Expand Down
36 changes: 36 additions & 0 deletions test/serializers/include_nested_associations_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'test_helper'

module ActiveModel
class Serializer
class IncludeNestedAssociationsTest < Minitest::Test
def setup
@author = Author.new(name: 'Steve K.')
@author.bio = Bio.new(id: 1, content: 'Hello!', rating: 5)

@post = Post.new({ title: 'New Post', body: 'Body' })

@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
@post.comments = [@comment]

@post.author = @author
end

def test_no_nested_association_are_included_by_default
@post_serializer = PostSerializer.new(@post)
json = ActiveModel::Serializer::Adapter::Json.new(@post_serializer).as_json
assert_nil json[:author][:bio]
end

def test_no_option_is_passed_in
@post_serializer = PostSerializerWithNestedAssociations.new(@post)
json = ActiveModel::Serializer::Adapter::Json.new(@post_serializer).as_json
expected_return = {
id: 1,
content: 'Hello!',
rating: 5
}
assert_equal json[:author][:bio] , expected_return
end
end
end
end