active_model_serializers/lib/active_model/serializer/array_serializer.rb
Alexandre de Oliveira bd27da1b76 Adds support for meta attribute
Currently, 0.10.0.pre doesn't support `meta` option in `render`. This
way, there's no way to support features such as pagination. `0.9` had
this feature in place.

This adds support for it, as well as fixes small things in README.md.

This won't support `meta` in array responses because arrays don't have
keys, obviously. Also, the response should have a `root` key, otherwise
no `meta` will be included.

In some cases, for example using JsonApi, ArraySerializer will result in
a response with a `root`. In that case, `meta` will be included.
2015-01-05 02:56:33 -02:00

31 lines
708 B
Ruby

module ActiveModel
class Serializer
class ArraySerializer
include Enumerable
delegate :each, to: :@objects
attr_reader :meta, :meta_key
def initialize(objects, options = {})
@objects = objects.map do |object|
serializer_class = options.fetch(
:serializer,
ActiveModel::Serializer.serializer_for(object)
)
serializer_class.new(object)
end
@meta = options[:meta]
@meta_key = options[:meta_key]
end
def json_key
@objects.first.json_key if @objects.first
end
def root=(root)
@objects.first.root = root if @objects.first
end
end
end
end