Renaming Error to NoSerializerError

This commit is contained in:
João Moura 2015-06-22 17:53:46 -03:00
parent e5d1e40dbd
commit d3649d5b4e
3 changed files with 7 additions and 13 deletions

View File

@ -43,10 +43,10 @@ module ActionController
@_serializer_opts[:scope_name] = _serialization_scope @_serializer_opts[:scope_name] = _serialization_scope
begin begin
object = serializer.new(resource, @_serializer_opts) serialized = serializer.new(resource, @_serializer_opts)
rescue ActiveModel::Serializer::ArraySerializer::Error rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
else else
resource = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts) resource = ActiveModel::Serializer::Adapter.create(serialized, @_adapter_opts)
end end
end end

View File

@ -211,13 +211,7 @@ module ActiveModel
association_value, association_value,
options.except(:serializer).merge(serializer_from_options(association_options)) options.except(:serializer).merge(serializer_from_options(association_options))
) )
rescue ActiveModel::Serializer::ArraySerializer::Error rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
# 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.
# 2. Convert association_value to hash using implicit as_json
# 3. Set as virtual value (serializer is nil)
# 4. Consider warning when this happens
virtual_value = association_value virtual_value = association_value
virtual_value = virtual_value.as_json if virtual_value.respond_to?(:as_json) virtual_value = virtual_value.as_json if virtual_value.respond_to?(:as_json)
association_options[:association_options][:virtual_value] = virtual_value association_options[:association_options][:virtual_value] = virtual_value

View File

@ -1,11 +1,11 @@
module ActiveModel module ActiveModel
class Serializer class Serializer
class ArraySerializer class ArraySerializer
Error = Class.new(StandardError) NoSerializerError = Class.new(StandardError)
include Enumerable include Enumerable
delegate :each, to: :@objects delegate :each, to: :@objects
attr_reader :meta, :meta_key, :objects attr_reader :meta, :meta_key
def initialize(objects, options = {}) def initialize(objects, options = {})
@resource = objects @resource = objects
@ -16,7 +16,7 @@ module ActiveModel
) )
if serializer_class.nil? if serializer_class.nil?
fail Error, "No serializer found for object: #{object.inspect}" fail NoSerializerError, "No serializer found for object: #{object.inspect}"
else else
serializer_class.new(object, options.except(:serializer)) serializer_class.new(object, options.except(:serializer))
end end