Skip to content

Commit

Permalink
Strengthen UniversalPaginator
Browse files Browse the repository at this point in the history
  • Loading branch information
NuckChorris committed Sep 21, 2023
1 parent cbc9d61 commit 4c29a8a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/universal_paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ class UniversalPaginator < JSONAPI::Paginator
attr_reader :params

def initialize(params)
super
@params = params
# Initialize sub-paginator
paginator
end

def offset?
params[:offset].present? || params[:limit].present?
if params&.fetch(:offset, nil) || params&.fetch(:limit, nil)
true
else
false
end
end

def page?
Expand All @@ -32,10 +37,12 @@ def apply(relation, _order_options)
if offset?
paginator.apply(relation, _order_options)
else
params = params.to_h
# We apply the PagedPaginator logic ourselves because it uses offset/limit
# We want to have page/per method calls to be applied to the relation
relation = relation.page(params[:number]) if params[:number].present?
relation = relation.per(params[:size]) if params[:size].present?
relation
end
end
end

0 comments on commit 4c29a8a

Please sign in to comment.