mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge pull request #94 from teeparham/array-serializer-root
Add root option to ArraySerializer
This commit is contained in:
@@ -40,13 +40,17 @@ module ActionController
|
||||
end
|
||||
|
||||
def _render_option_json(json, options)
|
||||
if json.respond_to?(:to_ary)
|
||||
options[:root] ||= controller_name unless options[:root] == false
|
||||
end
|
||||
|
||||
serializer = options.delete(:serializer) ||
|
||||
(json.respond_to?(:active_model_serializer) && json.active_model_serializer)
|
||||
|
||||
if json.respond_to?(:to_ary)
|
||||
if options[:root] != false && serializer.root != false
|
||||
# default root element for arrays is serializer's root or the controller name
|
||||
# the serializer for an Array is ActiveModel::ArraySerializer
|
||||
options[:root] ||= serializer.root || controller_name
|
||||
end
|
||||
end
|
||||
|
||||
if serializer
|
||||
options[:scope] = serialization_scope
|
||||
options[:url_options] = url_options
|
||||
|
||||
@@ -29,11 +29,18 @@ module ActiveModel
|
||||
|
||||
# Active Model Array Serializer
|
||||
#
|
||||
# It serializes an array checking if each element that implements
|
||||
# It serializes an Array, checking if each element that implements
|
||||
# the +active_model_serializer+ method.
|
||||
#
|
||||
# To disable serialization of root elements, in an initializer:
|
||||
#
|
||||
# ActiveModel::ArraySerializer.root = false
|
||||
#
|
||||
class ArraySerializer
|
||||
attr_reader :object, :options
|
||||
|
||||
class_attribute :root
|
||||
|
||||
def initialize(object, options={})
|
||||
@object, @options = object, options
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user