will_paginate can paginate plain Ruby arrays

Posted . Visible to the public.

While you are probably using will_paginate Show archive.org snapshot to paginate ActiveRecord scopes, it can actually paginate plain Ruby arrays. The resulting arrayish object will have the same methods as a paginated scope, e.g. #total_entries. This means you can render pagination controls with the same code that works with paginated scopes.

To enable this, add an initializer config/initializers/array_paginate.rb:

require 'will_paginate/array'

You can now say:

> numbers = (1..1000).to_a
> page = numbers.paginate(:page => 2, :per_page => 10)
=> [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 
> page.total_entries
=> 1000
Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-01-03 17:43)