Last active
February 26, 2018 07:32
-
-
Save soveran/5014455 to your computer and use it in GitHub Desktop.
Pagination helper for Ohm models.
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(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 |
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
# 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
i see that
Ohm::Utils.sort_options
has no keystart
to accept and limit should get an array.Therefore L7 should be:
page = collection.sort(order: order, limit: [start, limit])