Skip to content

Commit

Permalink
Import fixes for pagination from jsonapi.rb PR
Browse files Browse the repository at this point in the history
See stas/jsonapi.rb#91 for details on what these
fix.
  • Loading branch information
mamhoff committed Jul 20, 2023
1 parent 7aa526f commit 4a273fb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,42 @@ def base_page_scope
def jsonapi_serializer_class(_resource, _is_collection)
::Alchemy::JsonApi::PageSerializer
end

# These overrides have to be in place until
# https://github.com/stas/jsonapi.rb/pull/91
# is merged and released
def jsonapi_paginate(resources)
@_jsonapi_original_size = resources.size
super
end

def jsonapi_pagination_meta(resources)
return {} unless JSONAPI::Rails.is_collection?(resources)

_, limit, page = jsonapi_pagination_params

numbers = { current: page }

total = @_jsonapi_original_size

last_page = [1, (total.to_f / limit).ceil].max

if page > 1
numbers[:first] = 1
numbers[:prev] = page - 1
end

if page < last_page
numbers[:next] = page + 1
numbers[:last] = last_page
end

if total.present?
numbers[:records] = total
end

numbers
end
end
end
end

0 comments on commit 4a273fb

Please sign in to comment.