Class: JobBoard::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/job_board/page.rb

Overview

Keyset pagination over a relation ordered by id DESC. Fetches limit + 1 records to detect whether an older page exists.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, before: nil, limit: 25, &row_builder) ⇒ Page

Returns a new instance of Page.



9
10
11
12
13
14
15
# File 'app/models/job_board/page.rb', line 9

def initialize(relation, before: nil, limit: 25, &row_builder)
  relation = relation.where(id: ...before.to_i) if before.present?
  fetched = relation.limit(limit + 1).to_a
  @more = fetched.size > limit
  @records = fetched.first(limit)
  @rows = row_builder ? @records.map(&row_builder) : @records
end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



7
8
9
# File 'app/models/job_board/page.rb', line 7

def records
  @records
end

#rowsObject (readonly)

Returns the value of attribute rows.



7
8
9
# File 'app/models/job_board/page.rb', line 7

def rows
  @rows
end

Instance Method Details

#each(&block) ⇒ Object



27
28
29
# File 'app/models/job_board/page.rb', line 27

def each(&block)
  rows.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/job_board/page.rb', line 31

def empty?
  records.empty?
end

#more?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/models/job_board/page.rb', line 17

def more?
  @more
end

#next_beforeObject

Keyset cursor for the next (older) page — the id of the raw record, which is an execution id for execution-backed lists and a job id for finished.



23
24
25
# File 'app/models/job_board/page.rb', line 23

def next_before
  records.last&.id
end