Class: BentoSearch::Results::Pagination

Inherits:
Object
  • Object
show all
Defined in:
app/models/bento_search/results/pagination.rb

Overview

An object intended to be compatible with kaminari for pagination, although kaminari doesn’t doc/spec exactly what you need, so might break in future. Could be useful for your own custom pagination too.

You don’t normally create one of these yourself, you get one returned from Results#pagination

<%= paginate @results.pagination %>

Instance Method Summary collapse

Constructor Details

#initialize(total, normalized_args) ⇒ Pagination

first arg is results.total_items, second is result of normalized_args from searchresults.

We don’t do the page/start normalization calc here, we count on them both being passed in, already calculated by normalize_arguments in SearchResults. Expects :page, 0-based :start, and 1-based :per_page to all be passed in in initializer.



20
21
22
23
24
25
26
27
# File 'app/models/bento_search/results/pagination.rb', line 20

def initialize(total, normalized_args)
  normalized_args ||= {} # in some error cases, we end up with nil
  
  @total_count = total || 0
  @per_page = normalized_args[:per_page] || 10
  @current_page = normalized_args[:page]  || 1
  @start_record = (normalized_args[:start] || 0) + 1 
end

Instance Method Details

#count_recordsObject Also known as: count, total_count

If nil is passed in, will normalize to 0 so things doing math comparisons won’t raise.



47
48
49
# File 'app/models/bento_search/results/pagination.rb', line 47

def count_records
  @total_count
end

#current_pageObject



29
30
31
# File 'app/models/bento_search/results/pagination.rb', line 29

def current_page
  @current_page
end

#end_recordObject

1-based last record in window, suitable for showing to user. Can be 0 for empty result set.



41
42
43
# File 'app/models/bento_search/results/pagination.rb', line 41

def end_record
  [start_record + per_page - 1, count_records].min
end

#first_page?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/bento_search/results/pagination.rb', line 61

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/bento_search/results/pagination.rb', line 65

def last_page?
  current_page >= total_pages
end

#offset_valueObject

Recent kaminari’s have an offset_value which is 0-based, unlike our 1-based @start_record. Le Sigh.



77
78
79
# File 'app/models/bento_search/results/pagination.rb', line 77

def offset_value
  @start_record - 1
end

#per_pageObject Also known as: limit_value



69
70
71
# File 'app/models/bento_search/results/pagination.rb', line 69

def per_page
  @per_page
end

#start_recordObject

1-based start, suitable for showing to user Can be 0 for empty result set.



35
36
37
# File 'app/models/bento_search/results/pagination.rb', line 35

def start_record
  [@start_record, count_records].min
end

#total_pagesObject Also known as: num_pages



54
55
56
# File 'app/models/bento_search/results/pagination.rb', line 54

def total_pages
  (@total_count.to_f / @per_page).ceil
end