Class: Qa::PaginationService
- Inherits:
-
Object
- Object
- Qa::PaginationService
- Defined in:
- app/services/qa/pagination_service.rb
Overview
Provide pagination processing used to respond to requests for paginated results.
Defaults for page_offset and page_limit: (see example responses under #build_response)
format == :json
-
if neither page_offset nor page_limit is passed in, then… (backward compatible)
-
returns results as an Array<Hash>
-
returns all results
-
page_offset is “1”
-
page_limit is total_num_found
-
-
if either page_offset or page_limit is passed in, then…
-
returns results as an Array<Hash>
-
returns a page of results
-
default for page_offset is “1”
-
default for page_limit is DEFAULT_PAGE_LIMIT (i.e., 10)
-
format == :jsonapi
-
response is always in the jsonapi format
-
results are always paginated
-
default for page_offset is “1”
-
default for page_limit is DEFAULT_PAGE_LIMIT (i.e., 10)
How page_offset is calculated for pagination links:
expected page boundaries
-
Expected page boundaries are always calculated starting from page_offset=1 and the current page_limit. The page boundaries will include the page_offsets that cover all results. For example, page_limit=10 with 36 results will have page boundaries 1, 11, 21, 31.
self url
-
The self url always has the page_offset for the current page, which defaults to 1 if not passed in.
first page url
-
The first page url always has page_offset=1.
last page url
-
The last page url always has page_offset equal to the last of the expected page boundaries regardless of the passed in page_offset. For the example where page_limit=10 with 36 results, the last page will always have page_offset=31.
prev page url
-
Previous’ page_offset is calculated from the passed in page_offset whether or not it is on an expected page boundary.
-
For prev, page_offset = passed in page_offset - page_limit || nil if calculated as < 1
-
when current page_offset (e.g. 1) is less than page_limit (e.g. 10), then page_offset for prev will be nil (e.g. 1 - 10 = -9 which is < 1)
-
when current page_offset is an expected page boundary (e.g. 21), then page_offset for prev will also be a page boundary (e.g. 21 - 10 = 11 which is an expected page boundary)
-
when current page_offset is not on an expected page boundary (e.g. 13), then page_offset for prev will not be on an expected page boundary (e.g. 13 - 10 = 3 which is not an expected page boundary)
-
next page url
-
Next’s page_offset is calculated from the passed in page_offset whether or not it is on an expected page boundary.
-
For next, page_offset = passed in page_offset + page_limit || nil if calculated > total number of results found
-
when current page_offset (e.g. 31) is greater than total number of results (e.g. 36), then page_offset for next will be nil (e.g. 31 + 10 = 41 which is > 36)
-
when current page_offset is an expected page boundary (e.g. 21), then page_offset for next will also be a page boundary (e.g. 21 + 10 = 31 which is an expected page boundary)
-
when current page_offset is not on an expected page boundary (e.g. 13), then page_offset for next will not be on an expected page boundary (e.g. 13 + 10 = 23 which is not an expected page boundary)
-
Constant Summary collapse
- DEFAULT_PAGE_LIMIT =
Default page_limit to use if not passed in with the request.
10
- ERROR_NOT_INTEGER =
Error code for page_limit and page_offset when the value is not an integer.
901
- ERROR_OUT_OF_RANGE_TOO_SMALL =
Error code for page_limit and page_offset when the value is below the acceptable range (e.g. < 1).
902
- ERROR_OUT_OF_RANGE_TOO_LARGE =
Error code for page_offset when the value is above the acceptable range (e.g. > total_num_found).
903
Instance Method Summary collapse
-
#build_response ⇒ Object
Json results, optionally limited to requested page and optionally formatted according to the JSON-API standard.
-
#initialize(request:, results:, format: :json) ⇒ PaginationService
constructor
A new instance of PaginationService.
Constructor Details
#initialize(request:, results:, format: :json) ⇒ PaginationService
Returns a new instance of PaginationService.
106 107 108 109 110 111 112 |
# File 'app/services/qa/pagination_service.rb', line 106 def initialize(request:, results:, format: :json) @request = request @results = results @requested_format = format @page_offset_error = false @page_limit_error = false end |
Instance Method Details
#build_response ⇒ Object
Returns json results, optionally limited to requested page and optionally formatted according to the JSON-API standard. The default is to return just the results for backward compatibility. See examples.
229 230 231 |
# File 'app/services/qa/pagination_service.rb', line 229 def build_response json_api? ? build_json_api_response : build_json_response end |