Class: RailsDbInfo::TableEntries
- Inherits:
-
Object
- Object
- RailsDbInfo::TableEntries
- Defined in:
- lib/rails_db_info/table_entries.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(table) ⇒ TableEntries
constructor
A new instance of TableEntries.
- #load ⇒ Object
- #next_page ⇒ Object
- #paginate(options = {}) ⇒ Object
- #previous_page ⇒ Object
- #total_entries ⇒ Object
- #total_pages ⇒ Object
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_page ⇒ Object
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 |
#offset ⇒ Object
Returns the value of attribute offset.
5 6 7 |
# File 'lib/rails_db_info/table_entries.rb', line 5 def offset @offset end |
#per_page ⇒ Object
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 |
#table ⇒ Object (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
#load ⇒ Object
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_page ⇒ Object
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( = {}) self.per_page = 10 self.current_page = ([:page] || 1).to_i self.offset = current_page * per_page - per_page self end |
#previous_page ⇒ Object
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_entries ⇒ Object
32 33 34 |
# File 'lib/rails_db_info/table_entries.rb', line 32 def total_entries @total_entries ||= count end |
#total_pages ⇒ Object
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 |