Class: ActiverecordCursorPagination::CursorScope

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_cursor_pagination/cursor_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, scope, cursor, per: 15) ⇒ CursorScope

Returns a new instance of CursorScope.

Examples:

Empty cursor

Posts.where(published: true).order(created_at: :desc).cursor(nil, per: 100)
Posts.where(published: true).order(created_at: :desc).cursor("", per: 100)
Posts.where(published: true).order(created_at: :desc).cursor(EmptyCursor.new, per: 100)

Serialized cursor

Posts.where(published: true).order(created_at: :desc).cursor("SerializedCursorString", per: 100)

Record cursor

Posts.where(published: true).order(created_at: :desc).cursor(Post.find!(6), per: 100)

Cursor

cursor = ...deserialized cursor...
Posts.where(published: true).order(created_at: :desc).cursor(cursor, per: 100)

Parameters:

  • klass (Class)

    Model class

  • scope (ActiveRecord::Relation)

    The database query.

  • cursor (String, Cursor, EmptyCursor, ActiveRecord::Base, nil)

    The current page cursor.

  • [Integer] (Hash)

    a customizable set of options

Raises:

  • (InvalidCursorError)

    When the cursor is does not match the query or the cursor is not a valid type.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 27

def initialize(klass, scope, cursor, per: 15)
  @scope = scope.except :offset, :limit
  @klass = klass
  @per_page = per
  @table = @scope.table_name
  @id_column = "#{@table}.id"

  initialize_order_columns
  initialize_cursor cursor
  initialize_order_column_values
end

Instance Attribute Details

#per_pageObject (readonly)

Returns the value of attribute per_page.



3
4
5
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 3

def per_page
  @per_page
end

Instance Method Details

#any?Boolean

Get if there are records in the current page.

Returns:

  • (Boolean)

    True if not empty.



118
119
120
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 118

def any?
  !empty?
end

#current_cursorString

Get the current cursor

Returns:

  • (String)

    The current cursor



176
177
178
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 176

def current_cursor
  @cursor.to_param
end

#each {|ActiveRecord::Base| ... } ⇒ Object

Iterate each record in the current page.

Yields:

  • (ActiveRecord::Base)

    Invokes the block with each active record.



226
227
228
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 226

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

#each_with_index {|ActiveRecord::Base, Integer| ... } ⇒ Object

Iterate each record in the current page.

Yields:

  • (ActiveRecord::Base, Integer)

    Invokes the block with each active record and page row index.



234
235
236
237
238
239
240
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 234

def each_with_index(&block)
  i = 0
  current_page_scope.each do |r|
    block.call r, i
    i += 1
  end
end

#empty?Boolean Also known as: none?

Get if there no records in the current page.

Returns:

  • (Boolean)

    True if there are no records.



108
109
110
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 108

def empty?
  size.zero?
end

#first_page?Boolean

Get if the cursor is the first page

Returns:

  • (Boolean)

    True if first page



160
161
162
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 160

def first_page?
  scope_empty? || !previous_page?
end

#last_page?Boolean

Get if the cursor is the last page

Returns:

  • (Boolean)

    True if last page



168
169
170
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 168

def last_page?
  scope_empty? || !next_page?
end

#many?Boolean

Get if there are many records in the current page.

Returns:

  • (Boolean)

    True if there is more than one record.



126
127
128
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 126

def many?
  size > 1
end

#map {|ActiveRecord::Base| ... } ⇒ Object

Map each record in the current page.

Yields:

  • (ActiveRecord::Base)

    Invokes the block with each active record.



246
247
248
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 246

def map(&block)
  current_page_scope.map &block
end

#map_with_index {|ActiveRecord::Base, Integer| ... } ⇒ Object

Map each record in the current page.

Yields:

  • (ActiveRecord::Base, Integer)

    Invokes the block with each active record and page row index.



254
255
256
257
258
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 254

def map_with_index(&block)
  current_page_scope.map.with_index do |r, i|
    block.call r, i
  end
end

#next_cursorString

Get the next page cursor

Returns:

  • (String)


184
185
186
187
188
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 184

def next_cursor
  return EmptyCursor.to_param unless next_page?
  ids = next_page_scope.pluck(:id)
  Cursor.to_param @klass, @scope, @per_page, ids.first, ids.last
end

#next_cursor_recordActiveRecord::Base

Get the next record

Returns:

  • (ActiveRecord::Base)

Raises:



196
197
198
199
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 196

def next_cursor_record
  raise NotSingleRecordError unless single_record?
  next_page_scope.first if next_page?
end

#next_page?Boolean

Get if there is another page

Returns:

  • (Boolean)

    True if there is a next page



151
152
153
154
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 151

def next_page?
  return false if scope_empty?
  next_page_scope.any?
end

#one?Boolean

Get if there is only one in the current page.

Returns:

  • (Boolean)

    True if there is only one record.



134
135
136
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 134

def one?
  size == 1
end

#previous_cursorString

Get the previous page cursor.

Returns:

  • (String)


205
206
207
208
209
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 205

def previous_cursor
  return EmptyCursor.to_param unless previous_page?
  ids = previous_page_scope.pluck(:id)
  Cursor.to_param @klass, @scope, @per_page, ids.last, ids.first
end

#previous_cursor_recordActiveRecord::Base

Get the previous record.

Returns:

  • (ActiveRecord::Base)

Raises:



217
218
219
220
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 217

def previous_cursor_record
  raise NotSingleRecordError unless single_record?
  previous_page_scope.first if previous_page?
end

#previous_page?Boolean

Get if there is a previous page from the cursor

Returns:

  • (Boolean)

    True if there is previous page



142
143
144
145
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 142

def previous_page?
  return false if scope_empty?
  previous_page_scope.any?
end

#scope_any?Boolean

Get if there are records from the query scope.

Returns:

  • (Boolean)

    True if the query scope is not empty.



73
74
75
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 73

def scope_any?
  !scope_empty?
end

#scope_empty?Boolean Also known as: scope_none?

Get if there are not records from the query scope.

Returns:

  • (Boolean)

    True if the query scope is empty.



63
64
65
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 63

def scope_empty?
  total_count.zero?
end

#scope_many?Boolean

Get if there are many records from the query scope.

Returns:

  • (Boolean)

    True if there is more than one record.



89
90
91
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 89

def scope_many?
  total_count > 1
end

#scope_one?Boolean

Get if there is only one record from the query scope.

Returns:

  • (Boolean)

    True if there is only one record.



81
82
83
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 81

def scope_one?
  total_count == 1
end

#scope_sizeInteger Also known as: scope_count, total_count, total

Get the total count of records from the query scope.

Returns:

  • (Integer)

    The number of total records.



51
52
53
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 51

def scope_size
  @scope.except(:select).size
end

#single_record?Boolean

Get if the query is for single records

Returns:

  • (Boolean)

    True if only one record per page



43
44
45
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 43

def single_record?
  @per_page === 1
end

#sizeInteger Also known as: count, length

Get the number of records in the current page

Returns:

  • (Integer)

    The number of records



97
98
99
# File 'lib/activerecord_cursor_pagination/cursor_scope.rb', line 97

def size
  current_page_scope.except(:select).size
end