Move Serializer#serialize into Serializer#serializable_hash

This commit is contained in:
Benjamin Fleischer 2016-06-05 23:01:21 -05:00
parent caf4910b6e
commit 7254d34c90
3 changed files with 41 additions and 38 deletions

View File

@ -109,6 +109,7 @@ module ActiveModel
end end
end end
# @api private
def self.serialization_adapter_instance def self.serialization_adapter_instance
@serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes @serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
end end
@ -138,9 +139,7 @@ module ActiveModel
# associations, similar to how ActiveModel::Serializers::JSON is used # associations, similar to how ActiveModel::Serializers::JSON is used
# in ActiveRecord::Base. # in ActiveRecord::Base.
# #
# TODO: Move to here the Attributes adapter logic for # TODO: Include <tt>ActiveModel::Serializers::JSON</tt>.
# +serializable_hash_for_single_resource(options)+
# and include <tt>ActiveModel::Serializers::JSON</tt>.
# So that the below is true: # So that the below is true:
# @param options [nil, Hash] The same valid options passed to `serializable_hash` # @param options [nil, Hash] The same valid options passed to `serializable_hash`
# (:only, :except, :methods, and :include). # (:only, :except, :methods, and :include).
@ -164,10 +163,13 @@ module ActiveModel
# serializer.as_json(include: :posts) # serializer.as_json(include: :posts)
# # Second level and higher order associations work as well: # # Second level and higher order associations work as well:
# serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } }) # serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
def serializable_hash(adapter_opts = nil) def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
adapter_opts ||= {} adapter_options ||= {}
adapter_opts = { include: '*' }.merge!(adapter_opts) options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
serialize(adapter_opts) cached_attributes = adapter_options[:cached_attributes] ||= {}
resource = cached_attributes(options[:fields], cached_attributes, adapter_instance)
relationships = resource_relationships(adapter_options, options, adapter_instance)
resource.merge(relationships)
end end
alias to_hash serializable_hash alias to_hash serializable_hash
alias to_h serializable_hash alias to_h serializable_hash
@ -199,15 +201,6 @@ module ActiveModel
end end
end end
# @api private
def serialize(adapter_options, options = {}, adapter_instance = self.class.serialization_adapter_instance)
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
cached_attributes = adapter_options[:cached_attributes] ||= {}
resource = cached_attributes(options[:fields], cached_attributes, adapter_instance)
relationships = resource_relationships(adapter_options, options, adapter_instance)
resource.merge(relationships)
end
# @api private # @api private
def resource_relationships(adapter_options, options, adapter_instance) def resource_relationships(adapter_options, options, adapter_instance)
relationships = {} relationships = {}
@ -227,7 +220,7 @@ module ActiveModel
association_object = association_serializer && association_serializer.object association_object = association_serializer && association_serializer.object
return unless association_object return unless association_object
relationship_value = association_serializer.serialize(adapter_options, {}, adapter_instance) relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
if association.options[:polymorphic] && relationship_value if association.options[:polymorphic] && relationship_value
polymorphic_type = association_object.class.name.underscore polymorphic_type = association_object.class.name.underscore

View File

@ -11,22 +11,23 @@ module ActiveModel
@object = resources @object = resources
@options = options @options = options
@root = options[:root] @root = options[:root]
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer) @serializers = serializers_from_resources
@serializers = resources.map do |resource|
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
if serializer_class.nil? # rubocop:disable Style/GuardClause
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
else
serializer_class.new(resource, options.except(:serializer))
end
end
end end
def success? def success?
true true
end end
# @api private
def serializable_hash(adapter_options, options, adapter_instance)
include_directive = ActiveModel::Serializer.include_directive_from_options(adapter_options)
adapter_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, include_directive)
adapter_opts = adapter_options.merge(include_directive: include_directive)
serializers.map do |serializer|
serializer.serializable_hash(adapter_opts, options, adapter_instance)
end
end
# TODO: unify naming of root, json_key, and _type. Right now, a serializer's # TODO: unify naming of root, json_key, and _type. Right now, a serializer's
# json_key comes from the root option or the object's model name, by default. # json_key comes from the root option or the object's model name, by default.
# But, if a dev defines a custom `json_key` method with an explicit value, # But, if a dev defines a custom `json_key` method with an explicit value,
@ -56,19 +57,28 @@ module ActiveModel
object.respond_to?(:size) object.respond_to?(:size)
end end
# @api private
def serialize(adapter_options, options, adapter_instance)
include_directive = ActiveModel::Serializer.include_directive_from_options(adapter_options)
adapter_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, include_directive)
adapter_opts = adapter_options.merge(include_directive: include_directive)
serializers.map do |serializer|
serializer.serialize(adapter_opts, options, adapter_instance)
end
end
protected protected
attr_reader :serializers, :options attr_reader :serializers, :options
private
def serializers_from_resources
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
object.map do |resource|
serializer_from_resource(resource, serializer_context_class, options)
end
end
def serializer_from_resource(resource, serializer_context_class, options)
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
if serializer_class.nil? # rubocop:disable Style/GuardClause
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
else
serializer_class.new(resource, options.except(:serializer))
end
end
end end
end end
end end

View File

@ -3,7 +3,7 @@ module ActiveModelSerializers
class Attributes < Base class Attributes < Base
def serializable_hash(options = nil) def serializable_hash(options = nil)
options = serialization_options(options) options = serialization_options(options)
serializer.serialize(instance_options, options, self) serializer.serializable_hash(instance_options, options, self)
end end
end end
end end