avo icon indicating copy to clipboard operation
avo copied to clipboard

Add option to configure via_per_page on has_many

Open smathieu opened this issue 1 year ago • 7 comments

Feature

Currently, it seems only possible to set a global via_per_page.

It would be useful to have the ability to override that value in the resource itself.

API could be something like:

module Avo
  module Resources
    class CatalogProduct < Avo::BaseResource
      def fields
         field :certifications, as: :has_many, per_page: 72
      end
  end
end

or alternatively, we could default it per resource.

module Avo
  module Resources
    class CatalogCertification < Avo::BaseResource
      self.via_per_page = 72
      self.per_page = 24
      def fields
        # ...
      end
  end
end

This is useful because in most case, we want a smaller pagination window, but we do have a few workflows where it's useful to see a large amount of items for triggering action on a batch of records.

Current workarounds

Not aware of any

Screenshots or screen recordings

Additional context

smathieu avatar Apr 04 '24 20:04 smathieu

I think this can be useful, thanks for submitting it @smathieu

API suggestion:

module Avo
  module Resources
    class CatalogCertification < Avo::BaseResource
      self.pagination = -> do
        {
         via_per_page: 72
         per_page: 24
        }
      end
      def fields
        # ...
      end
  end
end

This way inside pagination proc we can do some custom logic looking into params to check if is association or not and set pagination params accordingly.

Paul-Bob avatar Apr 05 '24 06:04 Paul-Bob

You can set the via_per_page globally right now.

Otherwise, IDK is self.via_per_page would do it justice. I would rather do the first option:

field :certifications, as: :has_many, per_page: 72

adrianthedev avatar Apr 15 '24 14:04 adrianthedev

If the global configuration is enough for you, @smathieu, then it's ok to close this issue. If you need something more granular, we'll merge a PR with that. Unfortunately, we can't prioritize and work on it right now.

adrianthedev avatar Apr 15 '24 14:04 adrianthedev

The global configuration doesn't work very well for us, as we do have a use case where we need ~100 records rendering in a page, but that's way too much for other views.

This isn't super urgent for us, but we'd definitely benefit from the option to configure it.

All proposed API would work, but I think I prefer the per field proposed by @adrianthedev

smathieu avatar Apr 15 '24 16:04 smathieu

Got it!

Yes, the field config should work better from the same reason that you want to configure the per_page only in some scenarios.

I added the Help wanted label. Maybe someone wants to pick it up.

adrianthedev avatar Apr 15 '24 17:04 adrianthedev

Please assign to me

G-Simpson avatar Jul 25 '24 03:07 G-Simpson

Thank you @G-Simpson!

Paul-Bob avatar Jul 25 '24 07:07 Paul-Bob

+1 from https://discord.com/channels/740892036978442260/1125160641569771550/1309337580143251536

Paul-Bob avatar Nov 22 '24 10:11 Paul-Bob