diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5a5dc755a..012746a9e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/client_prefixes_controller.rb b/app/controllers/client_prefixes_controller.rb index 19162656c..4d20b6943 100644 --- a/app/controllers/client_prefixes_controller.rb +++ b/app/controllers/client_prefixes_controller.rb @@ -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] diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 8bdaaf99d..fb932d237 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -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]) diff --git a/app/controllers/concerns/paginatable.rb b/app/controllers/concerns/paginatable.rb new file mode 100644 index 000000000..78b23a31f --- /dev/null +++ b/app/controllers/concerns/paginatable.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/data_centers_controller.rb b/app/controllers/data_centers_controller.rb index a1a9149e0..6cb147511 100644 --- a/app/controllers/data_centers_controller.rb +++ b/app/controllers/data_centers_controller.rb @@ -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]) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index f26619a6e..af134a7d7 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -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" @@ -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 = [] @@ -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 diff --git a/app/controllers/media_controller.rb b/app/controllers/media_controller.rb index 916c2e62c..85b4d4101 100644 --- a/app/controllers/media_controller.rb +++ b/app/controllers/media_controller.rb @@ -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] @@ -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 diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index 536597a56..f8fd1ffd6 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -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? @@ -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 diff --git a/app/controllers/metadata_controller.rb b/app/controllers/metadata_controller.rb index 5c3e56e0d..5fcbf7e43 100644 --- a/app/controllers/metadata_controller.rb +++ b/app/controllers/metadata_controller.rb @@ -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] @@ -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 diff --git a/app/controllers/prefixes_controller.rb b/app/controllers/prefixes_controller.rb index 5ea8c28f1..f9e608f1a 100644 --- a/app/controllers/prefixes_controller.rb +++ b/app/controllers/prefixes_controller.rb @@ -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] @@ -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 diff --git a/app/controllers/provider_prefixes_controller.rb b/app/controllers/provider_prefixes_controller.rb index 0b72f9977..233543c3b 100644 --- a/app/controllers/provider_prefixes_controller.rb +++ b/app/controllers/provider_prefixes_controller.rb @@ -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] @@ -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 @@ -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 diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index e1e8e3f0d..928e315af 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -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]) @@ -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 diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 52ff6211c..4ba538725 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -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" @@ -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