paginate-responder 1.5.0 → 1.6.0.b0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +13 -7
- data/Appraisals +15 -0
- data/Gemfile +0 -6
- data/Rakefile +6 -10
- data/gemfiles/rails_3_2.gemfile +7 -0
- data/gemfiles/rails_4_2.gemfile +7 -0
- data/gemfiles/rails_5_0.gemfile +7 -0
- data/gemfiles/rails_head.gemfile +7 -0
- data/lib/paginate-responder.rb +5 -10
- data/lib/paginate-responder/base.rb +129 -0
- data/lib/paginate-responder/kaminari_adapter.rb +37 -0
- data/lib/paginate-responder/version.rb +2 -2
- data/lib/paginate-responder/will_paginate_adapter.rb +36 -0
- data/lib/responders/paginate_responder.rb +25 -5
- data/paginate-responder.gemspec +9 -4
- data/spec/paginate_responder_spec.rb +158 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/05-setup-and-teardown-adapter.rb +38 -0
- data/{test/test_helper.rb → spec/support/10-application.rb} +28 -28
- metadata +101 -26
- data/gemfiles/Gemfile.rails-4-1 +0 -7
- data/gemfiles/Gemfile.rails-4-2 +0 -8
- data/gemfiles/Gemfile.rails-head +0 -6
- data/lib/paginate-responder/adapter/base.rb +0 -46
- data/lib/paginate-responder/adapter/kaminari_adapter.rb +0 -34
- data/lib/paginate-responder/adapter/paginate_adapter.rb +0 -31
- data/lib/paginate-responder/paginator.rb +0 -99
- data/test/paginate_responder_test.rb +0 -247
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa30c367c32405ca6cc56ccee9ab1cad82e8894b
|
4
|
+
data.tar.gz: 5dd939187d91b5af8c9c35ac483a9d3c56d0b092
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be00e015691aeadf0d5174ee2010cb7b73e40ffc9c9c79ee6a6cef87a1617c065d85cd747f03d27d1f8ac99c6915ec7d526ecf06b3e38b6b428072c6a4a67747
|
7
|
+
data.tar.gz: 404cf3ad70bbfd40ee53a0c30268ccaaa922257022345f1d12d387580ec94a91d20726623032497fe2f16090edde4200ec7cba48f3d2e320adaf378dd5c7f323
|
data/.travis.yml
CHANGED
@@ -1,16 +1,22 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
|
-
- '2.3.
|
5
|
-
- '2.2'
|
6
|
-
- '2.1'
|
4
|
+
- '2.3.1'
|
5
|
+
- '2.2.5'
|
6
|
+
- '2.1.10'
|
7
7
|
env:
|
8
8
|
- GEM=will_paginate
|
9
9
|
- GEM=kaminari
|
10
10
|
gemfile:
|
11
|
-
- gemfiles/
|
12
|
-
- gemfiles/
|
13
|
-
- gemfiles/
|
11
|
+
- gemfiles/rails_3_2.gemfile
|
12
|
+
- gemfiles/rails_4_2.gemfile
|
13
|
+
- gemfiles/rails_5_0.gemfile
|
14
|
+
- gemfiles/rails_head.gemfile
|
14
15
|
matrix:
|
16
|
+
exclude:
|
17
|
+
- rvm: '2.1.10'
|
18
|
+
gemfile: gemfiles/rails_5_0.gemfile
|
19
|
+
- rvm: '2.1.10'
|
20
|
+
gemfile: gemfiles/rails_head.gemfile
|
15
21
|
allow_failures:
|
16
|
-
- gemfile: gemfiles/
|
22
|
+
- gemfile: gemfiles/rails_head.gemfile
|
data/Appraisals
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
appraise "rails-3-2" do
|
2
|
+
gem 'rails', '~> 3.2.0'
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise "rails-4-2" do
|
6
|
+
gem 'rails', '~> 4.2.0'
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise "rails-5-0" do
|
10
|
+
gem 'rails', '~> 5.0.0'
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise "rails-head" do
|
14
|
+
gem 'rails', github: 'rails/rails'
|
15
|
+
end
|
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -1,11 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require '
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
t.test_files = FileList['test/*_test.rb']
|
7
|
-
t.verbose = true
|
8
|
-
end
|
9
|
-
|
10
|
-
task :default => :test
|
11
|
-
task :spec => :test
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
task default: :spec
|
data/lib/paginate-responder.rb
CHANGED
@@ -1,17 +1,12 @@
|
|
1
1
|
require 'rack-link_headers'
|
2
2
|
|
3
3
|
module Responders
|
4
|
-
|
4
|
+
require 'responders/paginate_responder'
|
5
5
|
end
|
6
6
|
|
7
7
|
module PaginateResponder
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
autoload :Base, 'paginate-responder/adapter/base'
|
13
|
-
end
|
8
|
+
require 'paginate-responder/version'
|
9
|
+
require 'paginate-responder/base'
|
10
|
+
require 'paginate-responder/will_paginate_adapter'
|
11
|
+
require 'paginate-responder/kaminari_adapter'
|
14
12
|
end
|
15
|
-
|
16
|
-
require 'paginate-responder/adapter/paginate_adapter'
|
17
|
-
require 'paginate-responder/adapter/kaminari_adapter'
|
@@ -0,0 +1,129 @@
|
|
1
|
+
module PaginateResponder
|
2
|
+
class Base
|
3
|
+
attr_reader :responder, :resource
|
4
|
+
|
5
|
+
def initialize(responder)
|
6
|
+
@responder = responder
|
7
|
+
@resource = responder.resource
|
8
|
+
end
|
9
|
+
|
10
|
+
def request
|
11
|
+
responder.request
|
12
|
+
end
|
13
|
+
|
14
|
+
def controller
|
15
|
+
responder.controller
|
16
|
+
end
|
17
|
+
|
18
|
+
def response
|
19
|
+
controller.response
|
20
|
+
end
|
21
|
+
|
22
|
+
def paginate!
|
23
|
+
@resource = paginate
|
24
|
+
|
25
|
+
update
|
26
|
+
|
27
|
+
@resource
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def paginate
|
33
|
+
resource
|
34
|
+
end
|
35
|
+
|
36
|
+
def update
|
37
|
+
link! 'first', page: first_page if first_page
|
38
|
+
link! 'prev', page: prev_page if prev_page
|
39
|
+
link! 'next', page: next_page if next_page
|
40
|
+
link! 'last', page: last_page if last_page
|
41
|
+
|
42
|
+
response.headers["X-Total-Pages"] = total_pages.to_s if total_pages
|
43
|
+
response.headers["X-Total-Count"] = total_count.to_s if total_count
|
44
|
+
response.headers["X-Per-Page"] = per_page.to_s if per_page
|
45
|
+
end
|
46
|
+
|
47
|
+
def page
|
48
|
+
@page ||= begin
|
49
|
+
val ||= controller.page if controller.respond_to? :page
|
50
|
+
val ||= controller.params[:page]
|
51
|
+
val ||= first_page
|
52
|
+
cast_page val
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def cast_page(page)
|
57
|
+
Integer(page)
|
58
|
+
end
|
59
|
+
|
60
|
+
def per_page
|
61
|
+
@per_page ||= begin
|
62
|
+
val ||= controller.per_page if controller.respond_to? :per_page
|
63
|
+
val ||= controller.params[:per_page].try(:to_i)
|
64
|
+
val ||= default_per_page
|
65
|
+
val < 1 ? 1 : (val > max_per_page) ? max_per_page : val
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def max_per_page
|
70
|
+
@max_per_page ||= begin
|
71
|
+
val ||= controller.max_per_page if controller.respond_to? :max_per_page
|
72
|
+
val ||= default_max_per_page
|
73
|
+
val
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def first_page
|
78
|
+
1
|
79
|
+
end
|
80
|
+
|
81
|
+
def prev_page
|
82
|
+
page - 1 if page > 1
|
83
|
+
end
|
84
|
+
|
85
|
+
def next_page
|
86
|
+
page + 1 if total_pages && page < total_pages
|
87
|
+
end
|
88
|
+
|
89
|
+
def last_page
|
90
|
+
total_pages
|
91
|
+
end
|
92
|
+
|
93
|
+
def total_pages
|
94
|
+
nil
|
95
|
+
end
|
96
|
+
|
97
|
+
def total_count
|
98
|
+
nil
|
99
|
+
end
|
100
|
+
|
101
|
+
def default_per_page
|
102
|
+
50
|
103
|
+
end
|
104
|
+
|
105
|
+
def default_max_per_page
|
106
|
+
100
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def link!(rel, params)
|
112
|
+
if request.params.key?(:per_page)
|
113
|
+
params = {per_page: per_page}.merge(params)
|
114
|
+
end
|
115
|
+
|
116
|
+
params = request.params.merge(params)
|
117
|
+
|
118
|
+
url = controller.url_for(params)
|
119
|
+
|
120
|
+
response.link(url, rel: rel)
|
121
|
+
end
|
122
|
+
|
123
|
+
class << self
|
124
|
+
def suitable?(resource)
|
125
|
+
false
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module PaginateResponder
|
2
|
+
#
|
3
|
+
# Pagination adapter for kaminari.
|
4
|
+
#
|
5
|
+
class KaminariAdapter < Base
|
6
|
+
def paginate
|
7
|
+
resource.page(page).per(per_page)
|
8
|
+
end
|
9
|
+
|
10
|
+
def default_per_page
|
11
|
+
Kaminari.config.default_per_page
|
12
|
+
end
|
13
|
+
|
14
|
+
def default_max_per_page
|
15
|
+
Kaminari.config.max_per_page
|
16
|
+
end
|
17
|
+
|
18
|
+
def total_pages
|
19
|
+
return resource.total_pages if resource.respond_to? :total_pages
|
20
|
+
return resource.num_pages if resource.respond_to? :num_pages
|
21
|
+
end
|
22
|
+
|
23
|
+
def total_count
|
24
|
+
resource.total_count if resource.respond_to? :total_count
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
def suitable?(resource, responder)
|
29
|
+
resource.respond_to?(:page) and not resource.respond_to?(:paginate)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if defined?(:Kaminari)
|
35
|
+
::Responders::PaginateResponder.register KaminariAdapter
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module PaginateResponder
|
2
|
+
#
|
3
|
+
# Pagination adapter for will_paginate.
|
4
|
+
#
|
5
|
+
class WillPaginateAdapter < Base
|
6
|
+
def paginate
|
7
|
+
resource.paginate page: page, per_page: per_page
|
8
|
+
end
|
9
|
+
|
10
|
+
def total_pages
|
11
|
+
resource.total_pages if resource.respond_to? :total_pages
|
12
|
+
end
|
13
|
+
|
14
|
+
def total_count
|
15
|
+
resource.total_entries if resource.respond_to? :total_entries
|
16
|
+
end
|
17
|
+
|
18
|
+
def default_per_page
|
19
|
+
50
|
20
|
+
end
|
21
|
+
|
22
|
+
def default_max_per_page
|
23
|
+
50
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
def suitable?(resource, responder)
|
28
|
+
resource.respond_to? :paginate
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
if defined?(:WillPaginate)
|
34
|
+
::Responders::PaginateResponder.register WillPaginateAdapter
|
35
|
+
end
|
36
|
+
end
|
@@ -1,12 +1,32 @@
|
|
1
1
|
module Responders
|
2
|
-
|
3
2
|
module PaginateResponder
|
4
|
-
|
5
3
|
def to_format
|
6
|
-
if get?
|
7
|
-
|
8
|
-
end
|
4
|
+
paginate! if get?
|
5
|
+
|
9
6
|
super
|
10
7
|
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def paginate!
|
12
|
+
adapter = ::Responders::PaginateResponder.find(self)
|
13
|
+
@resource = adapter.new(self).paginate! if adapter
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def register(adapter)
|
18
|
+
adapters << adapter
|
19
|
+
end
|
20
|
+
|
21
|
+
def adapters
|
22
|
+
@adpaters ||= ::Set.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def find(responder)
|
26
|
+
adapters.find do |adapter|
|
27
|
+
adapter.suitable?(responder.resource, responder)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
11
31
|
end
|
12
32
|
end
|
data/paginate-responder.gemspec
CHANGED
@@ -20,12 +20,17 @@ Gem::Specification.new do |gem|
|
|
20
20
|
|
21
21
|
gem.add_dependency 'rack-link_headers', '>= 2.2'
|
22
22
|
|
23
|
-
gem.add_development_dependency 'rake'
|
24
23
|
gem.add_development_dependency 'actionpack', '>= 3.2.0'
|
25
24
|
gem.add_development_dependency 'activerecord', '>= 3.2.0'
|
25
|
+
gem.add_development_dependency 'appraisal'
|
26
|
+
gem.add_development_dependency 'kaminari'
|
27
|
+
gem.add_development_dependency 'pry'
|
28
|
+
gem.add_development_dependency 'pry-byebug'
|
29
|
+
gem.add_development_dependency 'rake'
|
30
|
+
gem.add_development_dependency 'responders'
|
31
|
+
gem.add_development_dependency 'rspec'
|
32
|
+
gem.add_development_dependency 'rspec-rails'
|
26
33
|
gem.add_development_dependency 'sqlite3'
|
27
|
-
gem.add_development_dependency 'minitest'
|
28
|
-
gem.add_development_dependency 'minitest-reporters'
|
29
34
|
gem.add_development_dependency 'will_paginate'
|
30
|
-
gem.add_development_dependency '
|
35
|
+
gem.add_development_dependency 'test-unit'
|
31
36
|
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Responders::PaginateResponder, type: :controller do
|
4
|
+
case $gem
|
5
|
+
when 'will_paginate'
|
6
|
+
let(:array_resource) do
|
7
|
+
('AA'..'zz').to_a
|
8
|
+
end
|
9
|
+
when 'kaminari'
|
10
|
+
let(:array_resource) do
|
11
|
+
Kaminari.paginate_array ('AA'..'zz').to_a
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
before do
|
16
|
+
if ActiveRecord::VERSION::MAJOR >= 4
|
17
|
+
self.resource = ArModel.all
|
18
|
+
else
|
19
|
+
self.resource = ArModel.scoped
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:response) { action.call; @response }
|
24
|
+
let(:json) { JSON.parse response.body }
|
25
|
+
let(:params) { {format: :json} }
|
26
|
+
|
27
|
+
if ActionPack::VERSION::MAJOR >= 5
|
28
|
+
let(:action) { -> { get :index, params: params } }
|
29
|
+
else
|
30
|
+
let(:action) { -> { get :index, params } }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with AR resource' do
|
34
|
+
it { expect(json).to eq (1..50).to_a }
|
35
|
+
|
36
|
+
describe '?page' do
|
37
|
+
let(:params) { super().merge page: 2 }
|
38
|
+
|
39
|
+
it { expect(json).to eq (51..100).to_a }
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '?per_page' do
|
43
|
+
let(:params) { super().merge per_page: 10 }
|
44
|
+
|
45
|
+
it { expect(json).to eq (1..10).to_a }
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '?per_page&page' do
|
49
|
+
let(:params) { super().merge page: 2, per_page: 10 }
|
50
|
+
|
51
|
+
it { expect(json).to eq (11..20).to_a }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'with array resource' do
|
56
|
+
before { @controller.resource = array_resource }
|
57
|
+
|
58
|
+
it { expect(json).to eq ('AA'..'zz').to_a[0..49] }
|
59
|
+
|
60
|
+
describe '?page' do
|
61
|
+
let(:params) { super().merge page: 2 }
|
62
|
+
|
63
|
+
it { expect(json).to eq ('AA'..'zz').to_a[50..99] }
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '?per_page' do
|
67
|
+
let(:params) { super().merge per_page: 10 }
|
68
|
+
|
69
|
+
it { expect(json).to eq ('AA'..'zz').to_a[0..9] }
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '?per_page&page' do
|
73
|
+
let(:params) { super().merge page: 2, per_page: 10 }
|
74
|
+
|
75
|
+
it { expect(json).to eq ('AA'..'zz').to_a[10..19] }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with AR association' do
|
80
|
+
before { @controller.resource = ArModel.find(1).ar_assoc_models }
|
81
|
+
|
82
|
+
it { expect(json).to eq (1..5).to_a }
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'links' do
|
86
|
+
subject { response.links.map{|l| [l[:params][:rel], l[:url]]}.to_h }
|
87
|
+
|
88
|
+
it { expect(subject.size).to eq 3 }
|
89
|
+
it { is_expected.to include 'first' => 'http://test.host/index.json?page=1' }
|
90
|
+
it { is_expected.to include 'next' => 'http://test.host/index.json?page=2' }
|
91
|
+
it { is_expected.to include 'last' => 'http://test.host/index.json?page=14' }
|
92
|
+
|
93
|
+
context '?page' do
|
94
|
+
let(:params) { super().merge page: 2 }
|
95
|
+
|
96
|
+
it { expect(subject.size).to eq 4 }
|
97
|
+
it { is_expected.to include 'first' => 'http://test.host/index.json?page=1' }
|
98
|
+
it { is_expected.to include 'prev' => 'http://test.host/index.json?page=1' }
|
99
|
+
it { is_expected.to include 'next' => 'http://test.host/index.json?page=3' }
|
100
|
+
it { is_expected.to include 'last' => 'http://test.host/index.json?page=14' }
|
101
|
+
end
|
102
|
+
|
103
|
+
context '?page=5' do
|
104
|
+
let(:params) { super().merge page: 5 }
|
105
|
+
|
106
|
+
it { expect(subject.size).to eq 4 }
|
107
|
+
it { is_expected.to include 'first' => 'http://test.host/index.json?page=1' }
|
108
|
+
it { is_expected.to include 'prev' => 'http://test.host/index.json?page=4' }
|
109
|
+
it { is_expected.to include 'next' => 'http://test.host/index.json?page=6' }
|
110
|
+
it { is_expected.to include 'last' => 'http://test.host/index.json?page=14' }
|
111
|
+
end
|
112
|
+
|
113
|
+
context '?page last' do
|
114
|
+
let(:params) { super().merge page: 14 }
|
115
|
+
|
116
|
+
it { expect(subject.size).to eq 3 }
|
117
|
+
it { is_expected.to include 'first' => 'http://test.host/index.json?page=1' }
|
118
|
+
it { is_expected.to include 'prev' => 'http://test.host/index.json?page=13' }
|
119
|
+
it { is_expected.to include 'last' => 'http://test.host/index.json?page=14' }
|
120
|
+
end
|
121
|
+
|
122
|
+
context '?page before last' do
|
123
|
+
let(:params) { super().merge page: 13 }
|
124
|
+
|
125
|
+
it { expect(subject.size).to eq 4 }
|
126
|
+
it { is_expected.to include 'first' => 'http://test.host/index.json?page=1' }
|
127
|
+
it { is_expected.to include 'prev' => 'http://test.host/index.json?page=12' }
|
128
|
+
it { is_expected.to include 'next' => 'http://test.host/index.json?page=14' }
|
129
|
+
it { is_expected.to include 'last' => 'http://test.host/index.json?page=14' }
|
130
|
+
end
|
131
|
+
|
132
|
+
context '?per_page' do
|
133
|
+
let(:params) { super().merge page: 1, per_page: 10 }
|
134
|
+
|
135
|
+
it { expect(subject.size).to eq 3 }
|
136
|
+
it { is_expected.to include 'first' => 'http://test.host/index.json?page=1&per_page=10' }
|
137
|
+
it { is_expected.to include 'next' => 'http://test.host/index.json?page=2&per_page=10' }
|
138
|
+
it { is_expected.to include 'last' => 'http://test.host/index.json?page=68&per_page=10' }
|
139
|
+
end
|
140
|
+
|
141
|
+
context '?per_page above max per page limit' do
|
142
|
+
let(:params) { super().merge page: 1, per_page: 100 }
|
143
|
+
|
144
|
+
it { expect(subject.size).to eq 3 }
|
145
|
+
it { is_expected.to include 'first' => 'http://test.host/index.json?page=1&per_page=50' }
|
146
|
+
it { is_expected.to include 'next' => 'http://test.host/index.json?page=2&per_page=50' }
|
147
|
+
it { is_expected.to include 'last' => 'http://test.host/index.json?page=14&per_page=50' }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe 'headers' do
|
152
|
+
subject { response.headers.to_h }
|
153
|
+
|
154
|
+
it { is_expected.to include 'X-Total-Pages' => '14' }
|
155
|
+
it { is_expected.to include 'X-Total-Count' => '676' }
|
156
|
+
it { is_expected.to include 'X-Per-Page' => '50' }
|
157
|
+
end
|
158
|
+
end
|