Class: Mack::Database::Paginator
- Inherits:
-
Object
- Object
- Mack::Database::Paginator
- Defined in:
- lib/mack-orm/paginator.rb
Overview
This class provides a clean API for doing database pagination. The paginate methods needs to be implemented by the ORM developer.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
The current page in the pagination.
-
#klass ⇒ Object
The Class that all queries will be called on.
-
#options ⇒ Object
Options for the Paginator itself.
-
#query_options ⇒ Object
Options for the queries to be run.
-
#results ⇒ Object
The actual records themselves.
-
#results_per_page ⇒ Object
The number of results to be returned per page.
-
#total_pages ⇒ Object
The total pages available.
-
#total_results ⇒ Object
The total rows returned by the Paginator.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#end_index ⇒ Object
The ending index for this group of results.
-
#has_next? ⇒ Boolean
Is there a next page?.
-
#has_previous? ⇒ Boolean
Is there a previous page?.
-
#initialize(klass, options = {}, query_options = {}) ⇒ Paginator
constructor
Takes the Class that all queries will be run on, options for the Paginator, and options for the queries that are going to be run.
-
#paginate ⇒ Object
Implement this method in your ORM package.
-
#start_index ⇒ Object
The starting index for this group of results.
Constructor Details
#initialize(klass, options = {}, query_options = {}) ⇒ Paginator
Takes the Class that all queries will be run on, options for the Paginator, and options for the queries that are going to be run.
33 34 35 36 37 38 39 |
# File 'lib/mack-orm/paginator.rb', line 33 def initialize(klass, = {}, = {}) self.klass = klass self. = self. = self.current_page = (self..delete(:current_page) || 1).to_i self.results_per_page = (self..delete(:results_per_page) || configatron.mack.database.pagination.results_per_page).to_i end |
Instance Attribute Details
#current_page ⇒ Object
The current page in the pagination. Default is 1.
27 28 29 |
# File 'lib/mack-orm/paginator.rb', line 27 def current_page @current_page end |
#klass ⇒ Object
The Class that all queries will be called on
15 16 17 |
# File 'lib/mack-orm/paginator.rb', line 15 def klass @klass end |
#options ⇒ Object
Options for the Paginator itself
17 18 19 |
# File 'lib/mack-orm/paginator.rb', line 17 def @options end |
#query_options ⇒ Object
Options for the queries to be run
19 20 21 |
# File 'lib/mack-orm/paginator.rb', line 19 def @query_options end |
#results ⇒ Object
The actual records themselves
25 26 27 |
# File 'lib/mack-orm/paginator.rb', line 25 def results @results end |
#results_per_page ⇒ Object
The number of results to be returned per page.
29 30 31 |
# File 'lib/mack-orm/paginator.rb', line 29 def results_per_page @results_per_page end |
#total_pages ⇒ Object
The total pages available
23 24 25 |
# File 'lib/mack-orm/paginator.rb', line 23 def total_pages @total_pages end |
#total_results ⇒ Object
The total rows returned by the Paginator
21 22 23 |
# File 'lib/mack-orm/paginator.rb', line 21 def total_results @total_results end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
74 75 76 |
# File 'lib/mack-orm/paginator.rb', line 74 def ==(other) # :nodoc: self.results == other.results end |
#end_index ⇒ Object
The ending index for this group of results. Useful for building things like:
Displaying 11 - 20 of 56 results.
69 70 71 72 |
# File 'lib/mack-orm/paginator.rb', line 69 def end_index ei = self.current_page * self.results_per_page return (ei < self.total_results ? ei : self.total_results) end |
#has_next? ⇒ Boolean
Is there a next page?
48 49 50 |
# File 'lib/mack-orm/paginator.rb', line 48 def has_next? return self.current_page != self.total_pages && self.total_pages > 1 end |
#has_previous? ⇒ Boolean
Is there a previous page?
53 54 55 |
# File 'lib/mack-orm/paginator.rb', line 53 def has_previous? return self.current_page != 1 && self.total_pages > 1 end |
#paginate ⇒ Object
Implement this method in your ORM package. It should return self
and set the following accessors: total_results
, total_pages
, results
.
43 44 45 |
# File 'lib/mack-orm/paginator.rb', line 43 def paginate raise NoMethodError.new('paginate') end |
#start_index ⇒ Object
The starting index for this group of results. Useful for building things like:
Displaying 11 - 20 of 56 results.
60 61 62 63 64 |
# File 'lib/mack-orm/paginator.rb', line 60 def start_index return 0 if self.total_results == 0 si = ((self.current_page - 1) * self.results_per_page) return (si >= 0 ? si + 1 : 0) end |