forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ActiveModel::Serializer#default_include class attribute to impl…
…ement rails-api#1333 Added test to cover new feature
- Loading branch information
Showing
2 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class DefaultIncludeTest < ActiveSupport::TestCase | ||
class Blog < ActiveModelSerializers::Model | ||
attr_accessor :id, :name, :posts | ||
end | ||
class Post < ActiveModelSerializers::Model | ||
attr_accessor :id, :title, :author | ||
end | ||
class Author < ActiveModelSerializers::Model | ||
attr_accessor :id, :name | ||
end | ||
class BlogSerializer < ActiveModel::Serializer | ||
attributes :id | ||
attribute :name, key: :title | ||
|
||
has_many :posts | ||
end | ||
class PostSerializer < ActiveModel::Serializer | ||
default_include [:author] | ||
attributes :id, :title | ||
|
||
has_one :author | ||
end | ||
class AuthorSerializer < ActiveModel::Serializer | ||
attributes :id, :name | ||
end | ||
|
||
setup do | ||
@authors = [Author.new(id: 1, name: 'Blog Author')] | ||
@posts = [Post.new(id: 1, title: 'The first post', author: @authors.first), Post.new(id: 2, title: 'The second post', author: @authors.first)] | ||
@blog = Blog.new(id: 2, name: 'The Blog', posts: @posts) | ||
@serializer_instance = BlogSerializer.new(@blog) | ||
@serializable = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes) | ||
@expected_hash = { | ||
id: 2, | ||
title: 'The Blog', | ||
posts: [ | ||
{ id: 1, title: 'The first post', author: { id: 1, name: 'Blog Author'} }, | ||
{ id: 2, title: 'The second post', author: { id: 1, name: 'Blog Author'} } | ||
] } | ||
end | ||
|
||
test '#default_include allows to include associations in the result of #serializable_hash' do | ||
assert_equal @serializable.serializable_hash, @serializer_instance.serializable_hash | ||
assert_equal @expected_hash, @serializer_instance.serializable_hash | ||
end | ||
end | ||
end | ||
end |