Skip to content

Commit

Permalink
Merge b6de5c3 into 2e31a14
Browse files Browse the repository at this point in the history
  • Loading branch information
undr committed Jun 22, 2014
2 parents 2e31a14 + b6de5c3 commit 2f73775
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,6 @@
# VERSION 0.9.0.pre

* The following methods were removed
- Model#active\_model\_serializer
- Serializer#include!
- Serializer#include?
- Serializer#attr\_disabled=
Expand Down
31 changes: 21 additions & 10 deletions README.md
@@ -1,5 +1,5 @@
[![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)
[![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)

# ActiveModel::Serializers
Expand All @@ -14,8 +14,8 @@ If you want to read the stable documentation visit [0.8 README](https://github.c

## Purpose

`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
Objects that respond to read\_attribute\_for\_serialization
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
Objects that respond to read\_attribute\_for\_serialization
(including `ActiveModel` and `ActiveRecord` objects) are supported.

Serializers know about both a model and the `current_user`, so you can
Expand Down Expand Up @@ -71,7 +71,7 @@ $ rails g serializer post

Currently `ActiveModel::Serializers` expects objects to implement
read\_attribute\_for\_serialization. That's all you need to do to have
your POROs supported.
your POROs supported.

# render :json

Expand All @@ -94,11 +94,22 @@ This also works with `respond_with`, which uses `to_json` under the hood. Also
note that any options passed to `render :json` will be passed to your
serializer and available as `@options` inside.

To specify a custom serializer for an object, you can specify the
serializer when you render the object:
To specify a custom serializer for an object, there are 2 options:

#### 1. Specify the serializer in your model:

```ruby
class Post < ActiveRecord::Base
def active_model_serializer
FancyPostSerializer
end
end
```

#### 2. Specify the serializer when you render the object:

```ruby
render json: @post, serializer: FancyPostSerializer
render :json => @post, :serializer => FancyPostSerializer
```

## Arrays
Expand Down Expand Up @@ -573,8 +584,8 @@ this:
}
```

When side-loading data, your serializer cannot have the `{ root: false }` option,
as this would lead to invalid JSON. If you do not have a root key, the `include`
When side-loading data, your serializer cannot have the `{ root: false }` option,
as this would lead to invalid JSON. If you do not have a root key, the `include`
instruction will be ignored

You can also specify a different root for the embedded objects than the key
Expand Down
2 changes: 2 additions & 0 deletions lib/active_model/serializer.rb
Expand Up @@ -44,6 +44,7 @@ def serializer_for(resource)
ArraySerializer
else
begin
return resource.active_model_serializer if resource.respond_to?(:active_model_serializer)
Object.const_get "#{resource.class.name}Serializer"
rescue NameError
nil
Expand All @@ -55,6 +56,7 @@ def serializer_for(resource)
if resource.respond_to?(:to_ary)
ArraySerializer
else
return resource.active_model_serializer if resource.respond_to?(:active_model_serializer)
"#{resource.class.name}Serializer".safe_constantize
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/poro.rb
Expand Up @@ -35,6 +35,12 @@ def comments
class Comment < Model
end

class Author < Model
def active_model_serializer
UserSerializer
end
end

###
## Serializers
###
Expand Down
20 changes: 20 additions & 0 deletions test/unit/active_model/serializer/serializer_test.rb
@@ -0,0 +1,20 @@
require 'test_helper'

module ActiveModel
class Serializer
class SerializerForTest < Minitest::Test
def setup
@user = User.new({ name: 'User 1', email: 'email1@server.com' })
@author = Author.new({ name: 'User 1', email: 'email1@server.com' })
end

def test_without_custom_serializer
assert_equal(UserSerializer, ActiveModel::Serializer.serializer_for(@user))
end

def test_with_custom_serializer
assert_equal(UserSerializer, ActiveModel::Serializer.serializer_for(@author))
end
end
end
end

0 comments on commit 2f73775

Please sign in to comment.