Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArraySerializer#object like Serializer #1079

Merged
merged 1 commit into from
Sep 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,21 @@ def add_resource_relationships(attrs, serializer, options = {})

def add_links(options)
links = @hash.fetch(:links) { {} }
resources = serializer.instance_variable_get(:@resource)
@hash[:links] = add_pagination_links(links, resources, options) if is_paginated?(resources)
collection = serializer.object
if is_paginated?(collection)
@hash[:links] = add_pagination_links(links, collection, options)
end
end

def add_pagination_links(links, resources, options)
pagination_links = JsonApi::PaginationLinks.new(resources, options[:context]).serializable_hash(options)
def add_pagination_links(links, collection, options)
pagination_links = JsonApi::PaginationLinks.new(collection, options[:context]).serializable_hash(options)
links.update(pagination_links)
end

def is_paginated?(resource)
resource.respond_to?(:current_page) &&
resource.respond_to?(:total_pages) &&
resource.respond_to?(:size)
def is_paginated?(collection)
collection.respond_to?(:current_page) &&
collection.respond_to?(:total_pages) &&
collection.respond_to?(:size)
end
end
end
Expand Down
23 changes: 11 additions & 12 deletions lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ class Serializer
class ArraySerializer
NoSerializerError = Class.new(StandardError)
include Enumerable
delegate :each, to: :@objects
delegate :each, to: :@serializers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


attr_reader :root, :meta, :meta_key
attr_reader :object, :root, :meta, :meta_key

def initialize(objects, options = {})
def initialize(resources, options = {})
@root = options[:root]
@resource = objects
@objects = objects.map do |object|
serializer_class = options.fetch(
:serializer,
ActiveModel::Serializer.serializer_for(object)
)
@object = resources
@serializers = resources.map do |resource|
serializer_class = options.fetch(:serializer) {
ActiveModel::Serializer.serializer_for(resource)
}

if serializer_class.nil?
fail NoSerializerError, "No serializer found for object: #{object.inspect}"
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
else
serializer_class.new(object, options.except(:serializer))
serializer_class.new(resource, options.except(:serializer))
end
end
@meta = options[:meta]
@meta_key = options[:meta_key]
end

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)
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def build_named_collection(*resource)
resource
end

def test_has_object_reader_serializer_interface
assert_equal @serializer.object, @resource
end

def test_respond_to_each
assert_respond_to @serializer, :each
end
Expand Down