Skip to content

Instantly share code, notes, and snippets.

@soveran
Last active February 26, 2018 07:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soveran/5014455 to your computer and use it in GitHub Desktop.
Save soveran/5014455 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.
@alexey
Copy link

alexey commented Feb 26, 2018

i see that Ohm::Utils.sort_options has no key start to accept and limit should get an array.

Therefore L7 should be:
page = collection.sort(order: order, limit: [start, limit])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment