api-pagination 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39ada68101786c8ae919218460cd300a41c03dab
4
- data.tar.gz: e738863fb5ca287442afa734d7b87a01c0173e86
3
+ metadata.gz: 9a9271269dab6d4d3b99562c1262975c14c76efc
4
+ data.tar.gz: 3f98efbf74253877c9c2e0e99da24ac5a798e041
5
5
  SHA512:
6
- metadata.gz: 332904cbbbcb0bbfee70edab279f0aecb400d8c3d4e8b76db5ecbec51ff532132467060b5b8a238436c846ab8b4f48b5927eb8575bf1878cc31e74227a8bcb0f
7
- data.tar.gz: 3d9bad3a6aff7918d9f104abb8fa351566c7d8c745afe0178d0bb1afed61a611bb95a01c7bb7abef07687ec82b747421d2e663ca1aa7e615e3b3aab9ce8793c7
6
+ metadata.gz: 84f99de16f079a1a9e5acc30f3405b8798b2d5f215a022bb7cc5d3b98990bad16f3318aa2ae04fe234d454905144604091b31cea3aa5b1cb36fb3bb9f790fcab
7
+ data.tar.gz: c122d6e8e2fec923b1ca0ad30eed0bc6d502a64c34c197c51b9de75e3cafe2e8c6fa6b89bbe514aa0bb57380910b67915569915a80c5caa227741a0b7632b574
@@ -3,19 +3,16 @@ require 'api-pagination/version'
3
3
 
4
4
  module ApiPagination
5
5
  class << self
6
- attr_writer :kaminari
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
- if ApiPagination.kaminari?
12
+ case ApiPagination.paginator
13
+ when :kaminari
17
14
  collection.page(options[:page]).per(options[:per_page]).tap(&block)
18
- elsif ApiPagination.will_paginate?
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
 
@@ -26,12 +26,12 @@ module ApiPagination
26
26
  def last_page?() !next_page end
27
27
  end
28
28
 
29
- ApiPagination.will_paginate = true
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 = true
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
- MAJOR = 2
3
- MINOR = 0
4
- PATCH = 1
2
+ class Version
3
+ MAJOR = 2
4
+ MINOR = 1
5
+ PATCH = 0
5
6
 
6
- VERSION = [MAJOR, MINOR, PATCH].join('.')
7
+ def self.to_s
8
+ [MAJOR, MINOR, PATCH].join('.')
9
+ end
10
+ end
11
+
12
+ VERSION = Version.to_s
7
13
  end
@@ -15,6 +15,7 @@ module Grape
15
15
  end
16
16
 
17
17
  header 'Link', links.join(', ') unless links.empty?
18
+ header 'Total', ApiPagination.total_from(collection)
18
19
  end
19
20
 
20
21
  ApiPagination.paginate(collection, params, &block)
@@ -15,7 +15,8 @@ module Rails
15
15
  links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
16
16
  end
17
17
 
18
- headers['Link'] = links.join(', ') unless links.empty?
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)
@@ -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
@@ -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
@@ -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
- ApiPagination.kaminari = ENV['PAGINATOR'] == 'kaminari'
30
- ApiPagination.will_paginate = ENV['PAGINATOR'] == 'will_paginate'
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.1
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-01-17 00:00:00.000000000 Z
11
+ date: 2014-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec