From 864950c983b660f2fca55b69284d2cadd0fc26fd Mon Sep 17 00:00:00 2001 From: kalle saas Date: Mon, 15 Feb 2021 17:08:21 +0100 Subject: [PATCH 1/4] always call jsonapi_page_size method on pagination enabled requests pass the per_page_param to the jsonapi_page_size method --- lib/jsonapi/pagination.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/jsonapi/pagination.rb b/lib/jsonapi/pagination.rb index 03ae22d..e4d7983 100644 --- a/lib/jsonapi/pagination.rb +++ b/lib/jsonapi/pagination.rb @@ -96,8 +96,7 @@ def jsonapi_pagination_meta(resources) # @return [Array] with the offset, limit and the current page number def jsonapi_pagination_params pagination = params[:page].try(:slice, :number, :size) || {} - per_page = pagination[:size].to_f.to_i - per_page = jsonapi_page_size if per_page < 1 + per_page = jsonapi_page_size(pagination[:size].to_i.to_f) num = [1, pagination[:number].to_f.to_i].max [(num - 1) * per_page, per_page, num] @@ -105,9 +104,13 @@ def jsonapi_pagination_params # Retrieves the default page size # + # @param per_page_param [Integer] per Page extracted from parmas hash + # # @return [Integer] - def jsonapi_page_size - self.class.const_get(:JSONAPI_PAGE_SIZE).to_i + def jsonapi_page_size(per_page_param) + per_page_param < 1 ? + self.class.const_get(:JSONAPI_PAGE_SIZE).to_i : + per_page_param end # Fallback to Rack's parsed query string when Rails is not available From bc49047f40d8a53bb1f31a6f4dbc43f399ebc2bf Mon Sep 17 00:00:00 2001 From: kalle saas Date: Tue, 16 Feb 2021 13:24:10 +0100 Subject: [PATCH 2/4] pass pagination to jsonapi_page_size and handle string-to-int conversion in jsonapi_page_size --- lib/jsonapi/pagination.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/jsonapi/pagination.rb b/lib/jsonapi/pagination.rb index e4d7983..b9afb97 100644 --- a/lib/jsonapi/pagination.rb +++ b/lib/jsonapi/pagination.rb @@ -96,7 +96,7 @@ def jsonapi_pagination_meta(resources) # @return [Array] with the offset, limit and the current page number def jsonapi_pagination_params pagination = params[:page].try(:slice, :number, :size) || {} - per_page = jsonapi_page_size(pagination[:size].to_i.to_f) + per_page = jsonapi_page_size(pagination) num = [1, pagination[:number].to_f.to_i].max [(num - 1) * per_page, per_page, num] @@ -104,13 +104,17 @@ def jsonapi_pagination_params # Retrieves the default page size # - # @param per_page_param [Integer] per Page extracted from parmas hash + # @param per_page_param [Hash] Page size extracted from parmas hash # # @return [Integer] - def jsonapi_page_size(per_page_param) - per_page_param < 1 ? - self.class.const_get(:JSONAPI_PAGE_SIZE).to_i : - per_page_param + def jsonapi_page_size(pagination_params) + per_page = pagination_params[:size].to_f.to_i + + return self.class + .const_get(:JSONAPI_PAGE_SIZE) + .to_i if per_page < 1 + + per_page end # Fallback to Rack's parsed query string when Rails is not available From c29a53dd352fb0abac8e039d088251e9cc4ee27e Mon Sep 17 00:00:00 2001 From: kalle saas Date: Tue, 16 Feb 2021 15:05:12 +0100 Subject: [PATCH 3/4] update readme to reflect changes --- README.md | 8 +++++--- lib/jsonapi/pagination.rb | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fe94452..8e3e243 100644 --- a/README.md +++ b/README.md @@ -308,12 +308,14 @@ use the `jsonapi_pagination_meta` method: ``` -If you want to change the default number of items per page, use the +If you want to change the default number of items per page, or define a custom logic to handle page size, use the `jsonapi_page_size` method: ```ruby - def jsonapi_page_size - 30 + def jsonapi_page_size(pagination_params) + per_page = pagination_params[:size].to_f.to_i + per_page = 30 if per_page > 30 + per_page end ``` ### Deserialization diff --git a/lib/jsonapi/pagination.rb b/lib/jsonapi/pagination.rb index b9afb97..a98d149 100644 --- a/lib/jsonapi/pagination.rb +++ b/lib/jsonapi/pagination.rb @@ -104,7 +104,9 @@ def jsonapi_pagination_params # Retrieves the default page size # - # @param per_page_param [Hash] Page size extracted from parmas hash + # @param per_page_param [Hash] opts the paginations params + # @option opts [String] :number the page number requested + # @option opts [String] :size the page size requested # # @return [Integer] def jsonapi_page_size(pagination_params) From 122e70f5cc16a768f37e0c2a50d0dcbc5e235b5e Mon Sep 17 00:00:00 2001 From: kalle saas Date: Tue, 16 Feb 2021 15:05:32 +0100 Subject: [PATCH 4/4] fix woring --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e3e243..5e86183 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,7 @@ use the `jsonapi_pagination_meta` method: ``` -If you want to change the default number of items per page, or define a custom logic to handle page size, use the +If you want to change the default number of items per page or define a custom logic to handle page size, use the `jsonapi_page_size` method: ```ruby