Class: Typesense::Pagination::Kaminari

Inherits:
Kaminari::PaginatableArray
  • Object
show all
Defined in:
lib/typesense/pagination/kaminari.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, options) ⇒ Kaminari

Returns a new instance of Kaminari.



10
11
12
# File 'lib/typesense/pagination/kaminari.rb', line 10

def initialize(array, options)
  super(array, **options)
end

Class Method Details

.create(results, total_hits, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/typesense/pagination/kaminari.rb', line 25

def create(results, total_hits, options = {})
  offset = ((options[:page] - 1) * options[:per_page])
  array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
  if array.empty? && !results.empty?
    # since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
    # otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
    results = ([nil] * offset) + results
    array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
  end
  array
end

Instance Method Details

#limit(_num) ⇒ Object



14
15
16
17
# File 'lib/typesense/pagination/kaminari.rb', line 14

def limit(_num)
  # noop
  self
end

#offset(_num) ⇒ Object



19
20
21
22
# File 'lib/typesense/pagination/kaminari.rb', line 19

def offset(_num)
  # noop
  self
end