Skip to content

Instantly share code, notes, and snippets.

@matiasleidemer
Forked from be9/paginate.rb
Last active January 8, 2018 14:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiasleidemer/b59be0703c666e16d44b34732e742b03 to your computer and use it in GitHub Desktop.
Save matiasleidemer/b59be0703c666e16d44b34732e742b03 to your computer and use it in GitHub Desktop.
kaminari + JSON API pagination helper
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.total_pages, collection.limit_value
render json: [{
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment