Merge pull request #617 from konukhov/namespaced_serializers-0-9

Namespaced serializers #499
This commit is contained in:
Steve Klabnik
2014-08-31 20:00:47 -04:00
12 changed files with 163 additions and 35 deletions

View File

@@ -135,3 +135,10 @@ end
class VideoSerializer < ActiveModel::Serializer
attributes :html
end
class ShortProfileSerializer < ::ProfileSerializer; end
module TestNamespace
class ProfileSerializer < ::ProfileSerializer; end
class UserSerializer < ::UserSerializer; end
end

View File

@@ -0,0 +1,47 @@
require 'test_helper'
module ActionController
module Serialization
class NamespacedSerializationTest < ActionController::TestCase
class TestNamespace::MyController < ActionController::Base
def render_profile_with_namespace
render json: Profile.new({ name: 'Name 1', description: 'Description 1'})
end
def render_profiles_with_namespace
render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})]
end
def render_comment
render json: Comment.new(content: 'Comment 1')
end
def render_comments
render json: [Comment.new(content: 'Comment 1')]
end
end
tests TestNamespace::MyController
def test_render_profile_with_namespace
get :render_profile_with_namespace
assert_serializer TestNamespace::ProfileSerializer
end
def test_render_profiles_with_namespace
get :render_profiles_with_namespace
assert_serializer TestNamespace::ProfileSerializer
end
def test_fallback_to_a_version_without_namespace
get :render_comment
assert_serializer CommentSerializer
end
def test_array_fallback_to_a_version_without_namespace
get :render_comments
assert_serializer CommentSerializer
end
end
end
end

View File

@@ -7,6 +7,7 @@ module ActiveModel
def setup
@association = Association::HasOne.new('post', serializer: PostSerializer)
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
@user = User.new
end
def test_build_serializer_for_array_called_twice
@@ -15,6 +16,20 @@ module ActiveModel
assert_instance_of(PostSerializer, serializer)
end
end
def test_build_serializer_from_in_a_namespace
assoc = Association::HasOne.new('profile')
serializer = TestNamespace::UserSerializer.new(@user).build_serializer(assoc)
assert_instance_of(TestNamespace::ProfileSerializer, serializer)
end
def test_build_serializer_with_prefix
assoc = Association::HasOne.new('profile', prefix: :short)
serializer = UserSerializer.new(@user).build_serializer(assoc)
assert_instance_of(ShortProfileSerializer, serializer)
end
end
end
end