Skip to content

Commit

Permalink
Improve pagination queries
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianna-chang-shopify committed Mar 17, 2021
1 parent b1cfac3 commit 7d13c0f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/models/maintenance_tasks/runs_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def initialize(runs, cursor)
# @return [ActiveRecord::Relation<MaintenanceTasks::Run>] a limited amount
# of Run records.
def records
runs_after_cursor = if @cursor.present?
@runs.where('id < ?', @cursor)
else
@runs
@records ||= begin
runs_after_cursor = if @cursor.present?
@runs.where('id < ?', @cursor)
else
@runs
end
runs_after_cursor.limit(RUNS_PER_PAGE)
end
runs_after_cursor.limit(RUNS_PER_PAGE)
end

# Returns the cursor to use for the next Page of Runs. It is the id of the
Expand All @@ -47,7 +49,7 @@ def next_cursor
# @return [Boolean] whether this Page contains the last Run record in the
# Runs dataset that is being paginated.
def last?
@runs.last == records.last
@runs.unscope(:includes).pluck(:id).last == next_cursor
end
end
end

0 comments on commit 7d13c0f

Please sign in to comment.