Skip to content
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
2 changes: 1 addition & 1 deletion lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def find_serializable(object)
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
ams.new(object, serializer_options)
else
object
DefaultSerializer.new(object, serializer_options)
end
end

Expand Down
36 changes: 36 additions & 0 deletions test/serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,42 @@ def test_embed_ids_include_true_with_root
}, serializer.as_json)
end

def test_embed_ids_include_true_without_explicit_serializer
serializer_class = post_serializer

serializer_class.class_eval do
root :post
embed :ids, include: true
has_many :comments
end

comment_class = CommentWithoutExplicitSerializer

post = Post.new(title: "Fix ALL the bugs", body: "Body of new post", email: "arne@arnebrasseur.net")
comments = [comment_class.new(title: "Comment1", id: 1)]
post.comments = comments

serializer = serializer_class.new(post)

assert_equal({
post: {
title: "Fix ALL the bugs",
body: "Body of new post",
comment_ids: [1],
author_id: nil
},
authors: [],
comments: [
{
id: 1,
title: "Comment1",
default_serialization_provided_by_model: true
}
],
}, serializer.as_json)

end

# the point of this test is to illustrate that deeply nested serializers
# still side-load at the root.
def test_embed_with_include_inserts_at_root
Expand Down
8 changes: 8 additions & 0 deletions test/test_fakes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class Comment < Model
def active_model_serializer; CommentSerializer; end
end

class CommentWithoutExplicitSerializer < Model
def as_json(*)
@attributes.slice(:title, :id).merge(
default_serialization_provided_by_model: true
)
end
end

class UserSerializer < ActiveModel::Serializer
attributes :first_name, :last_name

Expand Down