Question

I'm using Spree and in the products/search controller action it uses Ransack. The Spree documentation says that Spree provides no way to customize page size, so I thought I'd customize the action itself to use Ransack to turn off pagination entirely. Does Ransack support a way to either not paginate results or to at least make the pagination huge?

This is the code in the existing controller action:

@products = product_scope.ransack(params[:q]).result.page(params[:page])

When I puts the type of @products after that, it is ActiveRecord::Relation. When I puts of

@products = product_scope.ransack(params[:q]).result

it is also ActiveRecord::Relation but in the first case there's a method 'total_count' used in the rabl template that is not present in the second one. I have dug through the source for ransack and I can't find where the .page is defined. I'm wondering if perhaps it's included in some class_eval of ActiveRecord::Relation in one of the gems that Spree pulls in.

Someone somewhere has to have faced this problem and come up with a solution. I'm hoping against hope to find that person :P

jd

Was it helpful?

Solution

The .page method comes from the Kaminari gem, and not Ransack.

You can make the pagination "huge" by changing the default per_page value in Kaminari:

Kaminari.config.default_per_page = 1000

OTHER TIPS

Ryan's already got you, but I'm not sure if there isn't another way to do it as well. I can't tell from your question if you're talking about just the number of products shown per page, but it sounds like you are.

If so, I've done the same modification by using the built-in Spree preference products_per_page. When Spree provides a preference, I prefer (no pun intended) to use that over modifying gem parameters since it's part of their design already.

I fired up the console with bundle exec rails c, then ran the following command to set the preference on my store to 16 products per page: Spree::Config.products_per_page = 16

You can also put it in one of your initialization files so it doesn't get accidentally erased if you rebuild your database from scratch...I learned about it from this post in the Spree group. I also found details on setting Spree preferences here.

You want to use .per, for example:

collection.per(100)
collection.page(3).per(100)
collection.per(100).per(3)

Any of these will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top