Sha256: f26f207253516b9586263a14b76fa1be2b2ec0073167a1ed8a9aa9fa1b7a8921

Contents?: true

Size: 1.27 KB

Versions: 28

Compression:

Stored size: 1.27 KB

Contents

require 'will_paginate/collection'

class Array
  # Paginates a static array (extracting a subset of it). The result is a
  # WillPaginate::Collection instance, which is an array with a few more
  # properties about its paginated state.
  #
  # Parameters:
  # * <tt>:page</tt> - current page, defaults to 1
  # * <tt>:per_page</tt> - limit of items per page, defaults to 30
  # * <tt>:total_entries</tt> - total number of items in the array, defaults to
  #   <tt>array.length</tt> (obviously)
  #
  # Example:
  #   arr = ['a', 'b', 'c', 'd', 'e']
  #   paged = arr.paginate(:per_page => 2)      #->  ['a', 'b']
  #   paged.total_entries                       #->  5
  #   arr.paginate(:page => 2, :per_page => 2)  #->  ['c', 'd']
  #   arr.paginate(:page => 3, :per_page => 2)  #->  ['e']
  #
  # This method was originally {suggested by Desi
  # McAdam}[http://www.desimcadam.com/archives/8] and later proved to be the
  # most useful method of will_paginate library.
  def paginate(options = {})
    page     = options[:page] || 1
    per_page = options[:per_page] || WillPaginate.per_page
    total    = options[:total_entries] || self.length

    WillPaginate::Collection.create(page, per_page, total) do |pager|
      pager.replace self[pager.offset, pager.per_page].to_a
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
will_paginate-4.0.1 lib/will_paginate/array.rb
will_paginate-4.0.0 lib/will_paginate/array.rb
will_paginate-3.3.1 lib/will_paginate/array.rb
will_paginate-3.3.0 lib/will_paginate/array.rb
will_paginate-3.2.1 lib/will_paginate/array.rb
will_paginate-3.2.0 lib/will_paginate/array.rb
will_paginate-3.1.8 lib/will_paginate/array.rb
will_paginate-3.1.7 lib/will_paginate/array.rb
will_paginate-3.1.6 lib/will_paginate/array.rb
will_paginate-3.0.12 lib/will_paginate/array.rb
will_paginate-3.1.5 lib/will_paginate/array.rb
will_paginate-3.0.11 lib/will_paginate/array.rb
will_paginate-3.1.3 lib/will_paginate/array.rb
will_paginate-3.0.10 lib/will_paginate/array.rb
will_paginate-3.0.9 lib/will_paginate/array.rb
will_paginate-3.1.2 lib/will_paginate/array.rb
will_paginate-3.1.1 lib/will_paginate/array.rb
will_paginate-3.0.8 lib/will_paginate/array.rb
will_paginate-3.1.0 lib/will_paginate/array.rb
will_paginate_seo-3.0.4 lib/will_paginate/array.rb