module Rooble::ClassMethods

Public Instance Methods

pages(max_records_per_page=nil) click to toggle source

Returns the amount of available pages to do pagination.

# File lib/rooble.rb, line 28
def pages(max_records_per_page=nil)
  max_records_per_page ||= Rooble::configuration.max_records_per_page
  total_record_count = self.count
  return 0 unless total_record_count > 0
  pages = (total_record_count.to_f / max_records_per_page.to_f).ceil
end
paginate(page=1, max_records_per_page=nil, order_by: {id: :asc}) click to toggle source

Returns a set of paginated records given a page.

# File lib/rooble.rb, line 39
def paginate(page=1, max_records_per_page=nil, order_by: {id: :asc})
  page ||= 1

  if page.to_i < 0
    raise Rooble::Error.new "Pagination index must be greater than zero"
  end

  max_records_per_page ||= Rooble::configuration.max_records_per_page
  current_offset = ((page.to_i*max_records_per_page))-max_records_per_page
  records = self.limit(max_records_per_page)
                .offset(current_offset)
                .order(**order_by)
end