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 331 . Fork it
442 . Create your feature branch (` git checkout -b my-new-feature ` )
553 . 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 ` )
775 . Push to the branch (` git push origin my-new-feature ` )
886 . Create a new Pull Request
Original file line number Diff line number Diff line change @@ -42,18 +42,21 @@ 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
4851 config.page_param do |params |
49- params[:page ][:number ]
52+ params[:page ][:number ] if params[ :page ].is_a?( ActionController :: Parameters )
5053 end
5154
5255 # Optional: what parameter should be used to set the per page option
5356 config.per_page_param = :per_page
5457 # or
5558 config.per_page_param do |params |
56- params[:page ][:size ]
59+ params[:page ][:size ] if params[ :page ].is_a?( ActionController :: Parameters )
5760 end
5861
5962 # 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
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 |
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments