This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from barkbox/pagination-links
Pagination links in ams
- Loading branch information
Showing
9 changed files
with
201 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module PagingCursor | ||
end | ||
|
||
|
||
require 'paging_cursor/config' | ||
require 'paging_cursor/direction' | ||
require 'paging_cursor/active_record' | ||
require 'paging_cursor/active_model_serializer' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'active_model_serializers' | ||
|
||
# See https://github.com/rails-api/active_model_serializers/blob/a032201a91cbca407211bca0392ba881eef1f7ba/lib/active_model/serializer/collection_serializer.rb | ||
# for original version | ||
module ActiveModel | ||
class Serializer | ||
class CollectionSerializer | ||
|
||
def paginated? | ||
ActiveModelSerializers.config.jsonapi_pagination_links_enabled && | ||
object.respond_to?(:cursor_before) && | ||
object.respond_to?(:cursor_after) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# See https://github.com/rails-api/active_model_serializers/blob/a032201a91cbca407211bca0392ba881eef1f7ba/lib/active_model_serializers/adapter/json_api/pagination_links.rb | ||
# for the original version | ||
module ActiveModelSerializers | ||
module Adapter | ||
class JsonApi < Base | ||
class PaginationLinks | ||
|
||
def as_json | ||
pages_from.each_with_object({}) do |(key, value), hash| | ||
params = query_parameters.merge(cursor: value) | ||
params = Rack::Utils.build_nested_query(params) | ||
hash[key] = "#{url(adapter_options)}?#{params}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def pages_from | ||
{}.tap do |pages| | ||
return {} if collection.empty? | ||
|
||
pages[:self] = { before: (collection.cursor_after + 1).to_s } | ||
pages[:first] = { before: nil } | ||
pages[:prev] = { after: collection.cursor_after.to_s } | ||
pages[:next] = { before: collection.cursor_before.to_s } | ||
pages[:last] = { after: nil } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'rails_helper' | ||
include ActiveModelSerializers::Adapter | ||
|
||
describe JsonApi::PaginationLinks do | ||
|
||
before do | ||
10.times do | ||
User.create | ||
end | ||
@per_page = 5 | ||
end | ||
|
||
after do | ||
User.delete_all | ||
end | ||
|
||
shared_examples_for 'it returns the correct links' do | ||
specify do | ||
serialized_object = User.cursor(before: cursor, limit: @per_page) | ||
options = { | ||
serialization_context: OpenStruct.new( | ||
request_url: 'www.example.com', | ||
query_parameters: {'user_id' => '7'} # arbitrary string | ||
) | ||
} | ||
|
||
expected_links = { | ||
first: 'www.example.com?user_id=7&cursor[before]', | ||
last: 'www.example.com?user_id=7&cursor[after]', | ||
next: "www.example.com?user_id=7&cursor[before]=#{cursor - @per_page}", | ||
prev: "www.example.com?user_id=7&cursor[after]=#{cursor - 1}", | ||
self: "www.example.com?user_id=7&cursor[before]=#{cursor}" | ||
} | ||
|
||
response_links = JsonApi::PaginationLinks.new(serialized_object, options).as_json | ||
expect(response_links).to eq(expected_links) | ||
end | ||
end | ||
|
||
context 'cursor is the max id of serialized collection' do | ||
it_behaves_like 'it returns the correct links' do | ||
let(:cursor) { 10 } | ||
end | ||
end | ||
|
||
context 'cursor is less than the max id of serialized collection' do | ||
it_behaves_like 'it returns the correct links' do | ||
let(:cursor) { 18 } | ||
end | ||
end | ||
|
||
context 'empty serialized object' do | ||
it 'returns an empty links hash' do | ||
serialized_object = User.cursor(before: 1) | ||
options = { | ||
serialization_context: OpenStruct.new( | ||
request_url: 'www.example.com', | ||
query_parameters: {'user_id' => '7'} | ||
) | ||
} | ||
response_links = JsonApi::PaginationLinks.new(serialized_object, options).as_json | ||
expect(response_links).to eq({}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters