Skip to content

Commit

Permalink
Replaced the is_a?(Hash) check for jsonapi reserved :page param with …
Browse files Browse the repository at this point in the history
…respond_to?(:Fetch) and prepended to the Frontend
  • Loading branch information
ddnexus committed Jan 18, 2024
1 parent 0be1d3e commit 87f3cf1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/pagy/extras/jsonapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ def pagy_jsonapi_links(pagy, **opts)
# Should skip the jsonapi
def pagy_skip_jsonapi?(vars)
return true if vars[:jsonapi] == false || (vars[:jsonapi].nil? && DEFAULT[:jsonapi] == false)
# check the reserved :page param and raise PageParamError or return nil
raise ReservedParamError, params[:page] unless params[:page].is_a?(Hash) || params[:page].nil?
# check the reserved :page param
raise ReservedParamError, params[:page] unless params[:page].respond_to?(:fetch) || params[:page].nil?
end

# Override the Backend method
def pagy_get_page(vars)
return super if pagy_skip_jsonapi?(vars)
return 1 if params[:page].nil?

((params[:page].is_a?(Hash) && params[:page][vars[:page_param] || DEFAULT[:page_param]]) || 1).to_i
params[:page][vars[:page_param] || DEFAULT[:page_param]].to_i
end

# Override the ItemsExtra::Backend method
def pagy_get_items_size(vars)
return super if pagy_skip_jsonapi?(vars)
return if params[:page].nil?

params[:page][vars[:items_param] || DEFAULT[:items_param]] if params[:page].is_a?(Hash)
params[:page][vars[:items_param] || DEFAULT[:items_param]]
end

# Override UrlHelper method
Expand All @@ -59,4 +61,5 @@ def pagy_set_query_params(page, vars, params)
end
end
Backend.prepend JsonApiExtra
Frontend.prepend JsonApiExtra
end

0 comments on commit 87f3cf1

Please sign in to comment.