mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Merge pull request #1618 from bf4/RomanKapitonov-master
Empty collection root key from explicit serializer option
This commit is contained in:
@@ -182,7 +182,7 @@ module ActiveModel
|
||||
|
||||
# Used by adapter as resource root.
|
||||
def json_key
|
||||
root || object.class.model_name.to_s.underscore
|
||||
root || _type || object.class.model_name.to_s.underscore
|
||||
end
|
||||
|
||||
def read_attribute_for_serialization(attr)
|
||||
|
||||
@@ -8,10 +8,11 @@ module ActiveModel
|
||||
attr_reader :object, :root
|
||||
|
||||
def initialize(resources, options = {})
|
||||
@root = options[:root]
|
||||
@object = resources
|
||||
@object = resources
|
||||
@options = options
|
||||
@root = options[:root]
|
||||
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
||||
@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? # rubocop:disable Style/GuardClause
|
||||
@@ -26,9 +27,28 @@ module ActiveModel
|
||||
true
|
||||
end
|
||||
|
||||
# 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.
|
||||
# But, if a dev defines a custom `json_key` method with an explicit value,
|
||||
# we have no simple way to know that it is safe to call that instance method.
|
||||
# (which is really a class property at this point, anyhow).
|
||||
# rubocop:disable Metrics/CyclomaticComplexity
|
||||
# Disabling cop since it's good to highlight the complexity of this method by
|
||||
# including all the logic right here.
|
||||
def json_key
|
||||
root || derived_root
|
||||
return root if root
|
||||
# 1. get from options[:serializer] for empty resource collection
|
||||
key = object.empty? &&
|
||||
(explicit_serializer_class = options[:serializer]) &&
|
||||
explicit_serializer_class._type
|
||||
# 2. get from first serializer instance in collection
|
||||
key ||= (serializer = serializers.first) && serializer.json_key
|
||||
# 3. get from collection name, if a named collection
|
||||
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
|
||||
# 4. key may be nil for empty collection and no serializer option
|
||||
key && key.pluralize
|
||||
end
|
||||
# rubocop:enable Metrics/CyclomaticComplexity
|
||||
|
||||
def paginated?
|
||||
object.respond_to?(:current_page) &&
|
||||
@@ -38,14 +58,7 @@ module ActiveModel
|
||||
|
||||
protected
|
||||
|
||||
attr_reader :serializers
|
||||
|
||||
private
|
||||
|
||||
def derived_root
|
||||
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
||||
key.try(:pluralize)
|
||||
end
|
||||
attr_reader :serializers, :options
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user