Skip to content

Commit cfd0946

Browse files
authored
Merge pull request #114 from SrMouraSilva/jsonapi-rb-support
Fix #113 Jsonapi rb support
2 parents c9578ad + fb2bf24 commit cfd0946

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
1. Fork it
44
2. Create your feature branch (`git checkout -b my-new-feature`)
55
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`)
77
5. Push to the branch (`git push origin my-new-feature`)
88
6. Create a new Pull Request

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff 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

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)