-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Create NestedJson adapter. #1073
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class NestedJsonTest < Minitest::Test | ||
def setup | ||
ActionController::Base.cache_store.clear | ||
@author = Author.new(id: 1, name: 'Steve K.') | ||
@post = Post.new(id: 1, title: 'New Post', body: 'Body') | ||
@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] | ||
@author.posts = [@post] | ||
|
||
@serializer = AuthorNestedSerializer.new(@author) | ||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) | ||
end | ||
|
||
def test_has_many | ||
assert_equal({ | ||
id: 1, name: 'Steve K.', | ||
posts: [{ | ||
id: 1, title: 'New Post', body: 'Body', | ||
comments: [ | ||
{id: 1, body: 'ZOMG A COMMENT'}, | ||
{id: 2, body: 'ZOMG ANOTHER COMMENT'} | ||
] | ||
}] | ||
}, @adapter.serializable_hash(limit_depth: 5)[:author]) | ||
end | ||
|
||
def test_limit_depth | ||
assert_raises(StandardError) do | ||
@adapter.serializable_hash(limit_depth: 1, check_depth_strategy: :fail) | ||
end | ||
end | ||
|
||
def test_trim_strategy | ||
assert_equal({ | ||
id: 1, name: 'Steve K.', | ||
posts: [{ | ||
id: 1, title: 'New Post', body: 'Body', | ||
}] | ||
}, @adapter.serializable_hash(limit_depth: 1, check_depth_strategy: :trim)[:author]) | ||
end | ||
|
||
def test_pass_strategy | ||
assert_equal({ | ||
id: 1, name: 'Steve K.', | ||
posts: [{ | ||
id: 1, title: 'New Post', body: 'Body', | ||
comments: [ | ||
{id: 1, body: 'ZOMG A COMMENT'}, | ||
{id: 2, body: 'ZOMG ANOTHER COMMENT'} | ||
] | ||
}] | ||
}, @adapter.serializable_hash(limit_depth: 1, check_depth_strategy: :pass)[:author]) | ||
end | ||
|
||
def test_flatten_json | ||
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(@serializer) | ||
assert_equal({ | ||
id: 1, name: 'Steve K.', | ||
posts: [{ | ||
id: 1, title: 'New Post', body: 'Body', | ||
comments: [ | ||
{id: 1, body: 'ZOMG A COMMENT'}, | ||
{id: 2, body: 'ZOMG ANOTHER COMMENT'} | ||
] | ||
}] | ||
}, adapter.serializable_hash(limit_depth: 5)) | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,3 +266,18 @@ def json_key | |
raise StandardError, 'Intentional error for rescue_from test' | ||
end | ||
end | ||
|
||
CommentNestedSerializer = Class.new(ActiveModel::Serializer) do | ||
attributes :body, :id | ||
end | ||
|
||
PostNestedSerializer = Class.new(ActiveModel::Serializer) do | ||
attributes :title, :body, :id | ||
has_many :comments, serializer: CommentNestedSerializer | ||
end | ||
|
||
AuthorNestedSerializer = Class.new(ActiveModel::Serializer) do | ||
attributes :id, :name | ||
has_many :posts, serializer: PostNestedSerializer | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joaomdmoura I think we should start following some convention for associating poros with tests. I think it's becoming append-only... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a suggestion. class AuthorSerializer < ActiveModel::Serializer
attributes :id, :name
class ForShow < self
has_many :posts
end
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
class ForShow < self
has_many :comments
end
end and so on. I define default serializers not to have any relations. When I use them in a controller, I write like: def show
...
render json: @post, serializer: PostSerializer::ForShow
end Like this way, I can easily avoid unexpected circular relations because default serializers has no associations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. works for me |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!