Adds support for meta attribute

Currently, 0.10.0.pre doesn't support `meta` option in `render`. This
way, there's no way to support features such as pagination. `0.9` had
this feature in place.

This adds support for it, as well as fixes small things in README.md.

This won't support `meta` in array responses because arrays don't have
keys, obviously. Also, the response should have a `root` key, otherwise
no `meta` will be included.

In some cases, for example using JsonApi, ArraySerializer will result in
a response with a `root`. In that case, `meta` will be included.
This commit is contained in:
Alexandre de Oliveira
2015-01-05 01:00:03 -02:00
parent 282de21984
commit bd27da1b76
7 changed files with 170 additions and 15 deletions

View File

@@ -0,0 +1,77 @@
require 'test_helper'
module ActiveModel
class Serializer
class MetaTest < Minitest::Test
def setup
@blog = Blog.new(id: 1,
name: 'AMS Hints',
writer: Author.new(id: 2, name: "Steve"),
articles: [Post.new(id: 3, title: "AMS")])
end
def test_meta_is_present_with_root
adapter = load_adapter(root: "blog", meta: {total: 10})
expected = {
"blog" => {
id: 1,
title: "AMS Hints"
},
"meta" => {
total: 10
}
}
assert_equal expected, adapter.as_json
end
def test_meta_is_not_included_when_root_is_missing
adapter = load_adapter(meta: {total: 10})
expected = {
id: 1,
title: "AMS Hints"
}
assert_equal expected, adapter.as_json
end
def test_meta_key_is_used
adapter = load_adapter(root: "blog", meta: {total: 10}, meta_key: "haha_meta")
expected = {
"blog" => {
id: 1,
title: "AMS Hints"
},
"haha_meta" => {
total: 10
}
}
assert_equal expected, adapter.as_json
end
def test_meta_is_not_used_on_arrays
serializer = ArraySerializer.new([@blog], root: "blog", meta: {total: 10}, meta_key: "haha_meta")
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
expected = [{
id: 1,
name: "AMS Hints",
writer: {
id: 2,
name: "Steve"
},
articles: [{
id: 3,
title: "AMS",
body: nil
}]
}]
assert_equal expected, adapter.as_json
end
private
def load_adapter(options)
serializer = AlternateBlogSerializer.new(@blog, options)
ActiveModel::Serializer::Adapter::Json.new(serializer)
end
end
end
end