Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 34 lines (31 sloc) 1.298 kB
4c51c38 @mislav move Array#paginate definition to will_paginate/array.rb
authored
1 require 'will_paginate/collection'
2
97af7cb @mislav document Array#paginate
authored
3 class Array
4 # Paginates a static array (extracting a subset of it). The result is a
231f964 @mislav Total rdoc love. Point out that this is framework-agnostic now
authored
5 # WillPaginate::Collection instance, which is an array with a few more
97af7cb @mislav document Array#paginate
authored
6 # properties about its paginated state.
7 #
8 # Parameters:
9 # * <tt>:page</tt> - current page, defaults to 1
10 # * <tt>:per_page</tt> - limit of items per page, defaults to 30
11 # * <tt>:total_entries</tt> - total number of items in the array, defaults to
12 # <tt>array.length</tt> (obviously)
13 #
14 # Example:
15 # arr = ['a', 'b', 'c', 'd', 'e']
16 # paged = arr.paginate(:per_page => 2) #-> ['a', 'b']
17 # paged.total_entries #-> 5
18 # arr.paginate(:page => 2, :per_page => 2) #-> ['c', 'd']
19 # arr.paginate(:page => 3, :per_page => 2) #-> ['e']
20 #
21 # This method was originally {suggested by Desi
22 # McAdam}[http://www.desimcadam.com/archives/8] and later proved to be the
23 # most useful method of will_paginate library.
4c51c38 @mislav move Array#paginate definition to will_paginate/array.rb
authored
24 def paginate(options = {})
05fc834 @mislav refactor page number checking, add offset validation
authored
25 page = options[:page] || 1
26 per_page = options[:per_page] || WillPaginate.per_page
27 total = options[:total_entries] || self.length
28
29 WillPaginate::Collection.create(page, per_page, total) do |pager|
4c51c38 @mislav move Array#paginate definition to will_paginate/array.rb
authored
30 pager.replace self[pager.offset, pager.per_page].to_a
97af7cb @mislav document Array#paginate
authored
31 end
4c51c38 @mislav move Array#paginate definition to will_paginate/array.rb
authored
32 end
33 end
Something went wrong with that request. Please try again.