-
-
Save silviorelli/0c45dbc6daf227f6538ca86baa5cc694 to your computer and use it in GitHub Desktop.
kaminari + JSON API pagination helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment