bootstrap_pager 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +18 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +9 -0
- data/.idea/pager.iml +50 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +307 -0
- data/Rakefile +42 -0
- data/app/assets/javascripts/infinitescroll.js +8 -0
- data/app/helpers/pager_helper.rb +5 -0
- data/app/views/pager/_first_page.html.erb +11 -0
- data/app/views/pager/_first_page.html.haml +9 -0
- data/app/views/pager/_first_page.html.slim +10 -0
- data/app/views/pager/_gap.html.erb +8 -0
- data/app/views/pager/_gap.html.haml +8 -0
- data/app/views/pager/_gap.html.slim +10 -0
- data/app/views/pager/_last_page.html.erb +11 -0
- data/app/views/pager/_last_page.html.haml +9 -0
- data/app/views/pager/_last_page.html.slim +10 -0
- data/app/views/pager/_next_page.html.erb +11 -0
- data/app/views/pager/_next_page.html.haml +9 -0
- data/app/views/pager/_next_page.html.slim +10 -0
- data/app/views/pager/_page.html.erb +14 -0
- data/app/views/pager/_page.html.haml +11 -0
- data/app/views/pager/_page.html.slim +13 -0
- data/app/views/pager/_paginator.html.erb +25 -0
- data/app/views/pager/_paginator.html.haml +19 -0
- data/app/views/pager/_paginator.html.slim +20 -0
- data/app/views/pager/_prev_page.html.erb +11 -0
- data/app/views/pager/_prev_page.html.haml +9 -0
- data/app/views/pager/_prev_page.html.slim +10 -0
- data/bootstrap_pager.gemspec +36 -0
- data/config/locales/pager.yml +19 -0
- data/gemfiles/active_record_30.gemfile +9 -0
- data/gemfiles/active_record_31.gemfile +7 -0
- data/gemfiles/active_record_32.gemfile +10 -0
- data/gemfiles/active_record_40.gemfile +8 -0
- data/gemfiles/active_record_edge.gemfile +11 -0
- data/gemfiles/data_mapper_12.gemfile +15 -0
- data/gemfiles/mongo_mapper.gemfile +10 -0
- data/gemfiles/mongoid_24.gemfile +7 -0
- data/gemfiles/mongoid_30.gemfile +12 -0
- data/gemfiles/sinatra_13.gemfile +13 -0
- data/gemfiles/sinatra_14.gemfile +13 -0
- data/lib/bootstrap_pager/config.rb +51 -0
- data/lib/bootstrap_pager/grape.rb +4 -0
- data/lib/bootstrap_pager/helpers/action_view_extension.rb +152 -0
- data/lib/bootstrap_pager/helpers/paginator.rb +186 -0
- data/lib/bootstrap_pager/helpers/sinatra_helpers.rb +144 -0
- data/lib/bootstrap_pager/helpers/tags.rb +96 -0
- data/lib/bootstrap_pager/hooks.rb +41 -0
- data/lib/bootstrap_pager/models/active_record_extension.rb +22 -0
- data/lib/bootstrap_pager/models/active_record_model_extension.rb +20 -0
- data/lib/bootstrap_pager/models/active_record_relation_methods.rb +29 -0
- data/lib/bootstrap_pager/models/array_extension.rb +58 -0
- data/lib/bootstrap_pager/models/configuration_methods.rb +48 -0
- data/lib/bootstrap_pager/models/data_mapper_collection_methods.rb +15 -0
- data/lib/bootstrap_pager/models/data_mapper_extension.rb +48 -0
- data/lib/bootstrap_pager/models/mongo_mapper_extension.rb +18 -0
- data/lib/bootstrap_pager/models/mongoid_criteria_methods.rb +23 -0
- data/lib/bootstrap_pager/models/mongoid_extension.rb +33 -0
- data/lib/bootstrap_pager/models/page_scope_methods.rb +70 -0
- data/lib/bootstrap_pager/models/plucky_criteria_methods.rb +18 -0
- data/lib/bootstrap_pager/railtie.rb +7 -0
- data/lib/bootstrap_pager/sinatra.rb +5 -0
- data/lib/bootstrap_pager/version.rb +3 -0
- data/lib/bootstrap_pager.rb +38 -0
- data/lib/generators/pager/config_generator.rb +16 -0
- data/lib/generators/pager/templates/pager_config.rb +10 -0
- data/lib/generators/pager/views_generator.rb +118 -0
- data/spec/config/config_spec.rb +91 -0
- data/spec/fake_app/active_record/config.rb +3 -0
- data/spec/fake_app/active_record/models.rb +57 -0
- data/spec/fake_app/data_mapper/config.rb +7 -0
- data/spec/fake_app/data_mapper/models.rb +27 -0
- data/spec/fake_app/log/development.log +832 -0
- data/spec/fake_app/mongo_mapper/config.rb +2 -0
- data/spec/fake_app/mongo_mapper/models.rb +9 -0
- data/spec/fake_app/mongoid/config.rb +16 -0
- data/spec/fake_app/mongoid/models.rb +22 -0
- data/spec/fake_app/rails_app.rb +67 -0
- data/spec/fake_app/sinatra_app.rb +22 -0
- data/spec/fake_gem.rb +4 -0
- data/spec/helpers/action_view_extension_spec.rb +292 -0
- data/spec/helpers/helpers_spec.rb +135 -0
- data/spec/helpers/sinatra_helpers_spec.rb +170 -0
- data/spec/helpers/tags_spec.rb +140 -0
- data/spec/models/active_record/active_record_relation_methods_spec.rb +47 -0
- data/spec/models/active_record/default_per_page_spec.rb +32 -0
- data/spec/models/active_record/max_pages_spec.rb +23 -0
- data/spec/models/active_record/max_per_page_spec.rb +32 -0
- data/spec/models/active_record/scopes_spec.rb +242 -0
- data/spec/models/array_spec.rb +150 -0
- data/spec/models/data_mapper/data_mapper_spec.rb +207 -0
- data/spec/models/mongo_mapper/mongo_mapper_spec.rb +84 -0
- data/spec/models/mongoid/mongoid_spec.rb +126 -0
- data/spec/requests/users_spec.rb +53 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/spec_helper_for_sinatra.rb +34 -0
- data/spec/support/database_cleaner.rb +16 -0
- data/spec/support/matchers.rb +52 -0
- data/vendor/assets/javascripts/jquery.infinitescroll.js +814 -0
- data/vendor/assets/javascripts/jquery.infinitescroll.min.js +1 -0
- metadata +311 -0
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? Sinatra
|
4
|
+
ERB_TEMPLATE_FOR_PAGINATE = <<EOT
|
5
|
+
<div>
|
6
|
+
<ul>
|
7
|
+
<% @users.each do |user| %>
|
8
|
+
<li class="user_info"><%= user.id %></li>
|
9
|
+
<% end %>
|
10
|
+
</ul>
|
11
|
+
<%= paginate @users, @options %>
|
12
|
+
</div>
|
13
|
+
EOT
|
14
|
+
|
15
|
+
ERB_TEMPLATE_FOR_NEXT_PAGE = <<EOT
|
16
|
+
<div>
|
17
|
+
<ul>
|
18
|
+
<% @users.each do |user| %>
|
19
|
+
<li class="user_info"><%= user.id %></li>
|
20
|
+
<% end %>
|
21
|
+
</ul>
|
22
|
+
<%= link_to_next_page(@users, "Next!", {:id => 'next_page_link'}.merge(@options || {})) %>
|
23
|
+
</div>
|
24
|
+
EOT
|
25
|
+
|
26
|
+
describe 'BootstrapPager::Helpers::SinatraHelper' do
|
27
|
+
before do
|
28
|
+
50.times {|i| User.create! :name => "user#{i}"}
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#paginate' do
|
32
|
+
before do
|
33
|
+
mock_app do
|
34
|
+
register BootstrapPager::Helpers::SinatraHelpers
|
35
|
+
get '/users' do
|
36
|
+
@page = params[:page] || 1
|
37
|
+
@users = User.page(@page)
|
38
|
+
@options = {}
|
39
|
+
erb ERB_TEMPLATE_FOR_PAGINATE
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'normal paginations with Sinatra' do
|
45
|
+
before { get '/users' }
|
46
|
+
|
47
|
+
it 'should have a navigation tag' do
|
48
|
+
last_document.search('nav.pagination').should_not be_empty
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should have pagination links' do
|
52
|
+
last_document.search('.page a').should have_at_least(1).items
|
53
|
+
last_document.search('.next a').should have_at_least(1).items
|
54
|
+
last_document.search('.last a').should have_at_least(1).items
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should point to current page' do
|
58
|
+
last_document.search('.current').text.should match(/1/)
|
59
|
+
|
60
|
+
get '/users?page=2'
|
61
|
+
last_document.search('.current').text.should match(/2/)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should load 25 users' do
|
65
|
+
last_document.search('li.user_info').should have(25).items
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should preserve params' do
|
69
|
+
get '/users?foo=bar'
|
70
|
+
last_document.search('.page a').should(be_all do |elm|
|
71
|
+
elm.attribute('href').value =~ /foo=bar/
|
72
|
+
end)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'optional paginations with Sinatra' do
|
77
|
+
it 'should have 5 windows with 1 gap' do
|
78
|
+
mock_app do
|
79
|
+
register BootstrapPager::Helpers::SinatraHelpers
|
80
|
+
get '/users' do
|
81
|
+
@page = params[:page] || 1
|
82
|
+
@users = User.page(@page).per(5)
|
83
|
+
@options = {}
|
84
|
+
erb ERB_TEMPLATE_FOR_PAGINATE
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
get '/users'
|
89
|
+
last_document.search('.page').should have(6).items
|
90
|
+
last_document.search('.gap').should have(1).item
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should controll the inner window size' do
|
94
|
+
mock_app do
|
95
|
+
register BootstrapPager::Helpers::SinatraHelpers
|
96
|
+
get '/users' do
|
97
|
+
@page = params[:page] || 1
|
98
|
+
@users = User.page(@page).per(3)
|
99
|
+
@options = {:window => 10}
|
100
|
+
erb ERB_TEMPLATE_FOR_PAGINATE
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
get '/users'
|
105
|
+
last_document.search('.page').should have(12).items
|
106
|
+
last_document.search('.gap').should have(1).item
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should specify a page param name' do
|
110
|
+
mock_app do
|
111
|
+
register BootstrapPager::Helpers::SinatraHelpers
|
112
|
+
get '/users' do
|
113
|
+
@page = params[:page] || 1
|
114
|
+
@users = User.page(@page).per(3)
|
115
|
+
@options = {:param_name => :user_page}
|
116
|
+
erb ERB_TEMPLATE_FOR_PAGINATE
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
get '/users'
|
121
|
+
last_document.search('.page a').should(be_all do |elm|
|
122
|
+
elm.attribute('href').value =~ /user_page=\d+/
|
123
|
+
end)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe '#link_to_next_page' do
|
129
|
+
before do
|
130
|
+
mock_app do
|
131
|
+
register BootstrapPager::Helpers::SinatraHelpers
|
132
|
+
get '/users' do
|
133
|
+
@page = params[:page] || 1
|
134
|
+
@users = User.page(@page)
|
135
|
+
erb ERB_TEMPLATE_FOR_NEXT_PAGE
|
136
|
+
end
|
137
|
+
|
138
|
+
get '/users_placeholder' do
|
139
|
+
@page = params[:page] || 1
|
140
|
+
@options = {:placeholder => %{<span id='no_next_page'>No Next Page</span>}}
|
141
|
+
@users = User.page(@page)
|
142
|
+
erb ERB_TEMPLATE_FOR_NEXT_PAGE
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'having more page' do
|
148
|
+
it 'should have a more page link' do
|
149
|
+
get '/users'
|
150
|
+
last_document.search('a#next_page_link').should be_present
|
151
|
+
last_document.search('a#next_page_link').text.should match(/Next!/)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'the last page' do
|
156
|
+
it 'should not have a more page link' do
|
157
|
+
get '/users?page=2'
|
158
|
+
last_document.search('a#next_page_link').should be_empty
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should have a no more page notation using placeholder' do
|
162
|
+
get '/users_placeholder?page=2'
|
163
|
+
last_document.search('a#next_page_link').should be_empty
|
164
|
+
last_document.search('span#no_next_page').should be_present
|
165
|
+
last_document.search('span#no_next_page').text.should match(/No Next Page/)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
include BootstrapPager::Helpers
|
3
|
+
|
4
|
+
describe 'BootstrapPager::Helpers' do
|
5
|
+
describe 'Paginator' do
|
6
|
+
describe 'Paginator::PageProxy' do
|
7
|
+
describe '#current?' do
|
8
|
+
context 'current_page == page' do
|
9
|
+
subject { Paginator::PageProxy.new({:current_page => 26}, 26, nil) }
|
10
|
+
its(:current?) { should be_true }
|
11
|
+
end
|
12
|
+
context 'current_page != page' do
|
13
|
+
subject { Paginator::PageProxy.new({:current_page => 13}, 26, nil) }
|
14
|
+
its(:current?) { should_not be_true }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#first?' do
|
19
|
+
context 'page == 1' do
|
20
|
+
subject { Paginator::PageProxy.new({:current_page => 26}, 1, nil) }
|
21
|
+
its(:first?) { should be_true }
|
22
|
+
end
|
23
|
+
context 'page != 1' do
|
24
|
+
subject { Paginator::PageProxy.new({:current_page => 13}, 2, nil) }
|
25
|
+
its(:first?) { should_not be_true }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#last?' do
|
30
|
+
context 'current_page == page' do
|
31
|
+
subject { Paginator::PageProxy.new({:total_pages => 39}, 39, nil) }
|
32
|
+
its(:last?) { should be_true }
|
33
|
+
end
|
34
|
+
context 'current_page != page' do
|
35
|
+
subject { Paginator::PageProxy.new({:total_pages => 39}, 38, nil) }
|
36
|
+
its(:last?) { should_not be_true }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#next?' do
|
41
|
+
context 'page == current_page + 1' do
|
42
|
+
subject { Paginator::PageProxy.new({:current_page => 52}, 53, nil) }
|
43
|
+
its(:next?) { should be_true }
|
44
|
+
end
|
45
|
+
context 'page != current_page + 1' do
|
46
|
+
subject { Paginator::PageProxy.new({:current_page => 52}, 77, nil) }
|
47
|
+
its(:next?) { should_not be_true }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#prev?' do
|
52
|
+
context 'page == current_page - 1' do
|
53
|
+
subject { Paginator::PageProxy.new({:current_page => 77}, 76, nil) }
|
54
|
+
its(:prev?) { should be_true }
|
55
|
+
end
|
56
|
+
context 'page != current_page + 1' do
|
57
|
+
subject { Paginator::PageProxy.new({:current_page => 77}, 80, nil) }
|
58
|
+
its(:prev?) { should_not be_true }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#left_outer?' do
|
63
|
+
context 'current_page == left' do
|
64
|
+
subject { Paginator::PageProxy.new({:left => 3}, 3, nil) }
|
65
|
+
its(:left_outer?) { should be_true }
|
66
|
+
end
|
67
|
+
context 'current_page == left + 1' do
|
68
|
+
subject { Paginator::PageProxy.new({:left => 3}, 4, nil) }
|
69
|
+
its(:left_outer?) { should_not be_true }
|
70
|
+
end
|
71
|
+
context 'current_page == left + 2' do
|
72
|
+
subject { Paginator::PageProxy.new({:left => 3}, 5, nil) }
|
73
|
+
its(:left_outer?) { should_not be_true }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#right_outer?' do
|
78
|
+
context 'total_pages - page > right' do
|
79
|
+
subject { Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 6, nil) }
|
80
|
+
its(:right_outer?) { should_not be_true }
|
81
|
+
end
|
82
|
+
context 'total_pages - page == right' do
|
83
|
+
subject { Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 7, nil) }
|
84
|
+
its(:right_outer?) { should_not be_true }
|
85
|
+
end
|
86
|
+
context 'total_pages - page < right' do
|
87
|
+
subject { Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 8, nil) }
|
88
|
+
its(:right_outer?) { should be_true }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#inside_window?' do
|
93
|
+
context 'page > current_page' do
|
94
|
+
context 'page - current_page > window' do
|
95
|
+
subject { Paginator::PageProxy.new({:current_page => 4, :window => 5}, 10, nil) }
|
96
|
+
its(:inside_window?) { should_not be_true }
|
97
|
+
end
|
98
|
+
context 'page - current_page == window' do
|
99
|
+
subject { Paginator::PageProxy.new({:current_page => 4, :window => 6}, 10, nil) }
|
100
|
+
its(:inside_window?) { should be_true }
|
101
|
+
end
|
102
|
+
context 'page - current_page < window' do
|
103
|
+
subject { Paginator::PageProxy.new({:current_page => 4, :window => 7}, 10, nil) }
|
104
|
+
its(:inside_window?) { should be_true }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
context 'current_page > page' do
|
108
|
+
context 'current_page - page > window' do
|
109
|
+
subject { Paginator::PageProxy.new({:current_page => 15, :window => 4}, 10, nil) }
|
110
|
+
its(:inside_window?) { should_not be_true }
|
111
|
+
end
|
112
|
+
context 'current_page - page == window' do
|
113
|
+
subject { Paginator::PageProxy.new({:current_page => 15, :window => 5}, 10, nil) }
|
114
|
+
its(:inside_window?) { should be_true }
|
115
|
+
end
|
116
|
+
context 'current_page - page < window' do
|
117
|
+
subject { Paginator::PageProxy.new({:current_page => 15, :window => 6}, 10, nil) }
|
118
|
+
its(:inside_window?) { should be_true }
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
describe '#was_truncated?' do
|
123
|
+
before do
|
124
|
+
stub(@template = Object.new) do
|
125
|
+
options { {} }
|
126
|
+
params { {} }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
context 'last.is_a? Gap' do
|
130
|
+
subject { Paginator::PageProxy.new({}, 10, Gap.new(@template)) }
|
131
|
+
its(:was_truncated?) { should be_true }
|
132
|
+
end
|
133
|
+
context 'last.is not a Gap' do
|
134
|
+
subject { Paginator::PageProxy.new({}, 10, Page.new(@template)) }
|
135
|
+
its(:was_truncated?) { should_not be_true }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? ActiveRecord
|
4
|
+
describe BootstrapPager::ActiveRecordRelationMethods do
|
5
|
+
describe '#total_count' do
|
6
|
+
before do
|
7
|
+
@author = User.create! :name => 'author'
|
8
|
+
@author2 = User.create! :name => 'author2'
|
9
|
+
@author3 = User.create! :name => 'author3'
|
10
|
+
@books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
|
11
|
+
@books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
|
12
|
+
@books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
|
13
|
+
@readers = 4.times.map { User.create! :name => 'reader' }
|
14
|
+
@books.each {|book| book.readers << @readers }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when the scope includes an order which references a generated column" do
|
18
|
+
it "should successfully count the results" do
|
19
|
+
@author.readers.by_read_count.page(1).total_count.should == @readers.size
|
20
|
+
end
|
21
|
+
end
|
22
|
+
context "when the scope use conditions on includes" do
|
23
|
+
it "should keep includes and successfully count the results" do
|
24
|
+
# Only @author and @author2 have books titled with the title00x partern
|
25
|
+
User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
|
26
|
+
end
|
27
|
+
end
|
28
|
+
context "when total_count receives options" do
|
29
|
+
it "should return a distinct total count" do
|
30
|
+
User.page(1).total_count(:name, :distinct => true).should == 4
|
31
|
+
end
|
32
|
+
end
|
33
|
+
context "when count receives options" do
|
34
|
+
it "should return a distinct set by column" do
|
35
|
+
User.page(1).count(:name, :distinct => true).should == 4
|
36
|
+
end
|
37
|
+
end
|
38
|
+
context "when the scope returns an ActiveSupport::OrderedHash" do
|
39
|
+
it "should not throw exception by passing options to count" do
|
40
|
+
lambda {
|
41
|
+
@author.readers.by_read_count.page(1).total_count(:name, :distinct => true)
|
42
|
+
}.should_not raise_exception
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? ActiveRecord
|
4
|
+
|
5
|
+
describe 'default per_page' do
|
6
|
+
describe 'AR::Base' do
|
7
|
+
subject { ActiveRecord::Base }
|
8
|
+
it { should_not respond_to :paginates_per }
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { User.page 0 }
|
12
|
+
|
13
|
+
context 'by default' do
|
14
|
+
its(:limit_value) { should == 25 }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when explicitly set via paginates_per' do
|
18
|
+
before { User.paginates_per 1326 }
|
19
|
+
its(:limit_value) { should == 1326 }
|
20
|
+
after { User.paginates_per nil }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "default per_page value's independency per model" do
|
24
|
+
context "when User's default per_page was changed" do
|
25
|
+
before { User.paginates_per 1326 }
|
26
|
+
subject { Book.page 0 }
|
27
|
+
its(:limit_value) { should == 25 }
|
28
|
+
after { User.paginates_per nil }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? ActiveRecord
|
4
|
+
|
5
|
+
describe 'max pages' do
|
6
|
+
describe 'AR::Base' do
|
7
|
+
subject { ActiveRecord::Base }
|
8
|
+
it { should_not respond_to :max_pages_per }
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { User.page 0 }
|
12
|
+
|
13
|
+
context 'by default' do
|
14
|
+
its(:max_pages) { should == nil }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when explicitly set via max_pages_per' do
|
18
|
+
before { User.max_pages_per 3 }
|
19
|
+
its(:max_pages) { should == 3 }
|
20
|
+
after { User.max_pages_per nil }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? ActiveRecord
|
4
|
+
|
5
|
+
describe 'max per_page' do
|
6
|
+
describe 'AR::Base' do
|
7
|
+
subject { ActiveRecord::Base }
|
8
|
+
it { should_not respond_to :max_paginates_per }
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { User.page(0).per(100) }
|
12
|
+
|
13
|
+
context 'by default' do
|
14
|
+
its(:limit_value) { should == 100 }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when explicitly set via max_paginates_per' do
|
18
|
+
before { User.max_paginates_per 10 }
|
19
|
+
its(:limit_value) { should == 10 }
|
20
|
+
after { User.max_paginates_per nil }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "max per_page value's independency per model" do
|
24
|
+
context "when User's max per_page was changed" do
|
25
|
+
before { User.max_paginates_per 10 }
|
26
|
+
subject { Book.page(0).per(100) }
|
27
|
+
its(:limit_value) { should == 100 }
|
28
|
+
after { User.max_paginates_per nil }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,242 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? ActiveRecord
|
4
|
+
|
5
|
+
shared_examples_for 'the first page' do
|
6
|
+
it { should have(25).users }
|
7
|
+
its('first.name') { should == 'user001' }
|
8
|
+
end
|
9
|
+
|
10
|
+
shared_examples_for 'blank page' do
|
11
|
+
it { should have(0).users }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe BootstrapPager::ActiveRecordExtension do
|
15
|
+
before do
|
16
|
+
1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
|
17
|
+
1.upto(100) {|i| GemDefinedModel.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
|
18
|
+
end
|
19
|
+
|
20
|
+
[User, Admin, GemDefinedModel].each do |model_class|
|
21
|
+
context "for #{model_class}" do
|
22
|
+
describe '#page' do
|
23
|
+
context 'page 1' do
|
24
|
+
subject { model_class.page 1 }
|
25
|
+
it_should_behave_like 'the first page'
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'page 2' do
|
29
|
+
subject { model_class.page 2 }
|
30
|
+
it { should have(25).users }
|
31
|
+
its('first.name') { should == 'user026' }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'page without an argument' do
|
35
|
+
subject { model_class.page }
|
36
|
+
it_should_behave_like 'the first page'
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'page < 1' do
|
40
|
+
subject { model_class.page 0 }
|
41
|
+
it_should_behave_like 'the first page'
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'page > max page' do
|
45
|
+
subject { model_class.page 5 }
|
46
|
+
it_should_behave_like 'blank page'
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'ensure #order_values is preserved' do
|
50
|
+
subject { model_class.order('id').page 1 }
|
51
|
+
its('order_values.uniq') { should == ['id'] }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#per' do
|
56
|
+
context 'page 1 per 5' do
|
57
|
+
subject { model_class.page(1).per(5) }
|
58
|
+
it { should have(5).users }
|
59
|
+
its('first.name') { should == 'user001' }
|
60
|
+
end
|
61
|
+
|
62
|
+
context "page 1 per nil (using default)" do
|
63
|
+
subject { model_class.page(1).per(nil) }
|
64
|
+
it { should have(model_class.default_per_page).users }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#padding' do
|
69
|
+
context 'page 1 per 5 padding 1' do
|
70
|
+
subject { model_class.page(1).per(5).padding(1) }
|
71
|
+
it { should have(5).users }
|
72
|
+
its('first.name') { should == 'user002' }
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'page 19 per 5 padding 5' do
|
76
|
+
subject { model_class.page(19).per(5).padding(5) }
|
77
|
+
its(:current_page) { should == 19 }
|
78
|
+
its(:total_pages) { should == 19 }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#total_pages' do
|
83
|
+
context 'per 25 (default)' do
|
84
|
+
subject { model_class.page }
|
85
|
+
its(:total_pages) { should == 4 }
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'per 7' do
|
89
|
+
subject { model_class.page(2).per(7) }
|
90
|
+
its(:total_pages) { should == 15 }
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'per 65536' do
|
94
|
+
subject { model_class.page(50).per(65536) }
|
95
|
+
its(:total_pages) { should == 1 }
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'per 0 (using default)' do
|
99
|
+
subject { model_class.page(50).per(0) }
|
100
|
+
its(:total_pages) { should == 4 }
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'per -1 (using default)' do
|
104
|
+
subject { model_class.page(5).per(-1) }
|
105
|
+
its(:total_pages) { should == 4 }
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'per "String value that can not be converted into Number" (using default)' do
|
109
|
+
subject { model_class.page(5).per('aho') }
|
110
|
+
its(:total_pages) { should == 4 }
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'with max_pages < total pages count from database' do
|
114
|
+
before { model_class.max_pages_per 3 }
|
115
|
+
subject { model_class.page }
|
116
|
+
its(:total_pages) { should == 3 }
|
117
|
+
after { model_class.max_pages_per nil }
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'with max_pages > total pages count from database' do
|
121
|
+
before { model_class.max_pages_per 11 }
|
122
|
+
subject { model_class.page }
|
123
|
+
its(:total_pages) { should == 4 }
|
124
|
+
after { model_class.max_pages_per nil }
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'with max_pages is nil' do
|
128
|
+
before { model_class.max_pages_per nil }
|
129
|
+
subject { model_class.page }
|
130
|
+
its(:total_pages) { should == 4 }
|
131
|
+
end
|
132
|
+
|
133
|
+
context "with per(nil) using default" do
|
134
|
+
subject { model_class.page.per(nil) }
|
135
|
+
its(:total_pages) { should == 4 }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe '#current_page' do
|
140
|
+
context 'page 1' do
|
141
|
+
subject { model_class.page }
|
142
|
+
its(:current_page) { should == 1 }
|
143
|
+
end
|
144
|
+
|
145
|
+
context 'page 2' do
|
146
|
+
subject { model_class.page(2).per 3 }
|
147
|
+
its(:current_page) { should == 2 }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe '#next_page' do
|
152
|
+
context 'page 1' do
|
153
|
+
subject { model_class.page }
|
154
|
+
its(:next_page) { should == 2 }
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'page 5' do
|
158
|
+
subject { model_class.page(5) }
|
159
|
+
its(:next_page) { should be_nil }
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe '#prev_page' do
|
164
|
+
context 'page 1' do
|
165
|
+
subject { model_class.page }
|
166
|
+
its(:prev_page) { should be_nil }
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'page 5' do
|
170
|
+
subject { model_class.page(5) }
|
171
|
+
its(:prev_page) { should == 4 }
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe '#first_page?' do
|
176
|
+
context 'on first page' do
|
177
|
+
subject { model_class.page(1).per(10) }
|
178
|
+
its(:first_page?) { should == true }
|
179
|
+
end
|
180
|
+
|
181
|
+
context 'not on first page' do
|
182
|
+
subject { model_class.page(5).per(10) }
|
183
|
+
its(:first_page?) { should == false }
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe '#last_page?' do
|
188
|
+
context 'on last page' do
|
189
|
+
subject { model_class.page(10).per(10) }
|
190
|
+
its(:last_page?) { should == true }
|
191
|
+
end
|
192
|
+
|
193
|
+
context 'not on last page' do
|
194
|
+
subject { model_class.page(1).per(10) }
|
195
|
+
its(:last_page?) { should == false }
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe '#out_of_range?' do
|
200
|
+
context 'on last page' do
|
201
|
+
subject { model_class.page(10).per(10) }
|
202
|
+
its(:out_of_range?) { should == false }
|
203
|
+
end
|
204
|
+
|
205
|
+
context 'within range' do
|
206
|
+
subject { model_class.page(1).per(10) }
|
207
|
+
its(:out_of_range?) { should == false }
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'out of range' do
|
211
|
+
subject { model_class.page(11).per(10) }
|
212
|
+
its(:out_of_range?) { should == true }
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe '#count' do
|
217
|
+
context 'page 1' do
|
218
|
+
subject { model_class.page }
|
219
|
+
its(:count) { should == 25 }
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'page 2' do
|
223
|
+
subject { model_class.page 2 }
|
224
|
+
its(:count) { should == 25 }
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context 'chained with .group' do
|
229
|
+
subject { model_class.group('age').page(2).per 5 }
|
230
|
+
# 0..10
|
231
|
+
its(:total_count) { should == 11 }
|
232
|
+
its(:total_pages) { should == 3 }
|
233
|
+
end
|
234
|
+
|
235
|
+
context 'activerecord descendants' do
|
236
|
+
subject { ActiveRecord::Base.descendants }
|
237
|
+
its(:length) { should_not == 0 }
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|