8000 Fix #113 Transparent support for jsonapi-rb and other response render… · davidcelis/api-pagination@a923116 · GitHub
[go: up one dir, main page]

Skip to content

Commit a923116

Browse files
author
Paulo Mateus Moura da Silva
committed
Fix #113 Transparent support for jsonapi-rb and other response renderer that use custom format
1 parent c9578ad commit a923116

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ ApiPagination.configure do |config|
4242
# Optional: set this to add a header with the current page number.
4343
config.page_header = 'X-Page'
4444

45+
# Optional: set this to add other response format. Useful with tools that define :jsonapi format
46+
config.response_formats = [:json, :xml, :jsonapi]
47+
4548
# Optional: what parameter should be used to set the page option
4649
config.page_param = :page
4750
# or

lib/api-pagination/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Configuration
1010

1111
attr_accessor :base_url
1212

13+
attr_accessor :response_formats
14+
1315
def configure(&block)
1416
yield self
1517
end
@@ -20,6 +22,7 @@ def initialize
2022
@page_header = nil
2123
@include_total = true
2224
@base_url = nil
25+
@response_formats = [:json, :xml]
2326
end
2427

2528
['page', 'per_page'].each do |param_name|

lib/rails/pagination.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ def paginate(*options_or_collection)
88

99
return _paginate_collection(collection, options) if collection
1010

11-
collection = options[:json] || options[:xml]
11+
response_format = _discover_format(options)
12+
13+
collection = options[response_format]
1214
collection = _paginate_collection(collection, options)
1315

14-
options[:json] = collection if options[:json]
15-
options[:xml] = collection if options[:xml]
16+
options[response_format] = collection if options[response_format]
1617

1718
render options
1819
end
@@ -23,6 +24,12 @@ def paginate_with(collection)
2324

2425
private
2526

27+
def _discover_format(options)
28+
for response_format in ApiPagination.config.response_formats
29+
return response_format if options.key?(response_format)
30+
end
31+
end
32+
2633
def _paginate_collection(collection, options={})
2734
options[:page] = ApiPagination.config.page_param(params)
2835
options[:per_page] ||= ApiPagination.config.per_page_param(params)

0 commit comments

Comments
 (0)
0