Module: RailsDb::TablePagination

Included in:
TableData
Defined in:
lib/rails_db/table_pagination.rb

Constant Summary collapse

DEFAULT_PAGINATION_PER_PAGE =
10

Instance Method Summary collapse

Instance Method Details

#next_pageObject



5
6
7
# File 'lib/rails_db/table_pagination.rb', line 5

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#paginate(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/rails_db/table_pagination.rb', line 9

def paginate(options = {})
  self.per_page     = (options[:per_page] || DEFAULT_PAGINATION_PER_PAGE).to_i
  self.current_page = (options[:page] || 1).to_i
  self.offset       = (current_page * per_page) - per_page
  self.sort_column  = options[:sort_column]
  self.sort_order   = options[:sort_order]
  table
end

#previous_pageObject



18
19
20
# File 'lib/rails_db/table_pagination.rb', line 18

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#total_entriesObject



22
23
24
# File 'lib/rails_db/table_pagination.rb', line 22

def total_entries
  @total_entries ||= count
end

#total_pagesObject



26
27
28
# File 'lib/rails_db/table_pagination.rb', line 26

def total_pages
  total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
end