mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
return complete URIs on pagination links
This commit is contained in:
@@ -25,6 +25,7 @@ module ActionController
|
||||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
|
||||
options[:adapter] = false
|
||||
end
|
||||
options[:original_url] = original_url
|
||||
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
|
||||
if serializable_resource.serializer?
|
||||
serializable_resource.serialization_scope ||= serialization_scope
|
||||
@@ -57,5 +58,9 @@ module ActionController
|
||||
self._serialization_scope = scope
|
||||
end
|
||||
end
|
||||
|
||||
def original_url
|
||||
request.original_url.sub(/\?.*$/, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -29,7 +29,7 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
include_pagination_links if serializer.pagination
|
||||
include_pagination_links if serializer.options[:pagination]
|
||||
else
|
||||
@hash[:data] = attributes_for_serializer(serializer, options)
|
||||
add_resource_relationships(@hash[:data], serializer)
|
||||
@@ -168,7 +168,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def page_links
|
||||
@links ||= JsonApi::PaginationLinks.new(serializer.resource).page_links
|
||||
@links ||= JsonApi::PaginationLinks.new(serializer.resource, serializer.options).page_links
|
||||
end
|
||||
|
||||
def links?
|
||||
|
||||
@@ -5,11 +5,12 @@ module ActiveModel
|
||||
class PaginationLinks
|
||||
FIRST_PAGE = 1
|
||||
|
||||
attr_reader :collection
|
||||
attr_reader :collection, :options
|
||||
|
||||
def initialize(collection)
|
||||
def initialize(collection, options={})
|
||||
raise_unless_any_gem_installed
|
||||
@collection = collection
|
||||
@options = options
|
||||
end
|
||||
|
||||
def page_links
|
||||
@@ -20,7 +21,7 @@ module ActiveModel
|
||||
|
||||
def build_links
|
||||
pages_from.each_with_object({}) do |(key, value), hash|
|
||||
hash[key] = "?page=#{value}&per_page=#{collection.size}"
|
||||
hash[key] = "#{url}?page=#{value}&per_page=#{collection.size}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,6 +46,15 @@ module ActiveModel
|
||||
raise "AMS relies on either Kaminari or WillPaginate." +
|
||||
"Please install either dependency by adding one of those to your Gemfile"
|
||||
end
|
||||
|
||||
def url
|
||||
return default_url unless options && options[:links] && options[:links][:self]
|
||||
options[:links][:self]
|
||||
end
|
||||
|
||||
def default_url
|
||||
options[:original_url]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ module ActiveModel
|
||||
include Enumerable
|
||||
delegate :each, to: :@objects
|
||||
|
||||
attr_reader :root, :meta, :meta_key, :pagination, :resource
|
||||
attr_reader :root, :meta, :meta_key, :options, :resource
|
||||
|
||||
def initialize(objects, options = {})
|
||||
@root = options[:root]
|
||||
@@ -24,7 +24,7 @@ module ActiveModel
|
||||
end
|
||||
@meta = options[:meta]
|
||||
@meta_key = options[:meta_key]
|
||||
@pagination = options[:pagination]
|
||||
@options = options
|
||||
end
|
||||
|
||||
def json_key
|
||||
|
||||
Reference in New Issue
Block a user