Module: DataMapper::Is::Paginated::ClassMethods

Defined in:
lib/dm-is-paginated/is/paginated.rb

Instance Method Summary collapse

Instance Method Details

#paginated(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dm-is-paginated/is/paginated.rb', line 9

def paginated(options = {})
  page     = options.delete(:page) || 1
  per_page = options.delete(:per_page) || 5
    
  options.reverse_merge!({
    :order => [:id.desc]
  })
  
  page_count = (count(options).to_f / per_page).ceil
  
  options.merge!({
    :limit => per_page, 
    :offset => (page - 1) * per_page
  })
  
  [ page_count , all(options) ]
end