kaminari-rails4 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gemtest +0 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +16 -0
- data/CHANGELOG +351 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +300 -0
- data/Rakefile +42 -0
- data/app/helpers/kaminari_helper.rb +5 -0
- data/app/views/kaminari/_first_page.html.erb +11 -0
- data/app/views/kaminari/_first_page.html.haml +9 -0
- data/app/views/kaminari/_first_page.html.slim +10 -0
- data/app/views/kaminari/_gap.html.erb +8 -0
- data/app/views/kaminari/_gap.html.haml +8 -0
- data/app/views/kaminari/_gap.html.slim +9 -0
- data/app/views/kaminari/_last_page.html.erb +11 -0
- data/app/views/kaminari/_last_page.html.haml +9 -0
- data/app/views/kaminari/_last_page.html.slim +10 -0
- data/app/views/kaminari/_next_page.html.erb +11 -0
- data/app/views/kaminari/_next_page.html.haml +9 -0
- data/app/views/kaminari/_next_page.html.slim +10 -0
- data/app/views/kaminari/_page.html.erb +12 -0
- data/app/views/kaminari/_page.html.haml +10 -0
- data/app/views/kaminari/_page.html.slim +11 -0
- data/app/views/kaminari/_paginator.html.erb +23 -0
- data/app/views/kaminari/_paginator.html.haml +18 -0
- data/app/views/kaminari/_paginator.html.slim +19 -0
- data/app/views/kaminari/_prev_page.html.erb +11 -0
- data/app/views/kaminari/_prev_page.html.haml +9 -0
- data/app/views/kaminari/_prev_page.html.slim +10 -0
- data/config/locales/kaminari.yml +19 -0
- data/gemfiles/active_record_30.gemfile +7 -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/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.gemfile +13 -0
- data/kaminari.gemspec +36 -0
- data/lib/generators/kaminari/config_generator.rb +16 -0
- data/lib/generators/kaminari/templates/kaminari_config.rb +10 -0
- data/lib/generators/kaminari/views_generator.rb +118 -0
- data/lib/kaminari.rb +38 -0
- data/lib/kaminari/config.rb +51 -0
- data/lib/kaminari/engine.rb +4 -0
- data/lib/kaminari/grape.rb +4 -0
- data/lib/kaminari/helpers/action_view_extension.rb +151 -0
- data/lib/kaminari/helpers/paginator.rb +186 -0
- data/lib/kaminari/helpers/sinatra_helpers.rb +144 -0
- data/lib/kaminari/helpers/tags.rb +95 -0
- data/lib/kaminari/hooks.rb +33 -0
- data/lib/kaminari/models/active_record_extension.rb +22 -0
- data/lib/kaminari/models/active_record_model_extension.rb +20 -0
- data/lib/kaminari/models/active_record_relation_methods.rb +29 -0
- data/lib/kaminari/models/array_extension.rb +58 -0
- data/lib/kaminari/models/configuration_methods.rb +48 -0
- data/lib/kaminari/models/data_mapper_collection_methods.rb +15 -0
- data/lib/kaminari/models/data_mapper_extension.rb +48 -0
- data/lib/kaminari/models/mongo_mapper_extension.rb +18 -0
- data/lib/kaminari/models/mongoid_criteria_methods.rb +23 -0
- data/lib/kaminari/models/mongoid_extension.rb +33 -0
- data/lib/kaminari/models/page_scope_methods.rb +70 -0
- data/lib/kaminari/models/plucky_criteria_methods.rb +18 -0
- data/lib/kaminari/railtie.rb +7 -0
- data/lib/kaminari/sinatra.rb +5 -0
- data/lib/kaminari/version.rb +3 -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 +1 -0
- data/spec/fake_app/data_mapper/models.rb +27 -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 +56 -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 +262 -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 +179 -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 +33 -0
- data/spec/support/database_cleaner.rb +16 -0
- data/spec/support/matchers.rb +52 -0
- metadata +300 -0
@@ -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 Kaminari::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
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Kaminari::PaginatableArray do
|
4
|
+
it { should have(0).items }
|
5
|
+
|
6
|
+
context 'specifying limit and offset when initializing' do
|
7
|
+
subject { Kaminari::PaginatableArray.new((1..100).to_a, :limit => 10, :offset => 20) }
|
8
|
+
its(:current_page) { should == 3 }
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:array) { Kaminari::PaginatableArray.new((1..100).to_a) }
|
12
|
+
describe '#page' do
|
13
|
+
shared_examples_for 'the first page of array' do
|
14
|
+
it { should have(25).users }
|
15
|
+
its(:current_page) { should == 1 }
|
16
|
+
its(:first) { should == 1 }
|
17
|
+
end
|
18
|
+
|
19
|
+
shared_examples_for 'blank array page' do
|
20
|
+
it { should have(0).items }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'page 1' do
|
24
|
+
subject { array.page 1 }
|
25
|
+
it_should_behave_like 'the first page of array'
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'page 2' do
|
29
|
+
subject { array.page 2 }
|
30
|
+
it { should have(25).users }
|
31
|
+
its(:current_page) { should == 2 }
|
32
|
+
its(:first) { should == 26 }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'page without an argument' do
|
36
|
+
subject { array.page }
|
37
|
+
it_should_behave_like 'the first page of array'
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'page < 1' do
|
41
|
+
subject { array.page 0 }
|
42
|
+
it_should_behave_like 'the first page of array'
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'page > max page' do
|
46
|
+
subject { array.page 5 }
|
47
|
+
it_should_behave_like 'blank array page'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#per' do
|
52
|
+
context 'page 1 per 5' do
|
53
|
+
subject { array.page(1).per(5) }
|
54
|
+
it { should have(5).users }
|
55
|
+
its(:first) { should == 1 }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#total_pages' do
|
60
|
+
context 'per 25 (default)' do
|
61
|
+
subject { array.page }
|
62
|
+
its(:total_pages) { should == 4 }
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'per 7' do
|
66
|
+
subject { array.page(2).per(7) }
|
67
|
+
its(:total_pages) { should == 15 }
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'per 65536' do
|
71
|
+
subject { array.page(50).per(65536) }
|
72
|
+
its(:total_pages) { should == 1 }
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'per 0 (using default)' do
|
76
|
+
subject { array.page(50).per(0) }
|
77
|
+
its(:total_pages) { should == 4 }
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'per -1 (using default)' do
|
81
|
+
subject { array.page(5).per(-1) }
|
82
|
+
its(:total_pages) { should == 4 }
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'per "String value that can not be converted into Number" (using default)' do
|
86
|
+
subject { array.page(5).per('aho') }
|
87
|
+
its(:total_pages) { should == 4 }
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'per 25, padding 25' do
|
91
|
+
subject { array.page(1).padding(25) }
|
92
|
+
its(:total_pages) { should == 3 }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#current_page' do
|
97
|
+
context 'page 1' do
|
98
|
+
subject { array.page }
|
99
|
+
its(:current_page) { should == 1 }
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'page 2' do
|
103
|
+
subject { array.page(2).per 3 }
|
104
|
+
its(:current_page) { should == 2 }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#next_page' do
|
109
|
+
context 'page 1' do
|
110
|
+
subject { array.page }
|
111
|
+
its(:next_page) { should == 2 }
|
112
|
+
end
|
113
|
+
|
114
|
+
context 'page 5' do
|
115
|
+
subject { array.page 5 }
|
116
|
+
its(:next_page) { should be_nil }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '#prev_page' do
|
121
|
+
context 'page 1' do
|
122
|
+
subject { array.page }
|
123
|
+
its(:prev_page) { should be_nil }
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'page 5' do
|
127
|
+
subject { array.page 5 }
|
128
|
+
its(:prev_page) { should == 4 }
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe '#count' do
|
133
|
+
context 'page 1' do
|
134
|
+
subject { array.page }
|
135
|
+
its(:count) { should == 25 }
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'page 2' do
|
139
|
+
subject { array.page 2 }
|
140
|
+
its(:count) { should == 25 }
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'when setting total count explicitly' do
|
145
|
+
subject { Kaminari::PaginatableArray.new((1..10).to_a, :total_count => 9999).page(5).per(10) }
|
146
|
+
it { should have(10).items }
|
147
|
+
its(:first) { should == 1 }
|
148
|
+
its(:total_count) { should == 9999 }
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined? DataMapper
|
4
|
+
describe Kaminari::DataMapperExtension do
|
5
|
+
before do
|
6
|
+
100.times do |i|
|
7
|
+
User.create(:name => "User#{i}", :age => i)
|
8
|
+
end
|
9
|
+
|
10
|
+
worker0 = User[0]
|
11
|
+
50.times do |i|
|
12
|
+
worker0.projects << Project.create(:name => "Project#{i}")
|
13
|
+
end
|
14
|
+
worker0.projects.save
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'Collection' do
|
18
|
+
subject{ User.all }
|
19
|
+
it { should respond_to(:page) }
|
20
|
+
it { should_not respond_to(:per) }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'Model' do
|
24
|
+
subject{ User }
|
25
|
+
it { should respond_to(:page) }
|
26
|
+
it { should respond_to(:default_per_page) }
|
27
|
+
it { should_not respond_to(:per) }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#page' do
|
31
|
+
context 'page 0' do
|
32
|
+
subject { User.all(:age.gte => 60).page 0 }
|
33
|
+
it { should be_a DataMapper::Collection }
|
34
|
+
its(:current_page) { should == 1 }
|
35
|
+
its(:prev_page) { should be_nil }
|
36
|
+
its(:next_page) { should == 2 }
|
37
|
+
its('query.limit') { should == 25 }
|
38
|
+
its('query.offset') { should == 0 }
|
39
|
+
its(:total_count) { should == User.count(:age.gte => 60) }
|
40
|
+
its(:total_pages) { should == 2 }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'page 1' do
|
44
|
+
subject { User.all(:age.gte => 0).page 1 }
|
45
|
+
it { should be_a DataMapper::Collection }
|
46
|
+
its(:current_page) { should == 1 }
|
47
|
+
its(:prev_page) { should be_nil }
|
48
|
+
its(:next_page) { should == 2 }
|
49
|
+
its('query.limit') { should == 25 }
|
50
|
+
its('query.offset') { should == 0 }
|
51
|
+
its(:total_count) { should == 100 }
|
52
|
+
its(:total_pages) { should == 4 }
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'page 2' do
|
56
|
+
subject { User.page 2 }
|
57
|
+
it { should be_a DataMapper::Collection }
|
58
|
+
its(:current_page) { should == 2 }
|
59
|
+
its(:prev_page) { should == 1 }
|
60
|
+
its(:next_page) { should == 3 }
|
61
|
+
its(:limit_value) { should == 25 }
|
62
|
+
its('query.limit') { should == 25 }
|
63
|
+
its('query.offset') { should == 25 }
|
64
|
+
its(:total_count) { should == 100 }
|
65
|
+
its(:total_pages) { should == 4 }
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'page "foobar"' do
|
69
|
+
subject { User.page 'foobar' }
|
70
|
+
it { should be_a DataMapper::Collection }
|
71
|
+
its(:current_page) { should == 1 }
|
72
|
+
its(:prev_page) { should be_nil }
|
73
|
+
its(:next_page) { should == 2 }
|
74
|
+
its('query.limit') { should == 25 }
|
75
|
+
its('query.offset') { should == 0 }
|
76
|
+
its(:total_count) { should == 100 }
|
77
|
+
its(:total_pages) { should == 4 }
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'with criteria before' do
|
81
|
+
subject { User.all(:age.gt => 60).page 2 }
|
82
|
+
it { should be_a DataMapper::Collection }
|
83
|
+
its(:current_page) { should == 2 }
|
84
|
+
its(:prev_page) { should == 1 }
|
85
|
+
its(:next_page) { should be_nil }
|
86
|
+
its('query.limit') { should == 25 }
|
87
|
+
its('query.offset') { should == 25 }
|
88
|
+
its(:total_count) { should == User.count(:age.gt => 60) }
|
89
|
+
its(:total_pages) { should == 2 }
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'with criteria after' do
|
93
|
+
subject { User.page(2).all(:age.gt => 60) }
|
94
|
+
it { should be_a DataMapper::Collection }
|
95
|
+
its(:current_page) { should == 2 }
|
96
|
+
its(:prev_page) { should == 1 }
|
97
|
+
its(:next_page) { should be_nil }
|
98
|
+
its('query.limit') { should == 25 }
|
99
|
+
its('query.offset') { should == 25 }
|
100
|
+
its(:total_count) { should == User.count(:age.gt => 60) }
|
101
|
+
its(:total_pages) { should == 2 }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe '#per' do
|
106
|
+
context 'on simple query' do
|
107
|
+
subject { User.page(2).per(20) }
|
108
|
+
it { should be_a DataMapper::Collection }
|
109
|
+
its(:current_page) { should == 2 }
|
110
|
+
its(:prev_page) { should == 1 }
|
111
|
+
its(:next_page) { should == 3 }
|
112
|
+
its('query.limit') { should == 20 }
|
113
|
+
its(:limit_value) { should == 20 }
|
114
|
+
its('query.offset') { should == 20 }
|
115
|
+
its(:total_count) { should == 100 }
|
116
|
+
its(:total_pages) { should == 5 }
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'on query with condition' do
|
120
|
+
subject { User.page(5).all(:age.lte => 80).per(13) }
|
121
|
+
its(:current_page) { should == 5 }
|
122
|
+
its(:prev_page) { should == 4 }
|
123
|
+
its(:next_page) { should == 6 }
|
124
|
+
its('query.limit') { should == 13 }
|
125
|
+
its('query.offset') { should == 52 }
|
126
|
+
its(:total_count) { should == 81 }
|
127
|
+
its(:total_pages) { should == 7 }
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'on query with order' do
|
131
|
+
subject { User.page(5).all(:age.lte => 80, :order => [:age.asc]).per(13) }
|
132
|
+
it('includes user with age 52') { should include(User.first(:age => 52)) }
|
133
|
+
it('does not include user with age 51') { should_not include(User.first(:age => 51)) }
|
134
|
+
it('includes user with age 52') { should include(User.first(:age => 64)) }
|
135
|
+
it('does not include user with age 51') { should_not include(User.first(:age => 65)) }
|
136
|
+
its(:current_page) { should == 5 }
|
137
|
+
its(:prev_page) { should == 4 }
|
138
|
+
its(:next_page) { should == 6 }
|
139
|
+
its('query.limit') { should == 13 }
|
140
|
+
its('query.offset') { should == 52 }
|
141
|
+
its(:total_count) { should == 81 }
|
142
|
+
its(:total_pages) { should == 7 }
|
143
|
+
end
|
144
|
+
|
145
|
+
context 'on chained queries' do
|
146
|
+
subject { User.all(:age.gte => 50).page(3).all(:age.lte => 80).per(13) }
|
147
|
+
its(:current_page) { should == 3 }
|
148
|
+
its(:prev_page) { should == 2 }
|
149
|
+
its(:next_page) { should be_nil }
|
150
|
+
its('query.limit') { should == 13 }
|
151
|
+
its('query.offset') { should == 26 }
|
152
|
+
its(:total_count) { should == 31 }
|
153
|
+
its(:total_pages) { should == 3 }
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'on query on association' do
|
157
|
+
subject { User[0].projects.page(3).all(:name.like => 'Project%').per(5) }
|
158
|
+
its(:current_page) { should == 3 }
|
159
|
+
its(:prev_page) { should == 2 }
|
160
|
+
its(:next_page) { should == 4 }
|
161
|
+
its('query.limit') { should == 5 }
|
162
|
+
its('query.offset') { should == 10 }
|
163
|
+
its(:total_count) { should == 50 }
|
164
|
+
its(:total_pages) { should == 10 }
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'on query with association conditions' do
|
168
|
+
subject { User.page(3).all(:projects => Project.all).per(5) }
|
169
|
+
its(:current_page) { should == 3 }
|
170
|
+
its(:prev_page) { should == 2 }
|
171
|
+
its(:next_page) { should == 4 }
|
172
|
+
its('query.limit') { should == 5 }
|
173
|
+
its('query.offset') { should == 10 }
|
174
|
+
its(:total_count) { should == 50 }
|
175
|
+
its(:total_pages) { should == 10 }
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|