Disable pagination for relationships

473 views Asked by At

Given 2 resources:

jsonapi_resources :companies
jsonapi_resources :users

User has_many Companies

default_paginator = :paged

/companies request is paginated and that's what I want. But I also want to disable it for relationship request /users/4/companies. How to do this?

1

There are 1 answers

4
Guy Segev On BEST ANSWER

The best solution I found will be to override JSONAPI::RequestParser#parse_pagination like this:

class CustomNonePaginator < JSONAPI::Paginator
  def initialize
  end

  def apply(relation, _order_options)
    relation
  end

  def calculate_page_count(record_count)
    record_count
  end
end

class JSONAPI::RequestParser
  def parse_pagination(page)
    if disable_pagination?
      @paginator = CustomNonePaginator.new
    else
      original_parse_pagination(page)
    end
  end

  def disable_pagination?
     # your logic here 
     # request params are available through @params or @context variables 
     # so you get your action, path or any context data 
  end

  def original_parse_pagination(page)
    paginator_name = @resource_klass._paginator
    @paginator = JSONAPI::Paginator.paginator_for(paginator_name).new(page) unless paginator_name == :none
  rescue JSONAPI::Exceptions::Error => e
    @errors.concat(e.errors)
  end

end