From 55042937765258aeadac693752ea8534ec15bf7b Mon Sep 17 00:00:00 2001 From: David Verhasselt Date: Mon, 13 May 2013 18:23:20 +0300 Subject: [PATCH 1/3] Update README.md I'm a bit embarrassed to admit that it took me a while to figure out this was a possibility, so I figured maybe this change will save some other poor chaps their time and/or pride. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 7e92976b..ce244b4c 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,18 @@ def default_serializer_options end ``` +## Disabling root globally + +If you want to disable the ```root```-node for all serializers, not just when +they are used in ```respond_with```, you can set the ```root``` option for the +main class in an initializer just like with Arrays: + +```ruby +ActiveSupport.on_load(:active_model_serializers) do + ActiveModel::Serializer.root = false +end +``` + ## Getting the old version If you find that your project is already relying on the old rails to_json From 358b6a88756e07e1fbf324e92fe9c40a72579ff9 Mon Sep 17 00:00:00 2001 From: David Verhasselt Date: Tue, 14 May 2013 10:42:28 +0300 Subject: [PATCH 2/3] Update README.md --- README.md | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index ce244b4c..d870125d 100644 --- a/README.md +++ b/README.md @@ -141,10 +141,14 @@ render :json => @posts, :root => "some_posts" You may disable the root element for arrays at the top level, which will result in more concise json. To disable the root element for arrays, you have 4 options: -#### 1. Disable root globally for in `ArraySerializer`. In an initializer: +#### 1. Disable root globally for `ArraySerializer`. In an initializer: ```ruby ActiveSupport.on_load(:active_model_serializers) do + # Disabled for all serializers + # ActiveModel::Serializer.root = false + + # Or just for the ArraySerializer ActiveModel::ArraySerializer.root = false end ``` @@ -195,18 +199,6 @@ def default_serializer_options end ``` -## Disabling root globally - -If you want to disable the ```root```-node for all serializers, not just when -they are used in ```respond_with```, you can set the ```root``` option for the -main class in an initializer just like with Arrays: - -```ruby -ActiveSupport.on_load(:active_model_serializers) do - ActiveModel::Serializer.root = false -end -``` - ## Getting the old version If you find that your project is already relying on the old rails to_json From 1614ef3b12975ddf2c428ae4007f0bd930e9c8aa Mon Sep 17 00:00:00 2001 From: David Verhasselt Date: Tue, 14 May 2013 11:30:22 +0300 Subject: [PATCH 3/3] Create new section 'Disabling root' in README --- README.md | 71 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d870125d..c1325b88 100644 --- a/README.md +++ b/README.md @@ -139,39 +139,8 @@ render :json => @posts, :root => "some_posts" ``` You may disable the root element for arrays at the top level, which will result in -more concise json. To disable the root element for arrays, you have 4 options: - -#### 1. Disable root globally for `ArraySerializer`. In an initializer: - -```ruby -ActiveSupport.on_load(:active_model_serializers) do - # Disabled for all serializers - # ActiveModel::Serializer.root = false - - # Or just for the ArraySerializer - ActiveModel::ArraySerializer.root = false -end -``` - -#### 2. Disable root per render call in your controller: - -```ruby -render :json => @posts, :root => false -``` - -#### 3. Create a custom `ArraySerializer` and render arrays with it: - -```ruby -class CustomArraySerializer < ActiveModel::ArraySerializer - self.root = false -end - -# controller: -render :json => @posts, :serializer => CustomArraySerializer -``` - -Disabling the root element of the array with any of the above 3 methods -will produce +more concise json. See the next section for ways on how to do this. Disabling the +root element of the array with any of those methods will produce ```json [ @@ -185,6 +154,42 @@ To specify a custom serializer for the items within an array: ```ruby render :json => @posts, :each_serializer => FancyPostSerializer ``` + +## Disabling the root element + +You have 4 options to disable the root element, each with a slightly different scope: + +#### 1. Disable root globally for all, or per class + +In an initializer: + +```ruby +ActiveSupport.on_load(:active_model_serializers) do + # Disabled for all serializers + ActiveModel::Serializer.root = false + + # Or just for the ArraySerializer + # ActiveModel::ArraySerializer.root = false +end +``` + +#### 2. Disable root per render call in your controller + +```ruby +render :json => @posts, :root => false +``` + +#### 3. Subclass the serializer, and specify using it + +```ruby +class CustomArraySerializer < ActiveModel::ArraySerializer + self.root = false +end + +# controller: +render :json => @posts, :serializer => CustomArraySerializer +``` + #### 4. Define default_serializer_options in your controller If you define `default_serializer_options` method in your controller,