From 0994f3dda841742f4d725747bbd1a57014115617 Mon Sep 17 00:00:00 2001 From: Enio Lopes Date: Fri, 11 Jan 2013 00:18:14 -0200 Subject: [PATCH 1/3] Adds documentation for usage of meta and meta_key options --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 8c8e902b..8c913c5d 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,46 @@ class PostSerializer < ActiveModel::Serializer end ``` +If you would like to add meta information to the outputted JSON, use the `:meta` +option: + +``` +render :json => @posts, :serializer => CustomArraySerializer, +:meta => {:total => 10} +``` + +The above usage of `:meta` will produce the following: + +``` +{ + meta: { total: 10 + }, + posts: [ + { "title": "Post 1", "body": "Hello!" }, + { "title": "Post 2", "body": "Goodbye!" } + ] +} +``` + +If you would like to to change the attribute name you can use the `meta_key` option: + +``` +render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10}, :meta_key => 'meta_object' +``` + +The above usage of `:meta_key` will produce the following: + +``` +{ + meta_object: { total: 10 + }, + posts: [ + { "title": "Post 1", "body": "Hello!" }, + { "title": "Post 2", "body": "Goodbye!" } + ] +} +``` + If you would like direct, low-level control of attribute serialization, you can completely override the `attributes` method to return the hash you need: From 244021d65cfa07155550dfdcc67070de9aa4847c Mon Sep 17 00:00:00 2001 From: Enio Lopes Date: Fri, 11 Jan 2013 11:06:26 -0200 Subject: [PATCH 2/3] Corrects some markdown properties, identation and text --- README.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8c913c5d..bd21a9b5 100644 --- a/README.md +++ b/README.md @@ -256,37 +256,34 @@ end If you would like to add meta information to the outputted JSON, use the `:meta` option: -``` -render :json => @posts, :serializer => CustomArraySerializer, -:meta => {:total => 10} +```ruby +render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10} ``` The above usage of `:meta` will produce the following: -``` +```json { - meta: { total: 10 - }, - posts: [ + "meta": { "total": 10 }, + "posts": [ { "title": "Post 1", "body": "Hello!" }, { "title": "Post 2", "body": "Goodbye!" } ] } ``` -If you would like to to change the attribute name you can use the `meta_key` option: +If you would like to change the meta key name you can use the `meta_key` option: -``` +```ruby render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10}, :meta_key => 'meta_object' ``` The above usage of `:meta_key` will produce the following: -``` +```json { - meta_object: { total: 10 - }, - posts: [ + "meta_object": { "total": 10 }, + "posts": [ { "title": "Post 1", "body": "Hello!" }, { "title": "Post 2", "body": "Goodbye!" } ] From 97e7ec1d32eaddc92b72582fec6632aa39b83b5c Mon Sep 17 00:00:00 2001 From: Enio Lopes Date: Fri, 11 Jan 2013 11:22:45 -0200 Subject: [PATCH 3/3] Uses colon on :meta_key --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bd21a9b5..76fe1831 100644 --- a/README.md +++ b/README.md @@ -272,7 +272,7 @@ The above usage of `:meta` will produce the following: } ``` -If you would like to change the meta key name you can use the `meta_key` option: +If you would like to change the meta key name you can use the `:meta_key` option: ```ruby render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10}, :meta_key => 'meta_object'