Class: Mack::Database::Paginator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {}, query_options = {})
  self.klass = klass
  self.options = options
  self.query_options = query_options
  self.current_page = (self.options.delete(:current_page) || 1).to_i
  self.results_per_page = (self.options.delete(:results_per_page) || configatron.mack.database.pagination.results_per_page).to_i
end

Instance Attribute Details

#current_pageObject

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

#klassObject

The Class that all queries will be called on



15
16
17
# File 'lib/mack-orm/paginator.rb', line 15

def klass
  @klass
end

#optionsObject

Options for the Paginator itself



17
18
19
# File 'lib/mack-orm/paginator.rb', line 17

def options
  @options
end

#query_optionsObject

Options for the queries to be run



19
20
21
# File 'lib/mack-orm/paginator.rb', line 19

def query_options
  @query_options
end

#resultsObject

The actual records themselves



25
26
27
# File 'lib/mack-orm/paginator.rb', line 25

def results
  @results
end

#results_per_pageObject

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_pagesObject

The total pages available



23
24
25
# File 'lib/mack-orm/paginator.rb', line 23

def total_pages
  @total_pages
end

#total_resultsObject

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_indexObject

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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


53
54
55
# File 'lib/mack-orm/paginator.rb', line 53

def has_previous?
  return self.current_page != 1 && self.total_pages > 1
end

#paginateObject

Implement this method in your ORM package. It should return self and set the following accessors: total_results, total_pages, results.

Raises:

  • (NoMethodError)


43
44
45
# File 'lib/mack-orm/paginator.rb', line 43

def paginate
  raise NoMethodError.new('paginate')
end

#start_indexObject

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