Handle special-case of Array serializer with unserializable elements

This commit is contained in:
Benjamin Fleischer
2015-06-22 15:26:19 -05:00
committed by João Moura
parent cf77786da2
commit e5d1e40dbd
3 changed files with 8 additions and 6 deletions

View File

@@ -211,7 +211,7 @@ module ActiveModel
association_value,
options.except(:serializer).merge(serializer_from_options(association_options))
)
rescue NoMethodError
rescue ActiveModel::Serializer::ArraySerializer::Error
# 1. Failure to serialize an element in a collection, e.g. [ {hi: "Steve" } ] will fail
# with NoMethodError when the ArraySerializer finds no serializer for the hash { hi: "Steve" },
# and tries to call new on that nil.

View File

@@ -1,6 +1,7 @@
module ActiveModel
class Serializer
class ArraySerializer
Error = Class.new(StandardError)
include Enumerable
delegate :each, to: :@objects
@@ -14,7 +15,9 @@ module ActiveModel
ActiveModel::Serializer.serializer_for(object)
)
unless serializer_class.nil?
if serializer_class.nil?
fail Error, "No serializer found for object: #{object.inspect}"
else
serializer_class.new(object, options.except(:serializer))
end
end