Skip to content

Commit

Permalink
Fix namespace lookup for collections and has_many
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Robert committed Nov 15, 2016
1 parent c9a96a0 commit f7db7e2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/active_model/serializer/collection_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def serializers_from_resources
end

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?
ActiveModelSerializers.logger.debug "No serializer found for resource: #{resource.inspect}"
Expand Down
56 changes: 54 additions & 2 deletions test/action_controller/namespace_lookup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Serialization
class NamespaceLookupTest < ActionController::TestCase
class Book < ::Model; end
class Page < ::Model; end
class Chapter < ::Model; end
class Writer < ::Model; end

module Api
Expand All @@ -19,6 +20,13 @@ class BookSerializer < ActiveModel::Serializer
attributes :title, :body

belongs_to :writer
has_many :chapters
end

class ChapterSerializer < ActiveModel::Serializer
attribute :title do
"Chapter - #{object.title}"
end
end

class WriterSerializer < ActiveModel::Serializer
Expand All @@ -32,7 +40,22 @@ class LookupTestController < ActionController::Base

def implicit_namespaced_serializer
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
end
Expand Down Expand Up @@ -84,7 +107,36 @@ def namespace_set_in_before_filter

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)

assert_equal expected, actual
Expand Down

0 comments on commit f7db7e2

Please sign in to comment.