Add ArraySerializer#object like Serializer

This commit is contained in:
Benjamin Fleischer 2015-08-24 13:21:49 -05:00
parent 87c47f8fdc
commit dc4ee94fea
3 changed files with 25 additions and 20 deletions

View File

@ -163,19 +163,21 @@ module ActiveModel
def add_links(options) def add_links(options)
links = @hash.fetch(:links) { {} } links = @hash.fetch(:links) { {} }
resources = serializer.instance_variable_get(:@resource) collection = serializer.object
@hash[:links] = add_pagination_links(links, resources, options) if is_paginated?(resources) if is_paginated?(collection)
@hash[:links] = add_pagination_links(links, collection, options)
end
end end
def add_pagination_links(links, resources, options) def add_pagination_links(links, collection, options)
pagination_links = JsonApi::PaginationLinks.new(resources, options[:context]).serializable_hash(options) pagination_links = JsonApi::PaginationLinks.new(collection, options[:context]).serializable_hash(options)
links.update(pagination_links) links.update(pagination_links)
end end
def is_paginated?(resource) def is_paginated?(collection)
resource.respond_to?(:current_page) && collection.respond_to?(:current_page) &&
resource.respond_to?(:total_pages) && collection.respond_to?(:total_pages) &&
resource.respond_to?(:size) collection.respond_to?(:size)
end end
end end
end end

View File

@ -3,23 +3,22 @@ module ActiveModel
class ArraySerializer class ArraySerializer
NoSerializerError = Class.new(StandardError) NoSerializerError = Class.new(StandardError)
include Enumerable include Enumerable
delegate :each, to: :@objects delegate :each, to: :@serializers
attr_reader :root, :meta, :meta_key attr_reader :object, :root, :meta, :meta_key
def initialize(objects, options = {}) def initialize(resources, options = {})
@root = options[:root] @root = options[:root]
@resource = objects @object = resources
@objects = objects.map do |object| @serializers = resources.map do |resource|
serializer_class = options.fetch( serializer_class = options.fetch(:serializer) {
:serializer, ActiveModel::Serializer.serializer_for(resource)
ActiveModel::Serializer.serializer_for(object) }
)
if serializer_class.nil? if serializer_class.nil?
fail NoSerializerError, "No serializer found for object: #{object.inspect}" fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
else else
serializer_class.new(object, options.except(:serializer)) serializer_class.new(resource, options.except(:serializer))
end end
end end
@meta = options[:meta] @meta = options[:meta]
@ -27,7 +26,7 @@ module ActiveModel
end end
def json_key def json_key
key = root || @objects.first.try(:json_key) || @resource.try(:name).try(:underscore) key = root || @serializers.first.try(:json_key) || object.try(:name).try(:underscore)
key.try(:pluralize) key.try(:pluralize)
end end
end end

View File

@ -15,6 +15,10 @@ module ActiveModel
resource resource
end end
def test_has_object_reader_serializer_interface
assert_equal @serializer.object, @resource
end
def test_respond_to_each def test_respond_to_each
assert_respond_to @serializer, :each assert_respond_to @serializer, :each
end end