mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Include Serializer._type in collection serializer json_key cascade
This commit is contained in:
parent
2dd0c33461
commit
a74d174420
@ -3,6 +3,8 @@
|
|||||||
Breaking changes:
|
Breaking changes:
|
||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
- [#1618](https://github.com/rails-api/active_model_serializers/issues/1618) Get collection root key for
|
||||||
|
empty collection from explicit serializer option, when possible. (@bf4)
|
||||||
- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Provide key translation. (@remear)
|
- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Provide key translation. (@remear)
|
||||||
- [#1494](https://github.com/rails-api/active_model_serializers/pull/1494) Make serializers serializalbe
|
- [#1494](https://github.com/rails-api/active_model_serializers/pull/1494) Make serializers serializalbe
|
||||||
(using the Attributes adapter by default). (@bf4)
|
(using the Attributes adapter by default). (@bf4)
|
||||||
|
|||||||
@ -182,7 +182,7 @@ module ActiveModel
|
|||||||
|
|
||||||
# Used by adapter as resource root.
|
# Used by adapter as resource root.
|
||||||
def json_key
|
def json_key
|
||||||
root || object.class.model_name.to_s.underscore
|
root || _type || object.class.model_name.to_s.underscore
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_attribute_for_serialization(attr)
|
def read_attribute_for_serialization(attr)
|
||||||
|
|||||||
@ -8,15 +8,10 @@ module ActiveModel
|
|||||||
attr_reader :object, :root
|
attr_reader :object, :root
|
||||||
|
|
||||||
def initialize(resources, options = {})
|
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)
|
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|
|
@serializers = resources.map do |resource|
|
||||||
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
|
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
|
||||||
|
|
||||||
@ -32,9 +27,28 @@ module ActiveModel
|
|||||||
true
|
true
|
||||||
end
|
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
|
def json_key
|
||||||
root || derived_root || guess_root || default_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
|
end
|
||||||
|
# rubocop:enable Metrics/CyclomaticComplexity
|
||||||
|
|
||||||
def paginated?
|
def paginated?
|
||||||
object.respond_to?(:current_page) &&
|
object.respond_to?(:current_page) &&
|
||||||
@ -44,21 +58,7 @@ module ActiveModel
|
|||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
attr_reader :serializers
|
attr_reader :serializers, :options
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def derived_root
|
|
||||||
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,6 +3,10 @@ require 'test_helper'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class CollectionSerializerTest < ActiveSupport::TestCase
|
class CollectionSerializerTest < ActiveSupport::TestCase
|
||||||
|
MessagesSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
type 'messages'
|
||||||
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@comment = Comment.new
|
@comment = Comment.new
|
||||||
@post = Post.new
|
@post = Post.new
|
||||||
|
|||||||
6
test/fixtures/poro.rb
vendored
6
test/fixtures/poro.rb
vendored
@ -183,12 +183,6 @@ PaginatedSerializer = Class.new(ActiveModel::Serializer::CollectionSerializer) d
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
MessagesSerializer = Class.new(ActiveModel::Serializer) do
|
|
||||||
def json_key
|
|
||||||
'messages'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
attribute :id
|
attribute :id
|
||||||
attribute :name, key: :title
|
attribute :name, key: :title
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user