Class: MaintenanceTasks::RunsPage Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • runs (ActiveRecord::Relation<MaintenanceTasks::Run>)

    the relation of Run records to be paginated.

  • cursor (String, nil)

    the id that serves as the cursor when querying the Runs dataset to produce a page of Runs. If nil, the first Runs in the relation are used.



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

#cursorString? (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.

Returns:

  • (String, nil)

    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.

Returns:

  • (Boolean)

    whether this Page contains the last Run record in the Runs dataset that is being paginated. This is done by checking whether an extra Run was loaded by #records - if no extra Run was loaded, this is the last page.



60
61
62
63
# File 'app/models/maintenance_tasks/runs_page.rb', line 60

def last?
  records
  @extra_run.nil?
end

#next_cursorInteger

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.

Returns:

  • (Integer)

    the id of the last record for the Page.



51
52
53
# File 'app/models/maintenance_tasks/runs_page.rb', line 51

def next_cursor
  records.last.id
end

#recordsActiveRecord::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.

Returns:



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