Skip to content

Instantly share code, notes, and snippets.

@scalp42
Forked from soveran/paginate.rb
Created January 2, 2017 10:06
Show Gist options
  • Save scalp42/7187c36c00ee9f983b6be7eabc800c0b to your computer and use it in GitHub Desktop.
Save scalp42/7187c36c00ee9f983b6be7eabc800c0b to your computer and use it in GitHub Desktop.
Pagination helper for Ohm models.
def paginate(collection, options = {})
start = options[:start] || 0
limit = options[:limit] || 0
order = options[:order]
rest = collection.size > (start + limit)
page = collection.sort(order: order, start: start, limit: limit)
[rest, page]
end
# Let's say there are 40 users.
User.all.size # => 40
# We can paginate them with this expression.
remnant, discs = paginate(User.all, order: "DESC", limit: 10)
# Now, discs has a collection of instances of User, and remnant is a
# boolean that states whether there are more pages to display.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment