Skip to content

Commit

Permalink
jsonapi add jsonapi_paginate method
Browse files Browse the repository at this point in the history
  • Loading branch information
metekabak committed Apr 8, 2021
1 parent 0117b2f commit 9a5bd71
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/jsonapi/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def jsonapi_pagination(resources)
#
# @return [Hash] with the first, previous, next, current, last page numbers
def jsonapi_pagination_builder(resources)
return @_numbers if @_numbers
return {} unless JSONAPI::Rails.is_collection?(resources)

_, limit, page = jsonapi_pagination_params
Expand Down Expand Up @@ -95,7 +96,7 @@ def jsonapi_pagination_builder(resources)
numbers[:total_page] = last_page
end

numbers
@_numbers = numbers
end

def jsonapi_pagination_meta(resources)
Expand Down
39 changes: 38 additions & 1 deletion lib/jsonapi/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
# Rails integration
module JSONAPI
module Rails
JSONAPI_METHODS_MAPPING = {
JSONAPI_PAGINATE_METHODS_MAPPING = {
meta: :jsonapi_meta,
links: :jsonapi_pagination,
fields: :jsonapi_fields,
include: :jsonapi_include,
params: :jsonapi_serializer_params
}

JSONAPI_METHODS_MAPPING = {
meta: :jsonapi_meta,
fields: :jsonapi_fields,
include: :jsonapi_include,
params: :jsonapi_serializer_params
}

# Updates the mime types and registers the renderers
#
# @return [NilClass]
Expand Down Expand Up @@ -89,6 +96,35 @@ def self.add_errors_renderer!
#
# @return [NilClass]
def self.add_renderer!
ActionController::Renderers.add(:jsonapi_paginate) do |resource, options|
self.content_type ||= Mime[:jsonapi]

JSONAPI_PAGINATE_METHODS_MAPPING.to_a[0..1].each do |opt, method_name|
next unless respond_to?(method_name, true)
options[opt] ||= send(method_name, resource)
end

# If it's an empty collection, return it directly.
many = JSONAPI::Rails.is_collection?(resource, options[:is_collection])
if many && !resource.any?
return options.slice(:meta, :links).merge(data: []).to_json
end

JSONAPI_PAGINATE_METHODS_MAPPING.to_a[2..-1].each do |opt, method_name|
options[opt] ||= send(method_name) if respond_to?(method_name, true)
end

if respond_to?(:jsonapi_serializer_class, true)
serializer_class = jsonapi_serializer_class(resource, many)
else
serializer_class = JSONAPI::Rails.serializer_class(resource, many)
end

JSONAPI::Rails.serializer_to_json(
serializer_class.new(resource, options)
)
end

ActionController::Renderers.add(:jsonapi) do |resource, options|
self.content_type ||= Mime[:jsonapi]

Expand Down Expand Up @@ -117,6 +153,7 @@ def self.add_renderer!
serializer_class.new(resource, options)
)
end

end

# Checks if an object is a collection
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module JSONAPI
VERSION = '2.0.0'
VERSION = '2.1.0'
end

0 comments on commit 9a5bd71

Please sign in to comment.