pagy icon indicating copy to clipboard operation
pagy copied to clipboard

Reduce items minimum to 0 and possibly return empty array from pagy_get_items if items: 0 is specified

Open edeesis opened this issue 3 years ago • 1 comments

Code Issue

  • [x] I researched through the documentation, the pagy issues and a known Search Engine, and there is no reference|report|post|video that solves this problem
  • [x] I did install|upgraded to the latest version of pagy (or the latest 3.* for pagy legacy)
  • [x] I am providing at least one of the following working and self-contained code support that can reproduce this issue:
    • [x] Simple step by step list that would work in IRB with the Pagy::Console
    • [ ] Plain ruby file that can run as ruby my-problem.rb
    • [ ] Edited copy of the single file pagy_standalone_app.ru
    • [ ] Edited copy of the single file pagy_bare_rails.rb
    • [ ] Link of my own branch forked from pagy, which contains an added test file
    • [ ] Link of my own branch forked of any of the rails apps listed here.
    • [ ] Link of a standalone docker image downloadable from a docker repository (e.g. dockerhub.com)
    • [ ] docker-compose file that can be run with docker-compose up (and doesn't need to be built because it uses only docker images and no local context)

Ruby|Rails|Sinatra|... Versions

Ruby 3.1.2 Rails 6.1.5

Description

We would like to be able to send a items parameter of 0 and have it skip evaluating an ActiveRecord query and instead return an empty Array. If it helps, we're trying to evaluate the count of the query without getting the actual results.

While we would be happy to override the pagy_get_items method for that, the items parameter is set to a minimum of 1, so it throws a VariableError when we try this.

Steps to reproduce the issue using the provided support

Loading development environment (Rails 6.1.5)
irb(main):001:0> require 'pagy/console'
=> true
irb(main):002:0> include Pagy::Console
=> Object
irb(main):003:0> pagy_extras :items
Required extras: :items
=> nil                                                                                    
irb(main):004:0> pagy, items = pagy([1, 2, 3], items: 0)

Thank you!

edeesis avatar Sep 14 '22 21:09 edeesis

@edeesis So, what would you need the pagy object for, if you already know that the items to paginate would be none?

If you really need a pagy object with no items, you can pass count: 0 and you will get the pagy and items set to what you are asking, and skip the query.

ddnexus avatar Sep 14 '22 22:09 ddnexus

@edeesis Is this still an issue after my previous comment?

ddnexus avatar Dec 14 '22 00:12 ddnexus

Hey. I'll go ahead and close this. We ended up working around it in our own way.

Thanks!

edeesis avatar Dec 14 '22 01:12 edeesis