Class: Argus::Trail::Pagination::SimplePage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/argus/trail/pagination.rb

Overview

Minimal stand-in for a Kaminari page, used only when the host app hasn't bundled Kaminari. Implements just enough of Kaminari's relation interface for the engine's views (current_page, total_pages, total_count, each).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, page:, per:) ⇒ SimplePage

Returns a new instance of SimplePage.



12
13
14
15
16
17
# File 'lib/argus/trail/pagination.rb', line 12

def initialize(relation, page:, per:)
  @current_page = [ page.to_i, 1 ].max
  @per_page     = per
  @total_count  = relation.count
  @records      = relation.offset((@current_page - 1) * per).limit(per).to_a
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



10
11
12
# File 'lib/argus/trail/pagination.rb', line 10

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



10
11
12
# File 'lib/argus/trail/pagination.rb', line 10

def per_page
  @per_page
end

#total_countObject (readonly)

Returns the value of attribute total_count.



10
11
12
# File 'lib/argus/trail/pagination.rb', line 10

def total_count
  @total_count
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


20
# File 'lib/argus/trail/pagination.rb', line 20

def any? = @records.any?

#each(&block) ⇒ Object



19
# File 'lib/argus/trail/pagination.rb', line 19

def each(&block) = @records.each(&block)

#empty?Boolean

Returns:

  • (Boolean)


21
# File 'lib/argus/trail/pagination.rb', line 21

def empty? = @records.empty?

#sizeObject



22
# File 'lib/argus/trail/pagination.rb', line 22

def size = @records.size

#total_pagesObject



24
25
26
27
# File 'lib/argus/trail/pagination.rb', line 24

def total_pages
  return 0 if per_page.zero?
  (total_count.to_f / per_page).ceil
end