Skip to content

Commit

Permalink
Merge pull request #1079 from bf4/all_serializer_have_object
Browse files Browse the repository at this point in the history
Add ArraySerializer#object like Serializer
  • Loading branch information
joaomdmoura committed Sep 3, 2015
2 parents e0b74d8 + dc4ee94 commit 6784866
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
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

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

0 comments on commit 6784866

Please sign in to comment.