Module: DataMapper::Pagination

Defined in:
lib/dm-pager/helpers.rb,
lib/dm-pager/version.rb,
lib/dm-pager/defaults.rb,
lib/dm-pager/pagination.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

VERSION =
'0.0.8'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pagerObject

DataMapper::Pager instance.



8
9
10
# File 'lib/dm-pager/pagination.rb', line 8

def pager
  @pager
end

Class Method Details

.defaultsObject

Default pagination values.

Options

:per_page       Records per page; defaults to 6
:size           Number of intermediate page number links to be shown; Defaults to 7
:pager_class    Class for the div that contains the pagination links, defaults to 'pager'
:previous_text  Text for the 'previous' link, defaults to 'Previous'
:next_text      Text for the 'next' link, defaults to 'Next'
:first_text     Text for the 'first' link, defaults to 'First'
:last_text      Text for the 'last' link, defaults to 'Last'
:more_text      Text for the 'more' indicator, defaults to '...'

Examples

DataMapper::Pagination.defaults[:size] = 5


35
36
37
# File 'lib/dm-pager/defaults.rb', line 35

def self.defaults
  @defaults
end

Instance Method Details

#page(page = nil, options = {}) ⇒ Object

Page collection by the page number and options provided.

Since pagers will commonly be used with query strings, we coerce all numeric strings such as ‘12’ to their integer value 12. This is the case for page, :per_page, :page, etc.

Options

:page       Current page number
:per_page   Results per page; defaults to 6
:order      Defaults to [:id.desc]

Examples

User.all.page
User.all.page(2)
User.all.page(2, :per_page => 5)
User.all.page(:page => 2, :per_page => 5)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dm-pager/pagination.rb', line 31

def page page = nil, options = {}
  options, page = page, nil if page.is_a? Hash
  page ||= pager_option :page, options
  options.delete :page
  page = 1 unless (page = page.to_i) && page > 1
  per_page = pager_option(:per_page, options).to_i
  query = options.dup
  collection = new_collection scoped_query(options = {
    :limit => per_page,
    :offset => (page - 1) * per_page,
    :order => [:id.desc]
  }.merge(query))
  options.merge! :total => count(query), :page => page
  collection.pager = DataMapper::Pager.new options
  collection
end