Skip to content

Instantly share code, notes, and snippets.

@be9
Created September 5, 2013 04:18
Show Gist options
  • Select an option

  • Save be9/6446051 to your computer and use it in GitHub Desktop.

Select an option

Save be9/6446051 to your computer and use it in GitHub Desktop.

Revisions

  1. be9 created this gist Sep 5, 2013.
    16 changes: 16 additions & 0 deletions paginate.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    def paginate(scope, default_per_page = 20)
    collection = scope.page(params[:page]).per((params[:per_page] || default_per_page).to_i)

    current, total, per_page = collection.current_page, collection.num_pages, collection.limit_value

    return [{
    pagination: {
    current: current,
    previous: (current > 1 ? (current - 1) : nil),
    next: (current == total ? nil : (current + 1)),
    per_page: per_page,
    pages: total,
    count: collection.total_count
    }
    }, collection]
    end