class RailsDbInfo::TableEntries

Attributes

current_page[RW]
offset[RW]
per_page[RW]
table[R]

Public Class Methods

new(table) click to toggle source
# File lib/rails_db_info/table_entries.rb, line 9
def initialize(table)
  @table = table
end

Public Instance Methods

load() click to toggle source
# File lib/rails_db_info/table_entries.rb, line 13
def load
  connection.exec_query("SELECT * FROM #{table.name} LIMIT #{per_page} OFFSET #{offset}")
end
next_page() click to toggle source
# File lib/rails_db_info/table_entries.rb, line 17
def next_page
  current_page < total_pages ? (current_page + 1) : nil
end
paginate(options = {}) click to toggle source
# File lib/rails_db_info/table_entries.rb, line 21
def paginate(options = {})
  self.per_page = 10
  self.current_page = (options[:page] || 1).to_i
  self.offset = current_page * per_page - per_page
  self
end
previous_page() click to toggle source
# File lib/rails_db_info/table_entries.rb, line 28
def previous_page
  current_page > 1 ? (current_page - 1) : nil
end
total_entries() click to toggle source
# File lib/rails_db_info/table_entries.rb, line 32
def total_entries
  @total_entries ||= count
end
total_pages() click to toggle source
# File lib/rails_db_info/table_entries.rb, line 36
def total_pages
  total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
end

Private Instance Methods

connection() click to toggle source
# File lib/rails_db_info/table_entries.rb, line 47
def connection
  ActiveRecord::Base.connection
end
count() click to toggle source
# File lib/rails_db_info/table_entries.rb, line 43
def count
  connection.exec_query("SELECT COUNT(*) FROM #{table.name}").rows.flatten.last.to_i
end