File tree Expand file tree Collapse file tree 3 files changed +16
-3
lines changed Expand file tree Collapse file tree 3 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ ApiPagination.configure do |config|
42
42
# Optional: set this to add a header with the current page number.
43
43
config.page_header = ' X-Page'
44
44
45
+ # Optional: set this to add other response format. Useful with tools that define :jsonapi format
46
+ config.response_formats = [:json , :xml , :jsonapi ]
47
+
45
48
# Optional: what parameter should be used to set the page option
46
49
config.page_param = :page
47
50
# or
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ class Configuration
10
10
11
11
attr_accessor :base_url
12
12
13
+ attr_accessor :response_formats
14
+
13
15
def configure ( &block )
14
16
yield self
15
17
end
@@ -20,6 +22,7 @@ def initialize
20
22
@page_header = nil
21
23
@include_total = true
22
24
@base_url = nil
25
+ @response_formats = [ :json , :xml ]
23
26
end
24
27
25
28
[ 'page' , 'per_page' ] . each do |param_name |
Original file line number Diff line number Diff line change @@ -8,11 +8,12 @@ def paginate(*options_or_collection)
8
8
9
9
return _paginate_collection ( collection , options ) if collection
10
10
11
- collection = options [ :json ] || options [ :xml ]
11
+ response_format = _discover_format ( options )
12
+
13
+ collection = options [ response_format ]
12
14
collection = _paginate_collection ( collection , options )
13
15
14
- options [ :json ] = collection if options [ :json ]
15
- options [ :xml ] = collection if options [ :xml ]
16
+ options [ response_format ] = collection if options [ response_format ]
16
17
17
18
render options
18
19
end
@@ -23,6 +24,12 @@ def paginate_with(collection)
23
24
24
25
private
25
26
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
+
26
33
def _paginate_collection ( collection , options = { } )
27
34
options [ :page ] = ApiPagination . config . page_param ( params )
28
35
options [ :per_page ] ||= ApiPagination . config . per_page_param ( params )
You can’t perform that action at this time.
0 commit comments