This adds namespace lookup to serializer_for (#1968)

* This adds namespace lookup to serializer_for

* address rubocop issue

* address @bf4's feedback

* add docs

* update docs, add more tests

* apparently rails master doesn't have before filter

* try to address serializer cache issue between tests

* update cache for serializer lookup to include namespace in the key, and fix the tests for explicit namespace

* update docs, and use better cache key creation method

* update docs [ci skip]

* update docs [ci skip]

* add to changelog [ci skip]
This commit is contained in:
L. Preston Sego III
2016-11-09 07:57:39 -05:00
committed by GitHub
parent b709cd41e6
commit b29395b0ac
9 changed files with 295 additions and 7 deletions

View File

@@ -0,0 +1,149 @@
require 'test_helper'
module ActionController
module Serialization
class NamespaceLookupTest < ActionController::TestCase
class Book < ::Model; end
class Page < ::Model; end
class Writer < ::Model; end
module Api
module V2
class BookSerializer < ActiveModel::Serializer
attributes :title
end
end
module V3
class BookSerializer < ActiveModel::Serializer
attributes :title, :body
belongs_to :writer
end
class WriterSerializer < ActiveModel::Serializer
attributes :name
end
class LookupTestController < ActionController::Base
before_action only: [:namespace_set_in_before_filter] do
self.namespace_for_serializer = Api::V2
end
def implicit_namespaced_serializer
writer = Writer.new(name: 'Bob')
book = Book.new(title: 'New Post', body: 'Body', writer: writer)
render json: book
end
def explicit_namespace_as_module
book = Book.new(title: 'New Post', body: 'Body')
render json: book, namespace: Api::V2
end
def explicit_namespace_as_string
book = Book.new(title: 'New Post', body: 'Body')
# because this is a string, ruby can't auto-lookup the constant, so otherwise
# the looku things we mean ::Api::V2
render json: book, namespace: 'ActionController::Serialization::NamespaceLookupTest::Api::V2'
end
def explicit_namespace_as_symbol
book = Book.new(title: 'New Post', body: 'Body')
# because this is a string, ruby can't auto-lookup the constant, so otherwise
# the looku things we mean ::Api::V2
render json: book, namespace: :'ActionController::Serialization::NamespaceLookupTest::Api::V2'
end
def invalid_namespace
book = Book.new(title: 'New Post', body: 'Body')
render json: book, namespace: :api_v2
end
def namespace_set_in_before_filter
book = Book.new(title: 'New Post', body: 'Body')
render json: book
end
end
end
end
tests Api::V3::LookupTestController
setup do
@test_namespace = self.class.parent
end
test 'implicitly uses namespaced serializer' do
get :implicit_namespaced_serializer
assert_serializer Api::V3::BookSerializer
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' } }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'explicit namespace as module' do
get :explicit_namespace_as_module
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'explicit namespace as string' do
get :explicit_namespace_as_string
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'explicit namespace as symbol' do
get :explicit_namespace_as_symbol
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'invalid namespace' do
get :invalid_namespace
assert_serializer ActiveModel::Serializer::Null
expected = { 'title' => 'New Post', 'body' => 'Body' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'namespace set in before filter' do
get :namespace_set_in_before_filter
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
end
end
end

View File

@@ -0,0 +1,87 @@
require 'test_helper'
module ActiveModel
class Serializer
class SerializerForWithNamespaceTest < ActiveSupport::TestCase
class Book < ::Model; end
class Page < ::Model; end
class Publisher < ::Model; end
module Api
module V3
class BookSerializer < ActiveModel::Serializer
attributes :title, :author_name
has_many :pages
belongs_to :publisher
end
class PageSerializer < ActiveModel::Serializer
attributes :number, :text
belongs_to :book
end
class PublisherSerializer < ActiveModel::Serializer
attributes :name
end
end
end
class BookSerializer < ActiveModel::Serializer
attributes :title, :author_name
end
test 'resource without a namespace' do
book = Book.new(title: 'A Post', author_name: 'hello')
# TODO: this should be able to pull up this serializer without explicitly specifying the serializer
# currently, with no options, it still uses the Api::V3 serializer
result = ActiveModelSerializers::SerializableResource.new(book, serializer: BookSerializer).serializable_hash
expected = { title: 'A Post', author_name: 'hello' }
assert_equal expected, result
end
test 'resource with namespace' do
book = Book.new(title: 'A Post', author_name: 'hi')
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
expected = { title: 'A Post', author_name: 'hi', pages: nil, publisher: nil }
assert_equal expected, result
end
test 'has_many with nested serializer under the namespace' do
page = Page.new(number: 1, text: 'hello')
book = Book.new(title: 'A Post', author_name: 'hi', pages: [page])
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
expected = {
title: 'A Post', author_name: 'hi',
publisher: nil,
pages: [{
number: 1, text: 'hello'
}]
}
assert_equal expected, result
end
test 'belongs_to with nested serializer under the namespace' do
publisher = Publisher.new(name: 'Disney')
book = Book.new(title: 'A Post', author_name: 'hi', publisher: publisher)
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
expected = {
title: 'A Post', author_name: 'hi',
pages: nil,
publisher: {
name: 'Disney'
}
}
assert_equal expected, result
end
end
end
end