Class: RailsDbInfo::TableEntries

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_info/table_entries.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ TableEntries

Returns a new instance of TableEntries.



9
10
11
# File 'lib/rails_db_info/table_entries.rb', line 9

def initialize(table)
  @table = table
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



5
6
7
# File 'lib/rails_db_info/table_entries.rb', line 5

def current_page
  @current_page
end

#offsetObject

Returns the value of attribute offset.



5
6
7
# File 'lib/rails_db_info/table_entries.rb', line 5

def offset
  @offset
end

#per_pageObject

Returns the value of attribute per_page.



5
6
7
# File 'lib/rails_db_info/table_entries.rb', line 5

def per_page
  @per_page
end

#tableObject (readonly)

Returns the value of attribute table.



4
5
6
# File 'lib/rails_db_info/table_entries.rb', line 4

def table
  @table
end

Instance Method Details

#loadObject



13
14
15
# 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_pageObject



17
18
19
# File 'lib/rails_db_info/table_entries.rb', line 17

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

#paginate(options = {}) ⇒ Object



21
22
23
24
25
26
# 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_pageObject



28
29
30
# File 'lib/rails_db_info/table_entries.rb', line 28

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

#total_entriesObject



32
33
34
# File 'lib/rails_db_info/table_entries.rb', line 32

def total_entries
  @total_entries ||= count
end

#total_pagesObject



36
37
38
# 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