mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
return complete URIs on pagination links
This commit is contained in:
parent
36c452e60b
commit
e62a7d6f34
@ -30,10 +30,10 @@ ex:
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"links": {
|
"links": {
|
||||||
"first": "?page=1&per_page=1",
|
"first": "http://example.com/articles?page=1&per_page=1",
|
||||||
"prev": "?page=2&per_page=1",
|
"prev": "http://example.com/articles?page=2&per_page=1",
|
||||||
"next": "?page=4&per_page=1",
|
"next": "http://example.com/articles?page=4&per_page=1",
|
||||||
"last": "?page=13&per_page=1"
|
"last": "http://example.com/articles?page=13&per_page=1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -25,6 +25,7 @@ module ActionController
|
|||||||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
|
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
|
||||||
options[:adapter] = false
|
options[:adapter] = false
|
||||||
end
|
end
|
||||||
|
options[:original_url] = original_url
|
||||||
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
|
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
|
||||||
if serializable_resource.serializer?
|
if serializable_resource.serializer?
|
||||||
serializable_resource.serialization_scope ||= serialization_scope
|
serializable_resource.serialization_scope ||= serialization_scope
|
||||||
@ -57,5 +58,9 @@ module ActionController
|
|||||||
self._serialization_scope = scope
|
self._serialization_scope = scope
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def original_url
|
||||||
|
request.original_url.sub(/\?.*$/, "")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -29,7 +29,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
include_pagination_links if serializer.pagination
|
include_pagination_links if serializer.options[:pagination]
|
||||||
else
|
else
|
||||||
@hash[:data] = attributes_for_serializer(serializer, options)
|
@hash[:data] = attributes_for_serializer(serializer, options)
|
||||||
add_resource_relationships(@hash[:data], serializer)
|
add_resource_relationships(@hash[:data], serializer)
|
||||||
@ -168,7 +168,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def page_links
|
def page_links
|
||||||
@links ||= JsonApi::PaginationLinks.new(serializer.resource).page_links
|
@links ||= JsonApi::PaginationLinks.new(serializer.resource, serializer.options).page_links
|
||||||
end
|
end
|
||||||
|
|
||||||
def links?
|
def links?
|
||||||
|
|||||||
@ -5,11 +5,12 @@ module ActiveModel
|
|||||||
class PaginationLinks
|
class PaginationLinks
|
||||||
FIRST_PAGE = 1
|
FIRST_PAGE = 1
|
||||||
|
|
||||||
attr_reader :collection
|
attr_reader :collection, :options
|
||||||
|
|
||||||
def initialize(collection)
|
def initialize(collection, options={})
|
||||||
raise_unless_any_gem_installed
|
raise_unless_any_gem_installed
|
||||||
@collection = collection
|
@collection = collection
|
||||||
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
def page_links
|
def page_links
|
||||||
@ -20,7 +21,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def build_links
|
def build_links
|
||||||
pages_from.each_with_object({}) do |(key, value), hash|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -45,6 +46,15 @@ module ActiveModel
|
|||||||
raise "AMS relies on either Kaminari or WillPaginate." +
|
raise "AMS relies on either Kaminari or WillPaginate." +
|
||||||
"Please install either dependency by adding one of those to your Gemfile"
|
"Please install either dependency by adding one of those to your Gemfile"
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,7 @@ module ActiveModel
|
|||||||
include Enumerable
|
include Enumerable
|
||||||
delegate :each, to: :@objects
|
delegate :each, to: :@objects
|
||||||
|
|
||||||
attr_reader :root, :meta, :meta_key, :pagination, :resource
|
attr_reader :root, :meta, :meta_key, :options, :resource
|
||||||
|
|
||||||
def initialize(objects, options = {})
|
def initialize(objects, options = {})
|
||||||
@root = options[:root]
|
@root = options[:root]
|
||||||
@ -24,7 +24,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
@meta = options[:meta]
|
@meta = options[:meta]
|
||||||
@meta_key = options[:meta_key]
|
@meta_key = options[:meta_key]
|
||||||
@pagination = options[:pagination]
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
|
|||||||
@ -47,7 +47,10 @@ module ActionController
|
|||||||
tests PaginationTestController
|
tests PaginationTestController
|
||||||
|
|
||||||
def test_render_pagination_links_with_will_paginate
|
def test_render_pagination_links_with_will_paginate
|
||||||
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=1&per_page=1", "next"=>"?page=3&per_page=1", "last"=>"?page=3&per_page=1"}
|
expected_links = {"first"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=1&per_page=1",
|
||||||
|
"prev"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=1&per_page=1",
|
||||||
|
"next"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=3&per_page=1",
|
||||||
|
"last"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=3&per_page=1"}
|
||||||
|
|
||||||
get :render_pagination_using_will_paginate, page: 2, per_page: 1
|
get :render_pagination_using_will_paginate, page: 2, per_page: 1
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
@ -55,21 +58,26 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_render_only_last_and_next_pagination_links
|
def test_render_only_last_and_next_pagination_links
|
||||||
expected_links = {"next"=>"?page=2&per_page=2", "last"=>"?page=2&per_page=2"}
|
expected_links = {"next"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=2&per_page=2",
|
||||||
|
"last"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate?page=2&per_page=2"}
|
||||||
get :render_pagination_using_will_paginate, page: 1, per_page: 2
|
get :render_pagination_using_will_paginate, page: 1, per_page: 2
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert_equal expected_links, response['links']
|
assert_equal expected_links, response['links']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_pagination_links_with_kaminari
|
def test_render_pagination_links_with_kaminari
|
||||||
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=1&per_page=1", "next"=>"?page=3&per_page=1", "last"=>"?page=3&per_page=1"}
|
expected_links = {"first"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=1&per_page=1",
|
||||||
|
"prev"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=1&per_page=1",
|
||||||
|
"next"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=3&per_page=1",
|
||||||
|
"last"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=3&per_page=1"}
|
||||||
get :render_pagination_using_kaminari, page: 2, per_page: 1
|
get :render_pagination_using_kaminari, page: 2, per_page: 1
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert_equal expected_links, response['links']
|
assert_equal expected_links, response['links']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_only_prev_and_first_pagination_links
|
def test_render_only_prev_and_first_pagination_links
|
||||||
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=2&per_page=1"}
|
expected_links = {"first"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=1&per_page=1",
|
||||||
|
"prev"=>"http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari?page=2&per_page=1"}
|
||||||
get :render_pagination_using_kaminari, page: 3, per_page: 1
|
get :render_pagination_using_kaminari, page: 3, per_page: 1
|
||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert_equal expected_links, response['links']
|
assert_equal expected_links, response['links']
|
||||||
|
|||||||
@ -404,6 +404,9 @@ module ActionController
|
|||||||
def use_adapter?
|
def use_adapter?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
def original_url
|
||||||
|
"http://example.com/"
|
||||||
|
end
|
||||||
}.new
|
}.new
|
||||||
assert_match /adapter: false/, (capture(:stderr) {
|
assert_match /adapter: false/, (capture(:stderr) {
|
||||||
controller.get_serializer(Profile.new)
|
controller.get_serializer(Profile.new)
|
||||||
@ -415,6 +418,9 @@ module ActionController
|
|||||||
def use_adapter?
|
def use_adapter?
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
def original_url
|
||||||
|
"http://example.com/"
|
||||||
|
end
|
||||||
}.new
|
}.new
|
||||||
assert_equal "", (capture(:stderr) {
|
assert_equal "", (capture(:stderr) {
|
||||||
controller.get_serializer(Profile.new)
|
controller.get_serializer(Profile.new)
|
||||||
|
|||||||
@ -50,23 +50,23 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
}],
|
}],
|
||||||
links:{
|
links:{
|
||||||
first:"?page=1&per_page=1",
|
first: "http://example.com?page=1&per_page=1",
|
||||||
prev:"?page=1&per_page=1",
|
prev: "http://example.com?page=1&per_page=1",
|
||||||
next:"?page=3&per_page=1",
|
next: "http://example.com?page=3&per_page=1",
|
||||||
last:"?page=3&per_page=1"
|
last: "http://example.com?page=3&per_page=1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pagination_links_using_kaminari
|
def test_pagination_links_using_kaminari
|
||||||
serializer = ArraySerializer.new(using_kaminari, pagination: true)
|
serializer = ArraySerializer.new(using_kaminari, pagination: true, original_url: "http://example.com")
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
|
|
||||||
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pagination_links_using_will_paginate
|
def test_pagination_links_using_will_paginate
|
||||||
serializer = ArraySerializer.new(using_will_paginate, pagination: true)
|
serializer = ArraySerializer.new(using_will_paginate, pagination: true, original_url: "http://example.com")
|
||||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
|
|
||||||
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
||||||
|
|||||||
@ -95,7 +95,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_pagination_attr_readers
|
def test_pagination_attr_readers
|
||||||
serializer = ArraySerializer.new(@resource, pagination: true)
|
serializer = ArraySerializer.new(@resource, pagination: true)
|
||||||
assert_equal serializer.pagination, true
|
assert_equal serializer.options[:pagination], true
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_resource
|
def test_resource
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user