Class: MaintenanceTasks::RunsPage Private
- Inherits:
-
Object
- Object
- MaintenanceTasks::RunsPage
- Defined in:
- app/models/maintenance_tasks/runs_page.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class is responsible for handling cursor-based pagination for Run records.
Constant Summary collapse
- RUNS_PER_PAGE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The number of Runs to show on a single Task page.
20
Instance Attribute Summary collapse
-
#cursor ⇒ String?
readonly
private
The cursor for the page of Runs.
Instance Method Summary collapse
-
#initialize(runs, cursor) ⇒ RunsPage
constructor
private
Initializes a Runs Page with a Runs relation and a cursor.
-
#last? ⇒ Boolean
private
Returns whether this Page is the last one.
-
#next_cursor ⇒ Integer
private
Returns the cursor to use for the next Page of Runs.
-
#records ⇒ ActiveRecord::Relation<MaintenanceTasks::Run>
private
Returns the records for a Page, taking into account the cursor if one is present.
Constructor Details
#initialize(runs, cursor) ⇒ RunsPage
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes a Runs Page with a Runs relation and a cursor. This page is used by the views to render a set of Runs.
19 20 21 22 |
# File 'app/models/maintenance_tasks/runs_page.rb', line 19 def initialize(runs, cursor) @runs = runs @cursor = cursor end |
Instance Attribute Details
#cursor ⇒ String? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the cursor for the page of Runs.
25 26 27 |
# File 'app/models/maintenance_tasks/runs_page.rb', line 25 def cursor @cursor end |
Instance Method Details
#last? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether this Page is the last one.
60 61 62 63 |
# File 'app/models/maintenance_tasks/runs_page.rb', line 60 def last? records @extra_run.nil? end |
#next_cursor ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the cursor to use for the next Page of Runs. It is the id of the last record on the current Page.
51 52 53 |
# File 'app/models/maintenance_tasks/runs_page.rb', line 51 def next_cursor records.last.id end |
#records ⇒ ActiveRecord::Relation<MaintenanceTasks::Run>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the records for a Page, taking into account the cursor if one is present. Limits the number of records to 20.
An extra Run is loaded so that we can verify whether we’re on the last Page.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/maintenance_tasks/runs_page.rb', line 34 def records @records ||= begin runs_after_cursor = if @cursor.present? @runs.where("id < ?", @cursor) else @runs end limited_runs = runs_after_cursor.limit(RUNS_PER_PAGE + 1).load @extra_run = limited_runs.length > RUNS_PER_PAGE ? limited_runs.last : nil limited_runs.take(RUNS_PER_PAGE) end end |