Fix namespace lookup for collections and has_many (#1973)

This commit is contained in:
Yohan Robert 2016-11-15 14:35:58 +01:00 committed by L. Preston Sego III
parent c9a96a05ed
commit d0de53cbb2
3 changed files with 58 additions and 3 deletions

View File

@ -6,6 +6,7 @@ Breaking changes:
Fixes: Fixes:
- [#1973](https://github.com/rails-api/active_model_serializers/pull/1973) Fix namespace lookup for collections and has_many relationships (@groyoh)
- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87) - [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj) - [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
- [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino) - [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino)

View File

@ -71,7 +71,9 @@ module ActiveModel
end end
def serializer_from_resource(resource, serializer_context_class, options) def serializer_from_resource(resource, serializer_context_class, options)
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) } serializer_class = options.fetch(:serializer) do
serializer_context_class.serializer_for(resource, namespace: options[:namespace])
end
if serializer_class.nil? if serializer_class.nil?
ActiveModelSerializers.logger.debug "No serializer found for resource: #{resource.inspect}" ActiveModelSerializers.logger.debug "No serializer found for resource: #{resource.inspect}"

View File

@ -5,6 +5,7 @@ module ActionController
class NamespaceLookupTest < ActionController::TestCase class NamespaceLookupTest < ActionController::TestCase
class Book < ::Model; end class Book < ::Model; end
class Page < ::Model; end class Page < ::Model; end
class Chapter < ::Model; end
class Writer < ::Model; end class Writer < ::Model; end
module Api module Api
@ -19,6 +20,13 @@ module ActionController
attributes :title, :body attributes :title, :body
belongs_to :writer belongs_to :writer
has_many :chapters
end
class ChapterSerializer < ActiveModel::Serializer
attribute :title do
"Chapter - #{object.title}"
end
end end
class WriterSerializer < ActiveModel::Serializer class WriterSerializer < ActiveModel::Serializer
@ -32,7 +40,22 @@ module ActionController
def implicit_namespaced_serializer def implicit_namespaced_serializer
writer = Writer.new(name: 'Bob') writer = Writer.new(name: 'Bob')
book = Book.new(title: 'New Post', body: 'Body', writer: writer) book = Book.new(title: 'New Post', body: 'Body', writer: writer, chapters: [])
render json: book
end
def implicit_namespaced_collection_serializer
chapter1 = Chapter.new(title: 'Oh')
chapter2 = Chapter.new(title: 'Oh my')
render json: [chapter1, chapter2]
end
def implicit_has_many_namespaced_serializer
chapter1 = Chapter.new(title: 'Odd World')
chapter2 = Chapter.new(title: 'New World')
book = Book.new(title: 'New Post', body: 'Body', chapters: [chapter1, chapter2])
render json: book render json: book
end end
@ -84,7 +107,36 @@ module ActionController
assert_serializer Api::V3::BookSerializer assert_serializer Api::V3::BookSerializer
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' } } expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' }, 'chapters' => [] }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'implicitly uses namespaced serializer for collection' do
get :implicit_namespaced_collection_serializer
assert_serializer 'ActiveModel::Serializer::CollectionSerializer'
expected = [{ 'title' => 'Chapter - Oh' }, { 'title' => 'Chapter - Oh my' }]
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'implicitly uses namespaced serializer for has_many' do
get :implicit_has_many_namespaced_serializer
assert_serializer Api::V3::BookSerializer
expected = {
'title' => 'New Post',
'body' => 'Body', 'writer' => nil,
'chapters' => [
{ 'title' => 'Chapter - Odd World' },
{ 'title' => 'Chapter - New World' }
]
}
actual = JSON.parse(@response.body) actual = JSON.parse(@response.body)
assert_equal expected, actual assert_equal expected, actual