2011-02-18 12:33:56 +09:00
|
|
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
|
|
|
require 'mongoid'
|
2011-02-21 02:53:59 +09:00
|
|
|
require File.expand_path('../../lib/kaminari/models/mongoid_extension', File.dirname(__FILE__))
|
2011-02-18 12:33:56 +09:00
|
|
|
|
|
|
|
describe Kaminari::MongoidExtension do
|
|
|
|
before :all do
|
|
|
|
class Developer
|
|
|
|
include ::Mongoid::Document
|
2011-02-24 22:04:06 +09:00
|
|
|
field :salary, :type => Integer
|
2011-02-18 12:33:56 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
before do
|
|
|
|
stub(subject).count { 300 } # in order to avoid DB access...
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#page' do
|
|
|
|
context 'page 1' do
|
|
|
|
subject { Developer.page 1 }
|
|
|
|
it { should be_a Mongoid::Criteria }
|
|
|
|
its(:current_page) { should == 1 }
|
|
|
|
its(:limit_value) { should == 25 }
|
|
|
|
its(:num_pages) { should == 12 }
|
|
|
|
it { should skip(0) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 2' do
|
|
|
|
subject { Developer.page 2 }
|
|
|
|
it { should be_a Mongoid::Criteria }
|
|
|
|
its(:current_page) { should == 2 }
|
|
|
|
its(:limit_value) { should == 25 }
|
|
|
|
its(:num_pages) { should == 12 }
|
|
|
|
it { should skip 25 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page "foobar"' do
|
|
|
|
subject { Developer.page 'foobar' }
|
|
|
|
it { should be_a Mongoid::Criteria }
|
|
|
|
its(:current_page) { should == 1 }
|
|
|
|
its(:limit_value) { should == 25 }
|
|
|
|
its(:num_pages) { should == 12 }
|
|
|
|
it { should skip 0 }
|
|
|
|
end
|
2011-02-24 02:20:42 +07:00
|
|
|
|
|
|
|
context 'with criteria before' do
|
2011-02-24 22:04:06 +09:00
|
|
|
subject { Developer.where(:salary => 1).page 2 }
|
|
|
|
its(:selector) { should == {:salary => 1} }
|
2011-02-24 02:20:42 +07:00
|
|
|
its(:current_page) { should == 2 }
|
|
|
|
its(:limit_value) { should == 25 }
|
|
|
|
its(:num_pages) { should == 12 }
|
|
|
|
it { should skip 25 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with criteria after' do
|
2011-02-24 22:04:06 +09:00
|
|
|
subject { Developer.page(2).where(:salary => 1) }
|
|
|
|
its(:selector) { should == {:salary => 1} }
|
2011-02-24 02:20:42 +07:00
|
|
|
its(:current_page) { should == 2 }
|
|
|
|
its(:limit_value) { should == 25 }
|
|
|
|
its(:num_pages) { should == 12 }
|
|
|
|
it { should skip 25 }
|
|
|
|
end
|
|
|
|
|
2011-02-18 12:33:56 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#per' do
|
|
|
|
subject { Developer.page(2).per(10) }
|
|
|
|
it { should be_a Mongoid::Criteria }
|
|
|
|
its(:current_page) { should == 2 }
|
|
|
|
its(:limit_value) { should == 10 }
|
|
|
|
its(:num_pages) { should == 30 }
|
|
|
|
it { should skip 10 }
|
|
|
|
end
|
|
|
|
end
|