Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support serializer and each_serializer options in renderer #703

Merged
merged 3 commits into from
Nov 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ member when the resource names are included in the `include` option.
render @posts, include: 'authors,comments'
```

### Specify a serializer

If you wish to use a serializer other than the default, you can explicitly pass it to the renderer.

#### 1. For a resource:

```ruby
render json: @post, serializer: PostPreviewSerializer
```

#### 2. For an array resource:

```ruby
# Use the default `ArraySerializer`, which will use `each_serializer` to
# serialize each element
render json: @posts, each_serializer: PostPreviewSerializer

# Or, you can explicitly provide the collection serializer as well
render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
```

## Installation

Add this line to your application's Gemfile:
Expand Down
23 changes: 17 additions & 6 deletions lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ module Serialization

ADAPTER_OPTION_KEYS = [:include, :root]

def get_serializer(resource, options)
@_serializer ||= options.delete(:serializer)
@_serializer ||= ActiveModel::Serializer.serializer_for(resource)

if options.key?(:each_serializer)
options[:serializer] = options.delete(:each_serializer)
end

@_serializer
end

[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
define_method renderer_method do |resource, options|
serializer = ActiveModel::Serializer.serializer_for(resource)

if serializer
adapter_opts, serializer_opts =
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }
adapter_opts, serializer_opts =
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }

if (serializer = get_serializer(resource, serializer_opts))
# omg hax
object = serializer.new(resource, Hash[serializer_opts])
adapter = ActiveModel::Serializer.adapter.new(object, Hash[adapter_opts])
object = serializer.new(resource, serializer_opts)
adapter = ActiveModel::Serializer.adapter.new(object, adapter_opts)
super(adapter, options)
else
super(resource, options)
Expand Down
5 changes: 4 additions & 1 deletion lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class ArraySerializer

def initialize(objects, options = {})
@objects = objects.map do |object|
serializer_class = ActiveModel::Serializer.serializer_for(object)
serializer_class = options.fetch(
:serializer,
ActiveModel::Serializer.serializer_for(object)
)
serializer_class.new(object)
end
end
Expand Down
78 changes: 78 additions & 0 deletions test/action_controller/explicit_serializer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'test_helper'

module ActionController
module Serialization
class ExplicitSerializerTest < ActionController::TestCase
class MyController < ActionController::Base
def render_using_explicit_serializer
@profile = Profile.new(name: 'Name 1',
description: 'Description 1',
comments: 'Comments 1')
render json: @profile, serializer: ProfilePreviewSerializer
end

def render_array_using_explicit_serializer
array = [
Profile.new(name: 'Name 1',
description: 'Description 1',
comments: 'Comments 1'),
Profile.new(name: 'Name 2',
description: 'Description 2',
comments: 'Comments 2')
]
render json: array,
serializer: PaginatedSerializer,
each_serializer: ProfilePreviewSerializer
end

def render_array_using_implicit_serializer
array = [
Profile.new(name: 'Name 1',
description: 'Description 1',
comments: 'Comments 1'),
Profile.new(name: 'Name 2',
description: 'Description 2',
comments: 'Comments 2')
]
render json: array,
each_serializer: ProfilePreviewSerializer
end
end

tests MyController

def test_render_using_explicit_serializer
get :render_using_explicit_serializer

assert_equal 'application/json', @response.content_type
assert_equal '{"name":"Name 1"}', @response.body
end

def test_render_array_using_explicit_serializer
get :render_array_using_explicit_serializer
assert_equal 'application/json', @response.content_type

expected = {
'paginated' => [
{ 'name' => 'Name 1' },
{ 'name' => 'Name 2' }
]
}

assert_equal expected.to_json, @response.body
end

def test_render_array_using_explicit_serializer
get :render_array_using_implicit_serializer
assert_equal 'application/json', @response.content_type

expected = [
{ 'name' => 'Name 1' },
{ 'name' => 'Name 2' }
]
assert_equal expected.to_json, @response.body
end

end
end
end
12 changes: 12 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class ProfileSerializer < ActiveModel::Serializer
urls :posts, :comments
end

class ProfilePreviewSerializer < ActiveModel::Serializer
attributes :name

urls :posts, :comments
end

Post = Class.new(Model)
Comment = Class.new(Model)
Author = Class.new(Model)
Expand Down Expand Up @@ -67,3 +73,9 @@ class ProfileSerializer < ActiveModel::Serializer
belongs_to :writer
has_many :articles
end

PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
def json_key
'paginated'
end
end