mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge pull request #703 from ggordon/specify_serializer_from_controller
Support serializer and each_serializer options in renderer
This commit is contained in:
78
test/action_controller/explicit_serializer_test.rb
Normal file
78
test/action_controller/explicit_serializer_test.rb
Normal file
@@ -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
test/fixtures/poro.rb
vendored
12
test/fixtures/poro.rb
vendored
@@ -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)
|
||||
@@ -83,3 +89,9 @@ BlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||
belongs_to :writer
|
||||
has_many :articles
|
||||
end
|
||||
|
||||
PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
|
||||
def json_key
|
||||
'paginated'
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user