Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with api-pagination and pagy #11

Open
slhck opened this issue Sep 6, 2023 · 2 comments
Open

Usage with api-pagination and pagy #11

slhck opened this issue Sep 6, 2023 · 2 comments

Comments

@slhck
Copy link

slhck commented Sep 6, 2023

This is more of a question about the usage of this Gem.

I am using api-pagination in combination with pagy to provide a paginated API.
This might be as simple as the example shown here.

Internally api-pagination calls pagy via Pagy.new; here you can also see the offset and limit being applied.

How would I make this work with fast_page?

When I add the method from the README to my controller, it is not being called.

@slhck
Copy link
Author

slhck commented Sep 6, 2023

For now I was able to monkey-patch my way out of this issue by simply adding this to the controller:

module ApiPagination
  class << self
    def paginate_with_pagy(collection, options)
      if Pagy::DEFAULT[:max_per_page] && options[:per_page] > Pagy::DEFAULT[:max_per_page]
        options[:per_page] = Pagy::DEFAULT[:max_per_page]
      elsif options[:per_page] <= 0
        options[:per_page] = Pagy::DEFAULT[:items]
      end

      pagy = pagy_from(collection, options)
      collection = if collection.respond_to?(:offset) && collection.respond_to?(:limit)
        collection.offset(pagy.offset).limit(pagy.items).fast_page
      else
        collection[pagy.offset, pagy.items]
      end

      return [collection, pagy]
    end
  end
end

Notice the addition lf .fast_page in one of the lines.

But there is probably a cleaner way to do this…

@mscoutermarsh
Copy link
Member

Thanks @slhck! I'm going to look at this more when I have a chance.

I'm expecting your way of overriding the method is probably best. Also possible pagy may have changed since I last used it with fast_page.

I'll see if I can find a nicer way, if not, will update the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants