Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#paginate(rows, current_page, total_count, per_page = 10, params = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/application_helper.rb', line 2

def paginate(rows, current_page, total_count, per_page = 10, params = {})

  if per_page.is_a?(Hash)
    params = per_page
    per_page = 10
  end

  window      = 2
  total_pages = (total_count.to_f / per_page.to_f).ceil
  last_page   = total_pages - 1

  left  = (current_page - 1).downto(1).to_a.first(window).sort
  right = (current_page + 1).upto(total_pages).to_a.first(window)

  pages = 1.upto(current_page - 1).to_a.map{|p| left.include?(p) || p == 1 ? p : '...'}.uniq + [current_page] + (current_page + 1).upto(last_page).to_a.map{|p| right.include?(p) || p == last_page ? p : '...'}.uniq

   :div, :class => 'pagination' do
    raw pages.map{|page| page.eql?('...') ? (:span, page) : link_to(page, admin_path(params.merge({:page => page})), :class => page == current_page ? 'current' : nil)}.join(' ')
  end
end