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

Pagy problem with changing collection in API #907

Open
alsted opened this issue Sep 2, 2024 · 0 comments
Open

Pagy problem with changing collection in API #907

alsted opened this issue Sep 2, 2024 · 0 comments

Comments

@alsted
Copy link

alsted commented Sep 2, 2024

For an API endpoint index action that has a simple filter action as:

   def index
      # Filter @agreements if ext_ref param is present
      if params[:ext_ref].present?
        @agreements = @agreements.where(ext_ref: params[:ext_ref])
       end
    end

This causes a change to the collection that Pagy is initialized with in:
bullet_train-core/bullet_train-api/app/controllers/concerns/api/controllers/base.rb

where initialization takes place as:

  def apply_pagination
    collection = instance_variable_get collection_variable
    @pagy, collection = pagy_cursor collection, after: params[:after], order: {id: :asc}
    instance_variable_set collection_variable, collection
  end

Pagination headers are set later by:

def set_pagination_headers
    return unless @pagy

    if @pagy.has_more?
      if (collection = instance_variable_get(collection_variable))
        next_cursor = collection.last.id
        link_header = response.headers["Link"]
        link_value = "<#{modify_url_params(request.url, after: next_cursor)}>; rel=\"next\""
        response.headers["Link"] = link_header ? "#{link_header}, #{link_value}" : link_value
        response.headers["Pagination-Next"] = next_cursor
      end
    end
  end

The the simple filter shown is run in between apply_pagination and set_pagination_header. The filtering can cause an empty collection which leads to an error in next_cursor = collection.last.id as collection is has no members.

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

1 participant