mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Make serializer lookup configurable (#1757)
This commit is contained in:
committed by
Yohan Robert
parent
d0de53cbb2
commit
d31d741f43
49
test/action_controller/lookup_proc_test.rb
Normal file
49
test/action_controller/lookup_proc_test.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
class LookupProcTest < ActionController::TestCase
|
||||
module Api
|
||||
module V3
|
||||
class PostCustomSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
|
||||
belongs_to :author
|
||||
end
|
||||
|
||||
class AuthorCustomSerializer < ActiveModel::Serializer
|
||||
attributes :name
|
||||
end
|
||||
|
||||
class LookupProcTestController < ActionController::Base
|
||||
def implicit_namespaced_serializer
|
||||
author = Author.new(name: 'Bob')
|
||||
post = Post.new(title: 'New Post', body: 'Body', author: author)
|
||||
|
||||
render json: post
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
tests Api::V3::LookupProcTestController
|
||||
|
||||
test 'implicitly uses namespaced serializer' do
|
||||
controller_namespace = lambda do |resource_class, _parent_serializer_class, namespace|
|
||||
"#{namespace}::#{resource_class}CustomSerializer" if namespace
|
||||
end
|
||||
|
||||
with_prepended_lookup(controller_namespace) do
|
||||
get :implicit_namespaced_serializer
|
||||
|
||||
assert_serializer Api::V3::PostCustomSerializer
|
||||
|
||||
expected = { 'title' => 'New Post', 'body' => 'Body', 'author' => { 'name' => 'Bob' } }
|
||||
actual = JSON.parse(@response.body)
|
||||
|
||||
assert_equal expected, actual
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -15,6 +15,16 @@ module ActionController
|
||||
end
|
||||
end
|
||||
|
||||
module VHeader
|
||||
class BookSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
|
||||
def body
|
||||
'header'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module V3
|
||||
class BookSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
@@ -92,6 +102,14 @@ module ActionController
|
||||
book = Book.new(title: 'New Post', body: 'Body')
|
||||
render json: book
|
||||
end
|
||||
|
||||
def namespace_set_by_request_headers
|
||||
book = Book.new(title: 'New Post', body: 'Body')
|
||||
version_from_header = request.headers['X-API_VERSION']
|
||||
namespace = "ActionController::Serialization::NamespaceLookupTest::#{version_from_header}"
|
||||
|
||||
render json: book, namespace: namespace
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -102,6 +120,13 @@ module ActionController
|
||||
@test_namespace = self.class.parent
|
||||
end
|
||||
|
||||
test 'uses request headers to determine the namespace' do
|
||||
request.env['X-API_VERSION'] = 'Api::VHeader'
|
||||
get :namespace_set_by_request_headers
|
||||
|
||||
assert_serializer Api::VHeader::BookSerializer
|
||||
end
|
||||
|
||||
test 'implicitly uses namespaced serializer' do
|
||||
get :implicit_namespaced_serializer
|
||||
|
||||
|
||||
Reference in New Issue
Block a user