禁用关系的分页

时间:2016-12-19 04:55:47

标签: ruby-on-rails jsonapi-resources

给出2个资源:

eth0

用户has_many公司

default_paginator =:paged

auto eno1 iface eno1 inet static address 192.168.1.88 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 gateway 192.168.1.1 dns-nameservers 127.0.1.1 请求是分页的,这就是我想要的。但我也想为关系请求jsonapi_resources :companies jsonapi_resources :users 禁用它。怎么做?

1 个答案:

答案 0 :(得分:1)

我找到的最佳解决方案是覆盖JSONAPI::RequestParser#parse_pagination,如下所示:

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