mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
add feature to include pagination links in response
This commit is contained in:
@@ -21,9 +21,9 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def as_json(options = nil)
|
||||
hash = serializable_hash(options)
|
||||
include_meta(hash)
|
||||
hash
|
||||
serializable_hash(options).tap do |hash|
|
||||
include_meta(hash)
|
||||
end
|
||||
end
|
||||
|
||||
def self.create(resource, options = {})
|
||||
@@ -94,6 +94,21 @@ 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
|
||||
|
||||
@@ -4,8 +4,9 @@ module ActiveModel
|
||||
NoSerializerError = Class.new(StandardError)
|
||||
include Enumerable
|
||||
delegate :each, to: :@objects
|
||||
delegate :page_links, to: :pagination
|
||||
|
||||
attr_reader :root, :meta, :meta_key
|
||||
attr_reader :root, :meta, :meta_key, :pagination
|
||||
|
||||
def initialize(objects, options = {})
|
||||
@root = options[:root]
|
||||
@@ -24,6 +25,7 @@ module ActiveModel
|
||||
end
|
||||
@meta = options[:meta]
|
||||
@meta_key = options[:meta_key]
|
||||
@pagination = ActiveModel::Serializer::Pagination.new(objects) if options[:pagination]
|
||||
end
|
||||
|
||||
def json_key
|
||||
|
||||
63
lib/active_model/serializer/pagination.rb
Normal file
63
lib/active_model/serializer/pagination.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
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 = pages_from.each_with_object({}) do |(key, value), hash|
|
||||
hash[key] = "?page=#{value}&per_page=#{per_page}"
|
||||
end
|
||||
{ pages: pages } unless pages.empty?
|
||||
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
|
||||
@@ -2,6 +2,7 @@ require 'active_model'
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user