Skip to content

Commit

Permalink
Merge pull request #833 from datacite/pagination_fix
Browse files Browse the repository at this point in the history
Pagination fix
  • Loading branch information
digitaldogsbody authored Jul 4, 2022
2 parents 3d73de9 + 4c84737 commit fe6b4ca
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if results.size < page[:size] || page[:size] == 0
if results.size < page[:size] || page[:size] == 0 || page[:number] == total_pages
nil
else
request.base_url + "/activities?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/client_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if client_prefixes.blank?
if client_prefixes.blank? || page[:number] == total_pages
nil
else
request.base_url + "/client-prefixes?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @clients.blank?
if @clients.blank? || page[:number] == total_pages
nil
else
request.base_url + "/clients?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @contacts.blank?
if @contacts.blank? || page[:number] == total_pages
nil
else
request.base_url + "/contacts?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/data_centers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @clients.blank?
if @clients.blank? || page[:number] == total_pages
nil
else
request.base_url + "/data-centers?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if results.size < page[:size] || page[:size] == 0
if results.size < page[:size] || page[:size] == 0 || page[:number] == total_pages
nil
else
request.base_url + "/dois?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if results.size < page[:size] || page[:size] == 0
if results.size < page[:size] || page[:size] == 0 || page[:number] == total_pages
nil
else
request.base_url + "/events?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @media.blank?
if @media.blank? || page[:number] == total_pages
nil
else
request.base_url + "/media?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @members.blank?
if @members.blank? || page[:number] == total_pages
nil
else
request.base_url + "/members?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/metadata_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @metadata.blank?
if @metadata.blank? || page[:number] == total_pages
nil
else
request.base_url + "/media?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @providers.blank?
if @providers.blank? || page[:number] == total_pages
nil
else
request.base_url + "/providers?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if prefixes.blank?
if prefixes.blank? || page[:number] == total_pages
nil
else
request.base_url + "/prefixes?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/provider_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if provider_prefixes.blank?
if provider_prefixes.blank? || page[:number] == total_pages
nil
else
request.base_url + "/provider-prefixes?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if @providers.blank?
if @providers.blank? || page[:number] == total_pages
nil
else
request.base_url + "/providers?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if response.results.blank?
if response.results.blank? || page[:number] == total_pages
nil
else
request.base_url + "/repositories?" +
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/repository_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def index
options[:links] = {
self: request.original_url,
next:
if repository_prefixes.blank?
if repository_prefixes.blank? || page[:number] == total_pages
nil
else
request.base_url + "/repository-prefixes?" +
Expand Down
28 changes: 28 additions & 0 deletions spec/requests/datacite_dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@
expect(json.dig("links", "next")).to be_nil
end

it "returns correct page links when results is exactly divisible by page size" do
get "/dois?page[number]=1&page[size]=5", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(5)
expect(json.dig("meta", "total")).to eq(10)
next_link_absolute = Addressable::URI.parse(json.dig("links", "next"))
next_link = next_link_absolute.path + "?" + next_link_absolute.query
expect(next_link).to eq("/dois?page%5Bnumber%5D=2&page%5Bsize%5D=5")

get next_link, nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(5)
expect(json.dig("meta", "total")).to eq(10)
expect(json.dig("links", "next")).to be_nil
end

it "returns a blank resultset when page is above max page" do
get "/dois?page[number]=3&page[size]=5", nil, headers

expect(last_response.status).to eq(200)
expect(json["data"].size).to eq(0)
expect(json.dig("meta", "totalPages")).to eq(2)
expect(json.dig("meta", "page")).to eq(3)
expect(json.dig("links", "next")).to be_nil
end

it "returns dois with cursor" do
get "/dois?page[cursor]&page[size]=4", nil, headers

Expand Down
5 changes: 1 addition & 4 deletions spec/requests/provider_prefixes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@
get "/provider-prefixes", nil, headers
self_link_absolute = Addressable::URI.parse(json.dig("links", "self"))
expect(self_link_absolute.path).to eq("/provider-prefixes")

next_link_absolute = Addressable::URI.parse(json.dig("links", "next"))
next_link = next_link_absolute.path + "?" + next_link_absolute.query
expect(next_link).to eq("/provider-prefixes?page%5Bnumber%5D=2&page%5Bsize%5D=25")
expect(json.dig("links", "next")).to be_nil
end
end

Expand Down

0 comments on commit fe6b4ca

Please sign in to comment.