Skip to content

Commit

Permalink
exchange pagination class to inside json_api scope
Browse files Browse the repository at this point in the history
  • Loading branch information
bacarini committed Aug 7, 2015
1 parent a61675d commit 36d2140
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ If you want pagination links in your response, specify it in the `render`

AMS relies on either Kaminari or WillPaginate. Please install either dependency by adding one of those to your Gemfile.

Pagination links will only be included in your response if you are using an Adapter that supports `root`, as JsonAPI and Json adapters, the default adapter (FlattenJson) doesn't have `root`.
Pagination links will only be included in your response if you are using a JsonAPI adapter, the others adapters doesn't have this feature.

## Caching

Expand Down
16 changes: 0 additions & 16 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def as_json(options = nil)
return hash if self.class == FlattenJson

include_meta(hash)
include_pagination_links(hash) if options && options[:pagination]
end
end

Expand Down Expand Up @@ -97,21 +96,6 @@ def include_meta(json)
json[meta_key] = meta if meta
json
end

def include_pagination_links(json)
return unless page_links

links?(json) ? json.merge!(page_links) : json['links'] = page_links
json
end

def page_links
@links ||= serializer.page_links
end

def links?(json)
!json['links'].nil?
end
end
end
end
17 changes: 17 additions & 0 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_model/serializer/adapter/json_api/fragment_cache'
require 'active_model/serializer/adapter/json_api/pagination_links'

module ActiveModel
class Serializer
Expand Down Expand Up @@ -27,6 +28,8 @@ def serializable_hash(options = nil)
@hash[:included] |= result[:included]
end
end

include_pagination_links if serializer.pagination
else
@hash[:data] = attributes_for_serializer(serializer, options)
add_resource_relationships(@hash[:data], serializer)
Expand Down Expand Up @@ -157,6 +160,20 @@ def add_resource_relationships(attrs, serializer, options = {})
end
end
end

def include_pagination_links
return if page_links.empty?

links? ? @hash[:links].merge!(page_links) : @hash[:links] = page_links
end

def page_links
@links ||= JsonApi::PaginationLinks.new(serializer.resource).page_links
end

def links?
!@hash[:links].nil?
end
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions lib/active_model/serializer/adapter/json_api/pagination_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module ActiveModel
class Serializer
class Adapter
class JsonApi < Adapter
class PaginationLinks
FIRST_PAGE = 1

attr_reader :collection

def initialize(collection)
raise_unless_any_gem_installed
@collection = collection
end

def page_links
build_links
end

private

def build_links
pages_from.each_with_object({}) do |(key, value), hash|
hash[key] = "?page=#{value}&per_page=#{collection.size}"
end
end

def pages_from
return {} if collection.total_pages == FIRST_PAGE

{}.tap do |pages|
unless collection.current_page == FIRST_PAGE
pages[:first] = FIRST_PAGE
pages[:prev] = collection.current_page - FIRST_PAGE
end

unless collection.current_page == collection.total_pages
pages[:next] = collection.current_page + FIRST_PAGE
pages[:last] = collection.total_pages
end
end
end

def raise_unless_any_gem_installed
return if defined?(WillPaginate) || defined?(Kaminari)
raise "AMS relies on either Kaminari or WillPaginate." +
"Please install either dependency by adding one of those to your Gemfile"
end
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ class ArraySerializer
NoSerializerError = Class.new(StandardError)
include Enumerable
delegate :each, to: :@objects
delegate :page_links, to: :pagination

attr_reader :root, :meta, :meta_key, :pagination
attr_reader :root, :meta, :meta_key, :pagination, :resource

def initialize(objects, options = {})
@root = options[:root]
Expand All @@ -25,7 +24,7 @@ def initialize(objects, options = {})
end
@meta = options[:meta]
@meta_key = options[:meta_key]
@pagination = ActiveModel::Serializer::Pagination.new(objects) if options[:pagination]
@pagination = options[:pagination]
end

def json_key
Expand Down
62 changes: 0 additions & 62 deletions lib/active_model/serializer/pagination.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'active_model/serializer/version'
require 'active_model/serializer'
require 'active_model/serializer/fieldset'
require 'active_model/serializer/pagination'
require 'active_model/serializable_resource'

begin
Expand Down
9 changes: 9 additions & 0 deletions test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ def test_json_key_with_root_and_no_serializers
serializer = ArraySerializer.new(build_named_collection, root: 'custom_root')
assert_equal serializer.json_key, 'custom_roots'
end

def test_pagination_attr_readers
serializer = ArraySerializer.new(@resource, pagination: true)
assert_equal serializer.pagination, true
end

def test_resource
assert_equal @serializer.resource, @resource
end
end
end
end

0 comments on commit 36d2140

Please sign in to comment.