api-pagination 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/api-pagination.rb +11 -7
- data/lib/api-pagination/hooks.rb +2 -2
- data/lib/api-pagination/version.rb +10 -4
- data/lib/grape/pagination.rb +1 -0
- data/lib/rails/pagination.rb +2 -1
- data/spec/grape_spec.rb +7 -1
- data/spec/rails_spec.rb +7 -1
- data/spec/spec_helper.rb +8 -2
- data/spec/support/shared_examples/existing_headers.rb +4 -0
- data/spec/support/shared_examples/first_page.rb +4 -0
- data/spec/support/shared_examples/last_page.rb +4 -0
- data/spec/support/shared_examples/middle_page.rb +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a9271269dab6d4d3b99562c1262975c14c76efc
|
4
|
+
data.tar.gz: 3f98efbf74253877c9c2e0e99da24ac5a798e041
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84f99de16f079a1a9e5acc30f3405b8798b2d5f215a022bb7cc5d3b98990bad16f3318aa2ae04fe234d454905144604091b31cea3aa5b1cb36fb3bb9f790fcab
|
7
|
+
data.tar.gz: c122d6e8e2fec923b1ca0ad30eed0bc6d502a64c34c197c51b9de75e3cafe2e8c6fa6b89bbe514aa0bb57380910b67915569915a80c5caa227741a0b7632b574
|
data/lib/api-pagination.rb
CHANGED
@@ -3,19 +3,16 @@ require 'api-pagination/version'
|
|
3
3
|
|
4
4
|
module ApiPagination
|
5
5
|
class << self
|
6
|
-
|
7
|
-
attr_writer :will_paginate
|
8
|
-
|
9
|
-
def kaminari?() !!@kaminari end
|
10
|
-
def will_paginate?() !!@will_paginate end
|
6
|
+
attr_reader :paginator
|
11
7
|
|
12
8
|
def paginate(collection, options = {}, &block)
|
13
9
|
options[:page] ||= 1
|
14
10
|
options[:per_page] ||= 10
|
15
11
|
|
16
|
-
|
12
|
+
case ApiPagination.paginator
|
13
|
+
when :kaminari
|
17
14
|
collection.page(options[:page]).per(options[:per_page]).tap(&block)
|
18
|
-
|
15
|
+
when :will_paginate
|
19
16
|
collection.paginate(:page => options[:page], :per_page => options[:per_page]).tap(&block)
|
20
17
|
end
|
21
18
|
end
|
@@ -33,6 +30,13 @@ module ApiPagination
|
|
33
30
|
end
|
34
31
|
end
|
35
32
|
end
|
33
|
+
|
34
|
+
def total_from(collection)
|
35
|
+
case ApiPagination.paginator
|
36
|
+
when :kaminari then collection.total_count
|
37
|
+
when :will_paginate then collection.total_entries
|
38
|
+
end
|
39
|
+
end
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
data/lib/api-pagination/hooks.rb
CHANGED
@@ -26,12 +26,12 @@ module ApiPagination
|
|
26
26
|
def last_page?() !next_page end
|
27
27
|
end
|
28
28
|
|
29
|
-
ApiPagination.will_paginate
|
29
|
+
ApiPagination.instance_variable_set(:@paginator, :will_paginate)
|
30
30
|
end
|
31
31
|
|
32
32
|
begin; require 'kaminari'; rescue LoadError; end
|
33
33
|
if defined?(Kaminari)
|
34
|
-
ApiPagination.kaminari
|
34
|
+
ApiPagination.instance_variable_set(:@paginator, :kaminari)
|
35
35
|
end
|
36
36
|
|
37
37
|
STDERR.puts <<-EOC unless defined?(Kaminari) || defined?(WillPaginate)
|
@@ -1,7 +1,13 @@
|
|
1
1
|
module ApiPagination
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
class Version
|
3
|
+
MAJOR = 2
|
4
|
+
MINOR = 1
|
5
|
+
PATCH = 0
|
5
6
|
|
6
|
-
|
7
|
+
def self.to_s
|
8
|
+
[MAJOR, MINOR, PATCH].join('.')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
VERSION = Version.to_s
|
7
13
|
end
|
data/lib/grape/pagination.rb
CHANGED
data/lib/rails/pagination.rb
CHANGED
@@ -15,7 +15,8 @@ module Rails
|
|
15
15
|
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
|
16
16
|
end
|
17
17
|
|
18
|
-
headers['Link']
|
18
|
+
headers['Link'] = links.join(', ') unless links.empty?
|
19
|
+
headers['Total'] = ApiPagination.total_from(collection)
|
19
20
|
end
|
20
21
|
|
21
22
|
ApiPagination.paginate(collection, params, &block)
|
data/spec/grape_spec.rb
CHANGED
@@ -7,12 +7,18 @@ require 'support/shared_examples/last_page'
|
|
7
7
|
describe NumbersAPI do
|
8
8
|
describe 'GET #index' do
|
9
9
|
let(:links) { last_response.headers['Link'].split(', ') }
|
10
|
+
let(:total) { last_response.headers['Total'].to_i }
|
10
11
|
|
11
12
|
context 'without enough items to give more than one page' do
|
13
|
+
before { get :numbers, :count => 20 }
|
14
|
+
|
12
15
|
it 'should not paginate' do
|
13
|
-
get :numbers, :count => 20
|
14
16
|
expect(last_response.headers.keys).not_to include('Link')
|
15
17
|
end
|
18
|
+
|
19
|
+
it 'should give a Total header' do
|
20
|
+
expect(total).to eq(20)
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
context 'with existing Link headers' do
|
data/spec/rails_spec.rb
CHANGED
@@ -6,14 +6,20 @@ require 'support/shared_examples/last_page'
|
|
6
6
|
|
7
7
|
describe NumbersController, :type => :controller do
|
8
8
|
before { request.host = 'example.org' }
|
9
|
+
|
9
10
|
describe 'GET #index' do
|
10
11
|
let(:links) { response.headers['Link'].split(', ') }
|
12
|
+
let(:total) { response.headers['Total'].to_i }
|
11
13
|
|
12
14
|
context 'without enough items to give more than one page' do
|
15
|
+
before { get :index, :count => 20 }
|
13
16
|
it 'should not paginate' do
|
14
|
-
get :index, :count => 20
|
15
17
|
expect(response.headers.keys).not_to include('Link')
|
16
18
|
end
|
19
|
+
|
20
|
+
it 'should give a Total header' do
|
21
|
+
expect(total).to eq(20)
|
22
|
+
end
|
17
23
|
end
|
18
24
|
|
19
25
|
context 'with existing Link headers' do
|
data/spec/spec_helper.rb
CHANGED
@@ -24,10 +24,16 @@ PaginatedSet = Struct.new(:current_page, :per_page, :total_count) do
|
|
24
24
|
def paginate(options = {})
|
25
25
|
page(options[:page]).per(options[:per_page])
|
26
26
|
end
|
27
|
+
|
28
|
+
alias :total_entries :total_count
|
27
29
|
end
|
28
30
|
|
29
|
-
|
30
|
-
ApiPagination.
|
31
|
+
if ENV['PAGINATOR']
|
32
|
+
ApiPagination.instance_variable_set(:@paginator, ENV['PAGINATOR'].to_sym)
|
33
|
+
else
|
34
|
+
warn 'No PAGINATOR set. Defaulting to kaminari. To test against will_paginate, run `PAGINATOR=will_paginate bundle exec rspec`'
|
35
|
+
ApiPagination.instance_variable_set(:@paginator, :kaminari)
|
36
|
+
end
|
31
37
|
|
32
38
|
RSpec.configure do |config|
|
33
39
|
config.include Rack::Test::Methods
|
@@ -7,4 +7,8 @@ shared_examples 'an endpoint with existing Link headers' do
|
|
7
7
|
expect(links).to include('<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="next"')
|
8
8
|
expect(links).to include('<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="last"')
|
9
9
|
end
|
10
|
+
|
11
|
+
it 'should give a Total header' do
|
12
|
+
expect(total).to eq(30)
|
13
|
+
end
|
10
14
|
end
|
@@ -14,4 +14,8 @@ shared_examples 'an endpoint with a first page' do
|
|
14
14
|
it 'should give a link with rel "next"' do
|
15
15
|
expect(links).to include('<http://example.org/numbers?count=100&page=2>; rel="next"')
|
16
16
|
end
|
17
|
+
|
18
|
+
it 'should give a Total header' do
|
19
|
+
expect(total).to eq(100)
|
20
|
+
end
|
17
21
|
end
|
@@ -14,4 +14,8 @@ shared_examples 'an endpoint with a last page' do
|
|
14
14
|
it 'should give a link with rel "prev"' do
|
15
15
|
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="prev"')
|
16
16
|
end
|
17
|
+
|
18
|
+
it 'should give a Total header' do
|
19
|
+
expect(total).to eq(100)
|
20
|
+
end
|
17
21
|
end
|
@@ -5,4 +5,8 @@ shared_examples 'an endpoint with a middle page' do
|
|
5
5
|
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="next"')
|
6
6
|
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="prev"')
|
7
7
|
end
|
8
|
+
|
9
|
+
it 'should give a Total header' do
|
10
|
+
expect(total).to eq(100)
|
11
|
+
end
|
8
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|