Module: RailsDb::TablePagination

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

Instance Method Summary collapse

Instance Method Details

#next_pageObject



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

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

#paginate(options = {}) ⇒ Object



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

def paginate(options = {})
  self.per_page     = (options[:per_page] || 10).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



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

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

#total_entriesObject



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

def total_entries
  @total_entries ||= count
end

#total_pagesObject



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

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