exchange pagination class to inside json_api scope

This commit is contained in:
Bruno Bacarini
2015-08-07 16:59:44 -03:00
parent b864302695
commit 1fe8b06986
8 changed files with 81 additions and 82 deletions

View File

@@ -94,21 +94,6 @@ module ActiveModel
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

View File

@@ -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
@@ -27,6 +28,8 @@ module ActiveModel
@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)
@@ -157,6 +160,20 @@ module ActiveModel
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

View File

@@ -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

View File

@@ -4,9 +4,8 @@ module ActiveModel
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]
@@ -25,7 +24,7 @@ module ActiveModel
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

View File

@@ -1,62 +0,0 @@
module ActiveModel
class Serializer
class Pagination
attr_reader :collection
def initialize(collection)
@collection = collection
end
def page_links
send(default_adapter)
end
private
def kaminari
build_links collection.size
end
def will_paginate
setup_will_paginate
build_links collection.per_page
end
def build_links(per_page)
pages_from.each_with_object({}) do |(key, value), hash|
hash[key] = "?page=#{value}&per_page=#{per_page}"
end
end
def pages_from
return {} if collection.total_pages == 1
{}.tap do |pages|
unless collection.first_page?
pages[:first] = 1
pages[:prev] = collection.current_page - 1
end
unless collection.last_page?
pages[:next] = collection.current_page + 1
pages[:last] = collection.total_pages
end
end
end
def default_adapter
return :kaminari if defined?(Kaminari)
return :will_paginate if defined?(WillPaginate::CollectionMethods)
raise "AMS relies on either Kaminari or WillPaginate." +
"Please install either dependency by adding one of those to your Gemfile"
end
def setup_will_paginate
WillPaginate::CollectionMethods.module_eval do
def first_page?() !previous_page end
def last_page?() !next_page end
end
end
end
end
end