chewy icon indicating copy to clipboard operation
chewy copied to clipboard

Trying to use Pagy pagination with Chewy, running into autoloading will_paginate gem

Open dlegr250 opened this issue 1 year ago • 0 comments

Is your feature request related to a problem? Please describe.

I'm trying to implement the Pagy pagination gem with Chewy, but Chewy automatically includes kaminari and/or will_paginate if they are included in the project, and this built-in pagination is used behind-the-scenes automatically. The solution I've tried with some success is to make sure no other pagination gems are included, and then use Pagy with Chewy via:

chewy_query = SomeIndex.query(...)
@pagy, @records = Pagy.new(chewy_query.objects, page: 1, items: 50, count: chewy_query.total)

The specific issue is that pagination gems like will_paginate are automatically included and forced to be used if they are in the project, meaning I cannot switch from will_paginate to Pagy or another pagination alternative piece-by-piece, but must instead do an all-or-nothing approach.

Describe the solution you'd like

It would be nice to have an initializer for Chewy that sets the pagination gem to be used explicitly, and use none if none is provided. The reason I want to use Pagy is because it has a smaller memory footprint on pagination, and if you have a site that does a lot of Elasticsearch searches, decreasing the memory footprint a little bit pays off a lot in the long run.

Describe alternatives you've considered

Pagy works with elasticsearch-rails itself, but I like using Chewy because you add a lot of value and features! So getting rid of Chewy is a no-go for my team.

I have a solution that "seems" to work (listed above), but I'm not sure if it's the best implementation for trying to add Pagy pagination to Chewy. I was hoping there would have been a more plugin-style approach to pagination instead of using only kaminari or will_paginate.

I'm also looking at trying to reverse engineer those pagination modules that are listed in the Chewy pagination code, but I have to track down what calls belong to Chewy and which belong to the pagination gems themselves to see how I would implement something similar for Pagy.

dlegr250 avatar May 09 '23 01:05 dlegr250