mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
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:
@@ -37,6 +37,18 @@ module ActionController
|
||||
]
|
||||
render json: array
|
||||
end
|
||||
|
||||
def render_array_using_implicit_serializer_and_meta
|
||||
old_adapter = ActiveModel::Serializer.config.adapter
|
||||
|
||||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
||||
array = [
|
||||
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
]
|
||||
render json: array, meta: { total: 10 }
|
||||
ensure
|
||||
ActiveModel::Serializer.config.adapter = old_adapter
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
@@ -87,6 +99,13 @@ module ActionController
|
||||
|
||||
assert_equal expected.to_json, @response.body
|
||||
end
|
||||
|
||||
def test_render_array_using_implicit_serializer_and_meta
|
||||
get :render_array_using_implicit_serializer_and_meta
|
||||
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"profiles":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user