Rename ArraySerializer to CollectionSerializer for clarity

This commit is contained in:
Benjamin Fleischer
2015-10-07 05:07:00 -05:00
parent 737784c9b7
commit 2c8b9b796d
19 changed files with 213 additions and 148 deletions

View File

@@ -31,7 +31,7 @@ module ActionController
serializable_resource.serialization_scope_name = _serialization_scope
begin
serializable_resource.adapter
rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
resource
end
else

View File

@@ -1,4 +1,5 @@
require 'thread_safe'
require 'active_model/serializer/collection_serializer'
require 'active_model/serializer/array_serializer'
require 'active_model/serializer/include_tree'
require 'active_model/serializer/associations'
@@ -105,7 +106,7 @@ module ActiveModel
if resource.respond_to?(:serializer_class)
resource.serializer_class
elsif resource.respond_to?(:to_ary)
config.array_serializer
config.collection_serializer
else
options.fetch(:serializer) { get_serializer_for(resource.class) }
end

View File

@@ -1,41 +1,9 @@
module ActiveModel
class Serializer
class ArraySerializer
NoSerializerError = Class.new(StandardError)
include Enumerable
delegate :each, to: :@serializers
attr_reader :object, :root
def initialize(resources, options = {})
@root = options[:root]
@object = resources
@serializers = resources.map do |resource|
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
if serializer_class.nil?
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
else
serializer_class.new(resource, options.except(:serializer))
end
end
end
def json_key
key = root || serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize)
end
def paginated?
object.respond_to?(:current_page) &&
object.respond_to?(:total_pages) &&
object.respond_to?(:size)
end
protected
attr_reader :serializers
require 'active_model/serializer/collection_serializer'
class ActiveModel::Serializer
class ArraySerializer < CollectionSerializer
def initialize(*)
warn "Calling deprecated ArraySerializer in #{caller[0]}. Please use CollectionSerializer"
super
end
end
end

View File

@@ -0,0 +1,41 @@
module ActiveModel
class Serializer
class CollectionSerializer
NoSerializerError = Class.new(StandardError)
include Enumerable
delegate :each, to: :@serializers
attr_reader :object, :root
def initialize(resources, options = {})
@root = options[:root]
@object = resources
@serializers = resources.map do |resource|
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
if serializer_class.nil?
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
else
serializer_class.new(resource, options.except(:serializer))
end
end
end
def json_key
key = root || serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize)
end
def paginated?
object.respond_to?(:current_page) &&
object.respond_to?(:total_pages) &&
object.respond_to?(:size)
end
protected
attr_reader :serializers
end
end
end

View File

@@ -7,9 +7,19 @@ module ActiveModel
# Configuration options may also be set in
# Serializers and Adapters
included do |base|
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
base.config.adapter = :attributes
base.config.jsonapi_resource_type = :plural
config = base.config
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
def config.array_serializer=(collection_serializer)
self.collection_serializer = collection_serializer
end
def config.array_serializer
collection_serializer
end
config.adapter = :attributes
config.jsonapi_resource_type = :plural
end
end
end

View File

@@ -50,7 +50,7 @@ module ActiveModel
association_value,
serializer_options(subject, parent_serializer_options, reflection_options)
)
rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
end
elsif !association_value.nil? && !association_value.instance_of?(Object)