Paginating an Array in Ruby with will_paginate
Asked Answered
E

5

34

I have an array @level1 which looks like this :

[[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4], ............]

I want to apply pagination on this array such each page displays only a few rows at once. I tried this:

@temp1 = @level1.paginate(:page => params[:page])

But it throws the following error:

undefined method `paginate' for [[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4]]:Array

How can I perform pagination on this using will_paginate?

Erelong answered 1/11, 2012 at 23:29 Comment(0)
E
27

Sure, you can use WillPaginate::Collection to construct arbitrary pagination across collections. Assuming an array called values:

@values = WillPaginate::Collection.create(current_page, per_page, values.length) do |pager|
  pager.replace values
end

You can then treat @values just like any other WillPaginate collection, including passing it to the will_paginate view helper.

Economic answered 1/11, 2012 at 23:40 Comment(7)
This works great if you have an array of objects representing one page (and the total count) and want to use various WillPaginate view helpers - for example if you need to bypass ActiveRecord for performance reasons.Protuberancy
I tried this approach and was half successful. It generates the will_paginate view helper, but the array is not broken down: the full list is generated. Anything in particular needed in the view?Sorrel
@Sorrel You need to populate the values array with only the values for the current page. You then pass in current_page/per_page/the total number of values in the whole collection manually, and it constructs the pagination helpers.Economic
That sounds logical. In other words , since I'm sorting the array, first sort array, then '@values.page(params[:page])` or .pagination[...] and finally: WillPaginate::Collection.create(1, 10, @values.length) . This however returns undefined method page' for # < Array`... Is this documented someplace? I've been searching... Specific posting #23875593Sorrel
Wow, you have no idea how this answer helped me! Massive thumbs upGrind
The answer suggesting that you require will_paginate/array is a better solutionPurse
Assuming you don't mind extending Array. If you need it in one or two places, extending Array might be a code smell. will_paginate/array does exactly what this code does, except it extends it onto Array.Economic
G
82

See https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb

Just require 'will_paginate/array' before you try it and it will work just fine. If it were me, I'd put that in say config/initalizers/will_paginate_extensions.rb so it's available right away and from everywhere.

Gabelle answered 2/11, 2012 at 0:15 Comment(1)
@NathanColgate If you have to use a lot, then maybe yes. But if you want to use this in just a few places, I would not pollute the standard classes with stuff that aren't their responsibility. A PaginatedArray, that inherits from Array and has this method might be the better way. But I'll go with Chris' solution.Bellew
E
27

Sure, you can use WillPaginate::Collection to construct arbitrary pagination across collections. Assuming an array called values:

@values = WillPaginate::Collection.create(current_page, per_page, values.length) do |pager|
  pager.replace values
end

You can then treat @values just like any other WillPaginate collection, including passing it to the will_paginate view helper.

Economic answered 1/11, 2012 at 23:40 Comment(7)
This works great if you have an array of objects representing one page (and the total count) and want to use various WillPaginate view helpers - for example if you need to bypass ActiveRecord for performance reasons.Protuberancy
I tried this approach and was half successful. It generates the will_paginate view helper, but the array is not broken down: the full list is generated. Anything in particular needed in the view?Sorrel
@Sorrel You need to populate the values array with only the values for the current page. You then pass in current_page/per_page/the total number of values in the whole collection manually, and it constructs the pagination helpers.Economic
That sounds logical. In other words , since I'm sorting the array, first sort array, then '@values.page(params[:page])` or .pagination[...] and finally: WillPaginate::Collection.create(1, 10, @values.length) . This however returns undefined method page' for # < Array`... Is this documented someplace? I've been searching... Specific posting #23875593Sorrel
Wow, you have no idea how this answer helped me! Massive thumbs upGrind
The answer suggesting that you require will_paginate/array is a better solutionPurse
Assuming you don't mind extending Array. If you need it in one or two places, extending Array might be a code smell. will_paginate/array does exactly what this code does, except it extends it onto Array.Economic
G
5

Given a collection array in a Rails project

will_paginate ~>3.0.5

collection.paginate(page: params[:page], per_page: 10)

returns the pagination for the array values.

Do not forget require 'will_paginate/array' on top of the controller.

Garin answered 2/8, 2016 at 15:22 Comment(2)
I don't think an array can call paginate. This is the error I get: NoMethodError: undefined method paginate' for #<Array:0x007fc983759000>`Mezcaline
@AnthonyTo you missed the bit about requiring will_paginate/array.Lucero
R
2

This is my example based in the answer of @chris-heald

products = %w(i love ruby on rails)
@products = WillPaginate::Collection.create(params[:page], params[:per_page], products.length) do |pager|
   pager.replace products[pager.offset, pager.per_page]
end
Renfred answered 15/1, 2016 at 18:27 Comment(0)
R
0

I wasn't really happy with the answers provided in this thread.

I came up with this solution.

# app/lib/paged_array.rb

require 'will_paginate/array'

class PagedArray
  delegate_missing_to :@paged_collection

  def initialize(collection, page: 1, per_page: 20)
    @paged_collection = collection.paginate(
      page: page,
      per_page: per_page
    )
  end
end

And you can use it like that:

superheroes = %w[spiderman batman superman ironman]

PagedArray.new(superheroes, page: 1, per_page: 2)

I like this solution since it's explicit and you are not polluting your standard classes.

That being said, you are dependent of rails since I use "delegate_missing_to".

Rochus answered 13/1, 2019 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.