-
Notifications
You must be signed in to change notification settings - Fork 22
Avoid another (cached) query for next link #55
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
Conversation
Rather than issue another query for the next link, rely on the records already loaded
@@ -20,7 +20,11 @@ def from(scope) | |||
end | |||
|
|||
def next_param(scope) | |||
Cursor.encode page_number: page_number + 1, values: from(scope).last&.slice(*attributes) || {} | |||
if scope.order_values.none? && scope.limit_value.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scope argument can be a full set of records, or now just a portion and the method supports both.
This check supports the full set of records. Although we now never call this method with a full set of records internally to Geared Pagination, there's a test for it and I wasn't sure about how much of this should be considered a public API?
geared_pagination/test/portion_at_cursor_test.rb
Lines 55 to 68 in bfce103
test "next param" do | |
create_recordings | |
param = GearedPagination::PortionAtCursor.new(ordered_by: ORDERS).next_param(Recording.all) | |
assert_equal GearedPagination::Cursor.encode(page_number: 2, values: { number: 106, id: 106 }), param | |
param = GearedPagination::PortionAtCursor.new( | |
cursor: GearedPagination::Cursor.decode(param), ordered_by: ORDERS).next_param(Recording.all) | |
assert_equal GearedPagination::Cursor.encode(page_number: 3, values: { number: 76, id: 76 }), param | |
param = GearedPagination::PortionAtCursor.new( | |
cursor: GearedPagination::Cursor.decode(param), ordered_by: ORDERS).next_param(Recording.all) | |
assert_equal GearedPagination::Cursor.encode(page_number: 4, values: { number: 26, id: 26 }), param | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense thanks! Could extract a method for the check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed a3f9008 to avoid the check, by explicitly passing a portion
in a kwarg without breaking the existing API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Page#records
already returns the records for the current page, so#next_param
can use that to determine the last record for the next param, used in theLink
header.This is a performance optimization.
This avoids another query, albeit one that usually hits the Active Record Query Cache. The Query Cache is not always the most performant option.
cc @basecamp/sip