Class: Mack::Database::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/mack-data_mapper/paginator.rb

Instance Method Summary collapse

Instance Method Details

#paginateObject

Implements the paginate method from the Mack::Database::Paginator spec in mack-orm.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mack-data_mapper/paginator.rb', line 7

def paginate
  order_clause = [self.query_options.delete(:order)].flatten.compact
    
  self.total_results = self.klass.count(self.query_options)
  self.total_pages = (self.total_results.to_f / self.results_per_page).ceil
  
  self.current_page = self.total_pages if self.current_page > self.total_pages
  
  if order_clause.empty?
    self.klass.key.each do |k| 
      order_clause << k.name.to_sym.asc
    end
  end

  self.query_options.reverse_merge!({
    :order => order_clause
  })
  
  offset = (self.current_page - 1) * self.results_per_page
  offset = 0 if offset < 0
  
  self.query_options.merge!({
    :limit => self.results_per_page, 
    :offset => offset
  })
  
  self.results = self.klass.all(self.query_options)
  self
end