Skip to content

Commit

Permalink
Fix pagination. #181
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 10, 2019
1 parent 1935b14 commit 762e0db
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 90 deletions.
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class ApplicationController < ActionController::API
# include helper module for generating random DOI suffixes
include Helpable

# include helper module for pagination
include Paginatable

attr_accessor :current_user

# pass ability into serializer
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/client_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ def index
years = years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }
end

# pagination
page = params[:page].is_a?(Hash) ? params[:page] : {}
page[:number] = page[:number] && page[:number].to_i > 0 ? page[:number].to_i : 1
page[:size] = page[:size] && (1..1000).include?(page[:size].to_i) ? page[:size].to_i : 25
page = page_from_params(params)
total = collection.count

order = case params[:sort]
Expand Down
10 changes: 1 addition & 9 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ def index
else { "name.raw" => { order: 'asc' }}
end

page = params[:page].is_a?(Hash) ? params[:page] : {}
if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = page[:size] > 0 ? 10000/page[:size] : 1
else
page[:size] = 25
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1
page = page_from_params(params)

if params[:id].present?
response = Client.find_by_id(params[:id])
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/concerns/paginatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Paginatable
extend ActiveSupport::Concern

included do
# make sure page parameter is a hash with keys size, number and/or cursor
def page_from_params(params)
p = params.to_unsafe_h.dig(:page)

if p.is_a?(Hash)
page = {
size: p["size"],
number: p["number"],
cursor: p["cursor"]
}.compact
else
page = {}
end

if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = page[:size] > 0 ? 10000/page[:size] : 1
else
page[:size] = 25
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1

page
end
end
end
10 changes: 1 addition & 9 deletions app/controllers/data_centers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ def index
else { "name.raw" => { order: 'asc' }}
end

page = params[:page].is_a?(Hash) ? params[:page] : {}
if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = 1
else
page[:size] = 25
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1
page = page_from_params(params)

if params[:id].present?
response = Client.find_by_id(params[:id])
Expand Down
14 changes: 2 additions & 12 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,7 @@ def index
else { updated: { order: 'desc' }}
end

page = params[:page].is_a?(Hash) ? params[:page] : {}

if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = page[:size] > 0 ? 10000/page[:size] : 1
else
page[:size] = 25
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1
page = page_from_params(params)

sample_group_field = case params[:sample_group]
when "client" then "client_id"
Expand Down Expand Up @@ -146,7 +137,6 @@ def index
end

begin

# If we're using sample groups we need to unpack the results from the aggregation bucket hits.
if sample_group_field.present?
sample_dois = []
Expand Down Expand Up @@ -218,7 +208,7 @@ def index
"client-id" => params[:client_id],
fields: params[:fields],
"page[cursor]" => Array.wrap(@dois.last[:sort]).first,
"page[size]" => params.dig(:page, :size) }.compact.to_query
"page[size]" => page[:size] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ def index
collection = @doi.media
total = @doi.cached_media_count.reduce(0) { |sum, d| sum + d[:count].to_i }

page = params[:page].is_a?(Hash) ? params[:page] : {}
page[:number] = page[:number] && page[:number].to_i > 0 ? page[:number].to_i : 1
page[:size] = page[:size] && (1..1000).include?(page[:size].to_i) ? page[:size].to_i : 25
page = page_from_params(params)
total_pages = (total.to_f / page[:size]).ceil

order = case params[:sort]
Expand All @@ -32,8 +30,8 @@ def index
options[:links] = {
self: request.original_url,
next: @media.blank? ? nil : request.base_url + "/media?" + {
"page[number]" => params.dig(:page, :number).to_i + 1,
"page[size]" => params.dig(:page, :size),
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
Expand Down
16 changes: 4 additions & 12 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ def index
else { "name.raw" => { order: 'asc' }}
end

page = params[:page].is_a?(Hash) ? params[:page] : {}
if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = 1
else
page[:size] = 25
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1

page = page_from_params(params)

if params[:id].present?
response = Provider.find_by_id(params[:id])
elsif params[:ids].present?
Expand Down Expand Up @@ -59,8 +51,8 @@ def index
"organization-type" => params[:organization_type],
"focus-area" => params[:focus_area],
fields: params[:fields],
"page[number]" => params.dig(:page, :number),
"page[size]" => params.dig(:page, :size),
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: sort }.compact.to_query
}.compact
options[:include] = @include
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/metadata_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def index
collection = @doi.metadata
total = @doi.cached_metadata_count.reduce(0) { |sum, d| sum + d[:count].to_i }

