diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b15ec3..f0a453f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,11 @@ 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". ---> +## [0.5.0] - 2025-05-02 + +### Changed +- Removed a redundant check for the total amount of items in order to determine if there is a next or previous page. This gets rid of two `select count(*)` queries that were being executed, speeding up the pagination process. + ## [0.4.0] - 2023-10-06 ### Changed diff --git a/lib/rails_cursor_pagination/paginator.rb b/lib/rails_cursor_pagination/paginator.rb index 99806c4..1c3bb85 100644 --- a/lib/rails_cursor_pagination/paginator.rb +++ b/lib/rails_cursor_pagination/paginator.rb @@ -216,11 +216,10 @@ def custom_order_field? # @return [TrueClass, FalseClass] def previous_page? if paginate_forward? - # When paginating forward, we can only have a previous page if we were - # provided with a cursor and there were records discarded after applying - # this filter. These records would have to be on previous pages. - @cursor.present? && - filtered_and_sorted_relation.reorder('').size < total + # When paginating forward, if a cursor is present it must mean we have + # come from a previous page that has provided us a cursor. Therefore, we + # can assume that there is a previous page. + @cursor.present? else # When paginating backwards, if we managed to load one more record than # requested, this record will be available on the previous page. @@ -237,10 +236,10 @@ def next_page? # requested, this record will be available on the next page. records_plus_one.size > @page_size else - # When paginating backward, if applying our cursor reduced the number - # records returned, we know that the missing records will be on - # subsequent pages. - filtered_and_sorted_relation.reorder('').size < total + # When paginating backward, if a cursor is present it must mean we have + # come from a next page that has provided us a cursor. Therefore, we + # can assume that there is a next page. + @cursor.present? end end