Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling previous and next page checks #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ These are the latest changes on the project's `master` branch that have not yet
Follow the same format as previous releases by categorizing your feature into "Added", "Changed", "Deprecated", "Removed", "Fixed", or "Security".
--->

### Added
- Add option to not include `has_previous_page` and `has_next_page` in the `page_info`

## [0.3.0] - 2022-07-08

### Added
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ If omitted, or set to `false`, the resulting hash will lack the `:total` key, bu
It is therefore recommended to only pass `with_total: true` when requested by the user.
So in the next examples we will also leave it away.

By default, the page_info also includes the 'has_previous_page' and 'has_next_page' keys. These are set to true if there's a previous or next page, respectively.
Both sometimes requires a count query to be executed, which can be expensive. If you want to disable this behavior, you can pass `check_next: false` and/or `check_previous: false`.

To then get the next result page, you simply need to pass the last cursor of the returned page item via:

```ruby
Expand Down
14 changes: 9 additions & 5 deletions lib/rails_cursor_pagination/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ def initialize(relation, limit: nil, first: nil, after: nil, last: nil,
# `total` of records across all pages.
#
# @param with_total [TrueClass, FalseClass]
# @param check_next [TrueClass, FalseClass]
# @param check_previous [TrueClass, FalseClass]
# @return [Hash] with keys :page, :page_info, and optional :total
def fetch(with_total: false)
def fetch(with_total: false, check_next: true, check_previous: true)
{
**(with_total ? { total: total } : {}),
page_info: page_info,
page_info: page_info(check_next, check_previous),
page: page
}
end
Expand Down Expand Up @@ -164,11 +166,13 @@ def ensure_valid_params_combinations!(first, last, limit, before, after)

# Get meta information about the current page
#
# @param check_next [TrueClass, FalseClass]
# @param check_previous [TrueClass, FalseClass]
# @return [Hash]
def page_info
def page_info(check_next, check_previous)
{
has_previous_page: previous_page?,
has_next_page: next_page?,
**(check_previous ? { has_previous_page: previous_page? } : {}),
**(check_next ? { has_next_page: next_page? } : {}),
start_cursor: start_cursor,
end_cursor: end_cursor
}
Expand Down
16 changes: 16 additions & 0 deletions spec/rails_cursor_pagination/paginator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,22 @@
expect(subject[:total]).to eq expected_total
end
end

context 'when passing `check_next: false`' do
subject(:result) { instance.fetch(check_next: false) }

it 'does not include the `has_next_page` in `page_info`' do
expect(subject[:page_info]).to_not have_key :has_next_page
end
end

context 'when passing `check_previous: false`' do
subject(:result) { instance.fetch(check_previous: false) }

it 'does not include the `has_previous_page` in `page_info`' do
expect(subject[:page_info]).to_not have_key :has_previous_page
end
end
end

shared_examples_for 'a well working query that also supports SELECT' do
Expand Down