page = params[:page].is_a?(Hash) ? params[:page] : {}
page[:number] = page[:number] && page[:number].to_i > 0 ? page[:number].to_i : 1
page[:size] = page[:size] && (1..1000).include?(page[:size].to_i) ? page[:size].to_i : 25
page = page_from_params(params)
total_pages = (total.to_f / page[:size]).ceil

order = case params[:sort]
Expand All @@ -35,8 +33,8 @@ def index
options[:links] = {
self: request.original_url,
next: @metadata.blank? ? nil : request.base_url + "/media?" + {
"page[number]" => params.dig(:page, :number).to_i + 1,
"page[size]" => params.dig(:page, :size),
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
Expand Down
9 changes: 3 additions & 6 deletions app/controllers/prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ def index
count: collection.state("with-client").count }]
end

# pagination
page = params[:page].is_a?(Hash) ? params[:page] : {}
page[:number] = page[:number] && page[:number].to_i > 0 ? page[:number].to_i : 1
page[:size] = page[:size] && (1..1000).include?(page[:size].to_i) ? page[:size].to_i : 25
page = page_from_params(params)
total = collection.count

order = case params[:sort]
Expand Down Expand Up @@ -98,8 +95,8 @@ def index
"provider-id" => params[:provider_id],
"client_id" => params[:client_id],
year: params[:year],
"page[number]" => params.dig(:page, :number).to_i + 1,
"page[size]" => params.dig(:page, :size),
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
Expand Down
11 changes: 4 additions & 7 deletions app/controllers/provider_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ def index
count: collection.state("with-client").count }]
end

# pagination
page = params[:page].is_a?(Hash) ? params[:page] : {}
page[:number] = page[:number] && page[:number].to_i > 0 ? page[:number].to_i : 1
page[:size] = page[:size] && (1..1000).include?(page[:size].to_i) ? page[:size].to_i : 25
page = page_from_params(params)
total = collection.count

order = case params[:sort]
Expand All @@ -84,7 +81,7 @@ def index
options[:meta] = {
total: total,
"totalPages" => @provider_prefixes.total_pages,
page: page[:number].to_i,
page: page[:number],
states: states,
providers: providers,
years: years
Expand All @@ -97,8 +94,8 @@ def index
"provider-id" => params[:provider_id],
"prefix-id" => params[:prefix_id],
year: params[:year],
"page[number]" => params.dig(:page, :number).to_i + 1,
"page[size]" => params.dig(:page, :size),
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
}.compact
options[:include] = @include
Expand Down
14 changes: 3 additions & 11 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ def index
else { "name.raw" => { order: 'asc' }}
end

page = params[:page].is_a?(Hash) ? params[:page] : {}
if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = page[:size] > 0 ? 10000/page[:size] : 1
else
page[:size] = 25
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1
page = page_from_params(params)

if params[:id].present?
response = Provider.find_by_id(params[:id])
Expand Down Expand Up @@ -63,8 +55,8 @@ def index
"organization_type" => params[:organization_type],
"focus-area" => params[:focus_area],
fields: params[:fields],
"page[number]" => params.dig(:page, :number),
"page[size]" => params.dig(:page, :size),
"page[number]" => page[:number] + 1,
"page[size]" => page[:size],
sort: sort }.compact.to_query
}.compact
options[:include] = @include
Expand Down
13 changes: 3 additions & 10 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ def index
else { updated: { order: 'desc' }}
end

page = params[:page].is_a?(Hash) ? params[:page] : {}
if page[:size].present?
page[:size] = [page[:size].to_i, 1000].min
max_number = page[:size] > 0 ? 10000/page[:size] : 1
else
page[:size] = 25
max_number = 10000/page[:size]
end
page[:number] = page[:number].to_i > 0 ? [page[:number].to_i, max_number].min : 1
page = page_from_params(params)

sample_group_field = case params[:sample_group]
when "client" then "client_id"
Expand Down Expand Up @@ -85,7 +77,8 @@ def index
query: params[:query],
"member-id" => params[:provider_id],
"data-center-id" => params[:client_id],
"page[size]" => params.dig(:page, :size) }.compact.to_query
"page[cursor]" => Array.wrap(@dois.last[:sort]).first,
"page[size]" => page[:size] }.compact.to_query
}.compact
options[:include] = @include
options[:is_collection] = true
Expand Down

0 comments on commit 762e0db

Please sign in to comment.