Add pagination links automatically

Pagination links will be included in your response automatically as long
as the resource is paginated using Kaminari or WillPaginate
and if you are using a JSON-API adapter. The others adapters does not have this feature.
This commit is contained in:
Bruno Bacarini
2015-08-17 14:25:59 -03:00
parent a41d90cce4
commit 2c2f948fa0
8 changed files with 52 additions and 47 deletions

View File

@@ -2,7 +2,7 @@ require "set"
module ActiveModel
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :pagination])
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])
def initialize(resource, options = {})
@resource = resource

View File

@@ -164,7 +164,7 @@ module ActiveModel
def add_links(options)
links = @hash.fetch(:links) { {} }
resources = serializer.instance_variable_get(:@resource)
@hash[:links] = add_pagination_links(links, resources, options) if @options[:pagination]
@hash[:links] = add_pagination_links(links, resources, options) if options[:pagination]
end
def add_pagination_links(links, resources, options)

View File

@@ -14,7 +14,7 @@ module ActiveModel
def serializable_hash(options = {})
pages_from.each_with_object({}) do |(key, value), hash|
query_parameters = options.fetch(:query_parameters) { {} }
query_parameters = options[:pagination].fetch(:query_parameters) { {} }
params = query_parameters.merge(page: { number: value, size: collection.size }).to_query
hash[key] = "#{url(options)}?#{params}"
@@ -51,7 +51,7 @@ module ActiveModel
def url(options)
self_link = options.fetch(:links) {{}}
self_link.fetch(:self) {} ? options[:links][:self] : options[:original_url]
self_link.fetch(:self) {} ? options[:links][:self] : options[:pagination][:original_url]
end
end
end