ruby-on-railspaginationjsonapi-resources

jsonapi-resources conditionally disable paginator


I use jsonapi-resources gem in my Rails app. One of my resources is using pagination. There are cases when I want to disable the pagination and make sure that user will get back all the results.

I tried custom implementation with some custom filter, but failed.

I tried passing "weird" values to paginator, but that did not help (examples for paged paginator):


Solution

  • i got exactly the same problem today and found this unanswered question.

    Below is how I dealt with the problem.

    class CustomPaginator < PagedPaginator
      attr_reader :limit, :offset, :disable_pagination
    
      def initialize(params)
        @disable_pagination = params.nil?
        super
      end
    
      def apply(relation, _order_options)
        disable_pagination ? relation : super
      end
    end
    

    and in the resources I wanted to use the CustomPaginator I just added paginator :custom like below:

    class UserResource < JSONAPI::Resource
      paginator :custom
      ....user implementation
    end
    

    now whenever i want to use the paginator i need to explicitly use paginations params when I do the request to the server and when I don't use them I will get full unpaginated response.

    The class implementation is not perfect (duh!) but it's just to show you the general concept