Class: BentoSearch::Results::Pagination
- Inherits:
-
Object
- Object
- BentoSearch::Results::Pagination
- 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
-
#count_records ⇒ Object
(also: #count, #total_count)
If nil is passed in, will normalize to 0 so things doing math comparisons won’t raise.
- #current_page ⇒ Object
-
#end_record ⇒ Object
1-based last record in window, suitable for showing to user.
- #first_page? ⇒ Boolean
-
#initialize(total, normalized_args) ⇒ Pagination
constructor
first arg is results.total_items, second is result of normalized_args from searchresults.
- #last_page? ⇒ Boolean
-
#offset_value ⇒ Object
Recent kaminari’s have an offset_value which is 0-based, unlike our 1-based @start_record.
- #per_page ⇒ Object (also: #limit_value)
-
#start_record ⇒ Object
1-based start, suitable for showing to user Can be 0 for empty result set.
- #total_pages ⇒ Object (also: #num_pages)
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_records ⇒ Object 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_page ⇒ Object
29 30 31 |
# File 'app/models/bento_search/results/pagination.rb', line 29 def current_page @current_page end |
#end_record ⇒ Object
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
61 62 63 |
# File 'app/models/bento_search/results/pagination.rb', line 61 def first_page? current_page == 1 end |
#last_page? ⇒ Boolean
65 66 67 |
# File 'app/models/bento_search/results/pagination.rb', line 65 def last_page? current_page >= total_pages end |
#offset_value ⇒ Object
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_page ⇒ Object 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_record ⇒ Object
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_pages ⇒ Object 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 |