Guest User

Untitled

a guest
Apr 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. module Sequel
  2. class Dataset
  3. # Returns a paginated dataset. The resulting dataset also provides the
  4. # total number of pages (Dataset#page_count) and the current page number
  5. # (Dataset#current_page), as well as Dataset#prev_page and Dataset#next_page
  6. # for implementing pagination controls.
  7. def paginate(page_no, page_size)
  8. raise(Error, "You cannot paginate a dataset that already has a limit") if @opts[:limit]
  9. record_count = count
  10. total_pages = record_count.zero? ? 1 : (record_count / page_size.to_f).ceil
  11. raise(Error, "page_no must be > 0") if page_no < 1
  12. paginated = limit(page_size, (page_no - 1) * page_size)
  13. paginated.extend(Pagination)
  14. paginated.set_pagination_info(page_no, page_size, record_count)
  15. paginated
  16. end
  17.  
  18. module Pagination
  19. # Returns true if this page is the first page
  20. def first_page?
  21. @current_page == 1
  22. end
  23.  
  24. # Returns true if this page is the last page
  25. def last_page?
  26. @current_page == @page_count
  27. end
  28.  
  29. # Sets the pagination info
  30. def set_pagination_info(page_no, page_size, record_count)
  31. @current_page = page_no
  32. @page_size = page_size
  33. @pagination_record_count = record_count
  34. @page_count = record_count.zero? ? 1 : (record_count / page_size.to_f).ceil
  35. end
  36. end
  37. end
  38. end
Add Comment
Please, Sign In to add comment