2011-02-05 08:55:38 -05:00
|
|
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
|
|
|
|
2011-02-17 18:09:53 -05:00
|
|
|
describe Kaminari::ActiveRecordExtension do
|
2011-02-05 08:55:38 -05:00
|
|
|
before :all do
|
2011-02-22 10:20:44 -05:00
|
|
|
1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#page' do
|
|
|
|
shared_examples_for 'the first page' do
|
2011-02-06 06:11:45 -05:00
|
|
|
it { should have(25).users }
|
|
|
|
its('first.name') { should == 'user001' }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'blank page' do
|
|
|
|
it { should have(0).users }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 1' do
|
|
|
|
subject { User.page 1 }
|
|
|
|
it_should_behave_like 'the first page'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 2' do
|
|
|
|
subject { User.page 2 }
|
2011-02-06 06:11:45 -05:00
|
|
|
it { should have(25).users }
|
|
|
|
its('first.name') { should == 'user026' }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'page without an argument' do
|
|
|
|
subject { User.page }
|
|
|
|
it_should_behave_like 'the first page'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page < 1' do
|
|
|
|
subject { User.page 0 }
|
|
|
|
it_should_behave_like 'the first page'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page > max page' do
|
2011-02-06 06:11:45 -05:00
|
|
|
subject { User.page 5 }
|
2011-02-05 08:55:38 -05:00
|
|
|
it_should_behave_like 'blank page'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#per' do
|
|
|
|
context 'page 1 per 5' do
|
|
|
|
subject { User.page(1).per(5) }
|
|
|
|
it { should have(5).users }
|
2011-02-06 06:11:45 -05:00
|
|
|
its('first.name') { should == 'user001' }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#num_pages' do
|
2011-02-06 06:11:45 -05:00
|
|
|
context 'per 25 (default)' do
|
|
|
|
subject { User.page }
|
|
|
|
its(:num_pages) { should == 4 }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'per 7' do
|
2011-02-06 06:11:45 -05:00
|
|
|
subject { User.page(2).per(7) }
|
|
|
|
its(:num_pages) { should == 15 }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'per 65536' do
|
2011-02-06 06:11:45 -05:00
|
|
|
subject { User.page(50).per(65536) }
|
|
|
|
its(:num_pages) { should == 1 }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
2011-02-15 04:41:49 -05:00
|
|
|
|
|
|
|
context 'per 0 (using default)' do
|
|
|
|
subject { User.page(50).per(0) }
|
|
|
|
its(:num_pages) { should == 4 }
|
|
|
|
end
|
2011-02-16 11:37:52 -05:00
|
|
|
|
|
|
|
context 'per -1 (using default)' do
|
|
|
|
subject { User.page(5).per(-1) }
|
|
|
|
its(:num_pages) { should == 4 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'per "String value that can not be converted into Number" (using default)' do
|
|
|
|
subject { User.page(5).per('aho') }
|
|
|
|
its(:num_pages) { should == 4 }
|
|
|
|
end
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_page' do
|
|
|
|
context 'page 1' do
|
2011-02-06 06:11:45 -05:00
|
|
|
subject { User.page }
|
|
|
|
its(:current_page) { should == 1 }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 2' do
|
2011-02-06 06:11:45 -05:00
|
|
|
subject { User.page(2).per 3 }
|
|
|
|
its(:current_page) { should == 2 }
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|
|
|
|
end
|
2011-02-22 10:20:44 -05:00
|
|
|
|
2011-04-19 02:00:00 -04:00
|
|
|
describe '#count' do
|
|
|
|
context 'page 1' do
|
|
|
|
subject { User.page }
|
|
|
|
its(:count) { should == 25 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 2' do
|
|
|
|
subject { User.page 2 }
|
|
|
|
its(:count) { should == 25 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-22 10:20:44 -05:00
|
|
|
context 'chained with .group' do
|
|
|
|
subject { User.group('age').page(2).per 5 }
|
|
|
|
# 0..10
|
|
|
|
its(:total_count) { should == 11 }
|
|
|
|
its(:num_pages) { should == 3 }
|
|
|
|
end
|
2011-02-24 03:19:51 -05:00
|
|
|
|
|
|
|
context 'activerecord descendants' do
|
|
|
|
subject { ActiveRecord::Base.descendants }
|
|
|
|
its(:length) { should_not == 0 }
|
|
|
|
end
|
2011-02-05 08:55:38 -05:00
|
|
|
end
|