README.md in active_model_serializers-0.8.1 vs README.md in active_model_serializers-0.8.2

- old
+ new

@@ -1,6 +1,6 @@ -[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png)](https://travis-ci.org/rails-api/active_model_serializers) [![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers) [![Coverage Status](https://coveralls.io/repos/rails-api/active_model_serializers/badge.png?branch=master)](https://coveralls.io/r/rails-api/active_model_serializers) +[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png)](https://travis-ci.org/rails-api/active_model_serializers) [![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers) # Purpose The purpose of `ActiveModel::Serializers` is to provide an object to encapsulate serialization of `ActiveModel` objects, including `ActiveRecord` @@ -17,11 +17,11 @@ The easiest way to install `ActiveModel::Serializers` is to add it to your `Gemfile`: ```ruby -gem "active_model_serializers", "~> 0.7.0" +gem "active_model_serializers", "~> 0.8.0" ``` Then, install it on the command line: ``` @@ -52,11 +52,11 @@ using another ORM, or if you are using objects that are `ActiveModel` compliant but do not descend from `ActiveRecord` or include `Mongoid::Document`, you must add an include statement for `ActiveModel::SerializerSupport` to make models serializable. If you also want to make collections serializable, you should include -`ActiveModel::ArraySerializationSupport` into your ORM's +`ActiveModel::ArraySerializerSupport` into your ORM's relation/criteria class. # ActiveModel::Serializer All new serializers descend from ActiveModel::Serializer @@ -137,51 +137,60 @@ ```ruby 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: +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 -#### 1. Disable root globally for in `ArraySerializer`. In an initializer: +```json +[ + { "title": "Post 1", "body": "Hello!" }, + { "title": "Post 2", "body": "Goodbye!" } +] +``` +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 + # Disable for all serializers (except ArraySerializer) + ActiveModel::Serializer.root = false + + # Disable for ArraySerializer ActiveModel::ArraySerializer.root = false end ``` -#### 2. Disable root per render call in your controller: +#### 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: +#### 3. Subclass the serializer, and specify using 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 - -```json -[ - { "title": "Post 1", "body": "Hello!" }, - { "title": "Post 2", "body": "Goodbye!" } -] -``` - -To specify a custom serializer for the items within an array: - -```ruby -render :json => @posts, :each_serializer => FancyPostSerializer -``` #### 4. Define default_serializer_options in your controller If you define `default_serializer_options` method in your controller, all serializers in actions of this controller and it's children will use them. One of the options may be `root: false`