Module: Pagy::CountishPaginator

Defined in:
lib/pagy/toolbox/paginators/countish.rb

Class Method Summary collapse

Class Method Details

.paginate(collection, options) ⇒ Object

Return the Offset::Countish instance and records



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pagy/toolbox/paginators/countish.rb', line 10

def paginate(collection, options)
  options[:page] ||= options[:request].resolve_page(force_integer: false)

  if options[:page].is_a?(String)
    page, count, epoch = options[:page].split.map(&:to_i)
    options[:page]     = page
  end

  options[:limit] = options[:request].resolve_limit
  setup_options(count, epoch, collection, options)

  pagy = Offset::Countish.new(**options)
  [pagy, pagy.records(collection)]
end

.setup_options(count, epoch, collection, options) ⇒ Object

Get the count from the page and set epoch when ttl (Time To Live) requires it



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pagy/toolbox/paginators/countish.rb', line 26

def setup_options(count, epoch, collection, options)
  now     = Time.now.to_i
  ongoing = !options[:ttl] || (epoch && epoch <= now && now < (epoch + options[:ttl]))

  if !options[:count] && count && ongoing
    options[:count] = count
    options[:epoch] = epoch if options[:ttl]
  else # recount
    options[:count] ||= Countable.get_count(collection, options)
    options[:epoch]   = now if options[:ttl]
  end
end