File tree Expand file tree Collapse file tree 4 files changed +19
-6
lines changed Expand file tree Collapse file tree 4 files changed +19
-6
lines changed Original file line number Diff line number Diff line change 3
3
1 . Fork it
4
4
2 . Create your feature branch (` git checkout -b my-new-feature ` )
5
5
3 . Commit your changes and tests (` git commit -am 'Add some feature' ` )
6
- 4 . Run the tests (` PAGINATOR=kaminari bundle exec rspec; PAGINATOR=will_paginate bundle exec rspec ` )
6
+ 4 . Run the tests (` PAGINATOR=pagy bundle exec rspec; PAGINATOR= kaminari bundle exec rspec; PAGINATOR=will_paginate bundle exec rspec ` )
7
7
5 . Push to the branch (` git push origin my-new-feature ` )
8
8
6 . Create a new Pull Request
Original file line number Diff line number Diff line change @@ -42,18 +42,21 @@ 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
48
51
config.page_param do |params |
49
- params[:page ][:number ]
52
+ params[:page ][:number ] if params[ :page ].is_a?( ActionController :: Parameters )
50
53
end
51
54
52
55
# Optional: what parameter should be used to set the per page option
53
56
config.per_page_param = :per_page
54
57
# or
55
58
config.per_page_param do |params |
56
- params[:page ][:size ]
59
+ params[:page ][:size ] if params[ :page ].is_a?( ActionController :: Parameters )
57
60
end
58
61
59
62
# Optional: Include the total and last_page link header
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