Module: IndexedSearchHelper

Defined in:
app/helpers/indexed_search_helper.rb

Instance Method Summary collapse

Instance Method Details

#calculate_page_details(current_page, page_size, total_number) ⇒ Object

Shortcut to grab all page related calculations in one go. Returns an array of the index number of the first item on the page, the last item on the page, and the number of pages the total results occupy.



28
29
30
31
# File 'app/helpers/indexed_search_helper.rb', line 28

def calculate_page_details(current_page, page_size, total_number)
  return page_first(current_page, page_size), page_last(current_page, page_size, total_number),
      total_pages(page_size, total_number)
end

#page_first(current_page, page_size) ⇒ Object

Calculate the index number of the first item on the page, from current page number and number of items on a page. Suitable for a "first-last of total" display.



7
8
9
# File 'app/helpers/indexed_search_helper.rb', line 7

def page_first(current_page, page_size)
  (current_page - 1) * page_size + 1
end

#page_last(current_page, page_size, total_number) ⇒ Object

Calculate the index number of the last item on the page, from current page number, number of items on a page, and total number of items. Suitable for a "first-last of total" display



14
15
16
# File 'app/helpers/indexed_search_helper.rb', line 14

def page_last(current_page, page_size, total_number)
  [current_page * page_size, total_number].min
end

#total_pages(page_size, total_number) ⇒ Object

Calculate the number of pages the total results occupy, from the number of items on a page, and the total number of items. Useful for pagination navigation.



21
22
23
# File 'app/helpers/indexed_search_helper.rb', line 21

def total_pages(page_size, total_number)
  (1.0 * total_number / page_size).ceil
end