Sha256: 1564c462546b1fd4a22f5dd683cc653f6cea68f62ebd9af8ab415c0aa8b8412b

Contents?: true

Size: 904 Bytes

Versions: 1

Compression:

Stored size: 904 Bytes

Contents

module ApplicationHelper
  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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cartoset-0.0.2 app/helpers/application_helper.rb