mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
[FIX] Fetch json key from item serializer if empty collection is passed to collection serializer and each_searializer is specified.
This commit is contained in:
committed by
Benjamin Fleischer
parent
33a0f9c806
commit
2dd0c33461
@@ -10,8 +10,14 @@ module ActiveModel
|
||||
def initialize(resources, options = {})
|
||||
@root = options[:root]
|
||||
@object = resources
|
||||
|
||||
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
||||
|
||||
if resources.blank? && options[:serializer]
|
||||
@each_serializer = options[:serializer]
|
||||
end
|
||||
|
||||
@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
|
||||
@@ -27,7 +33,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def json_key
|
||||
root || derived_root
|
||||
root || derived_root || guess_root || default_root
|
||||
end
|
||||
|
||||
def paginated?
|
||||
@@ -43,8 +49,15 @@ module ActiveModel
|
||||
private
|
||||
|
||||
def derived_root
|
||||
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
||||
key.try(:pluralize)
|
||||
serializers.first.try(:json_key).try(:pluralize)
|
||||
end
|
||||
|
||||
def default_root
|
||||
object.try(:name).try(:underscore).try(:pluralize)
|
||||
end
|
||||
|
||||
def guess_root
|
||||
@each_serializer.try(:allocate).try(:json_key).try(:pluralize)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user