Skip to content

Commit

Permalink
Add first, last, next, prev to JSON API pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Nov 2, 2023
1 parent eea9d9f commit 90bf9c5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,28 @@ def handle_exception(exception)
error = Errors::Resolver.resolve_for(exception)
render status: error.http_status_code.to_i, json: error.to_json_api(request)
end

private

def meta(collection, options = {})
meta_resp = super

meta_resp[:pagination][:first] = { page: 1 }
meta_resp[:pagination][:last] = { page: collection.total_pages }

unless collection.first_page?
meta_resp[:pagination][:prev] = {
page: collection.current_page - 1,
}
end

unless collection.last_page? || collection.current_page >= collection.total_pages
meta_resp[:pagination][:next] = {
page: collection.current_page + 1
}
end

meta_resp
end
end
end

0 comments on commit 90bf9c5

Please sign in to comment.