2011-12-10 11:15:18 -05:00
|
|
|
require 'spec_helper'
|
2011-04-26 08:56:36 -04:00
|
|
|
|
2014-03-12 23:46:19 -04:00
|
|
|
describe 'Kaminari::ActionViewExtension', :if => defined?(Rails) do
|
2011-04-26 08:56:36 -04:00
|
|
|
describe '#paginate' do
|
|
|
|
before do
|
2011-07-23 05:24:49 -04:00
|
|
|
50.times {|i| User.create! :name => "user#{i}"}
|
2011-05-05 10:28:41 -04:00
|
|
|
@users = User.page(1)
|
2011-04-26 08:56:36 -04:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-05-05 10:28:41 -04:00
|
|
|
subject { helper.paginate @users, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should be_a String }
|
2011-04-26 08:56:36 -04:00
|
|
|
|
2011-05-05 10:28:41 -04:00
|
|
|
context 'escaping the pagination for javascript' do
|
|
|
|
it 'should escape for javascript' do
|
2012-05-22 23:01:54 -04:00
|
|
|
lambda { helper.escape_javascript(helper.paginate @users, :params => {:controller => 'users', :action => 'index'}) }.should_not raise_error
|
2011-04-26 08:56:36 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-08 01:29:50 -04:00
|
|
|
|
2014-06-10 12:01:55 -04:00
|
|
|
context 'accepts :theme option' do
|
|
|
|
before { helper.controller.append_view_path "spec/fake_app/views" }
|
|
|
|
subject { helper.paginate @users, :theme => "bootstrap", :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should match(/bootstrap-paginator/) }
|
|
|
|
it { should match(/bootstrap-page-link/) }
|
|
|
|
end
|
|
|
|
|
2014-05-09 00:23:30 -04:00
|
|
|
context 'accepts :view_prefix option' do
|
|
|
|
before { helper.controller.append_view_path "spec/fake_app/views" }
|
2014-05-13 22:50:56 -04:00
|
|
|
subject { helper.paginate @users, :views_prefix => "alternative/", :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should eq(" <b>1</b>\n") }
|
2014-05-08 01:29:50 -04:00
|
|
|
end
|
2014-09-24 10:16:27 -04:00
|
|
|
|
2014-11-05 23:58:20 -05:00
|
|
|
context "num_pages: 3" do
|
|
|
|
subject { helper.paginate @users, :num_pages => 3, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should match(/<a href="\/users\?page=3">Last/) }
|
|
|
|
end
|
2014-09-24 10:16:27 -04:00
|
|
|
end
|
|
|
|
|
2011-12-29 14:13:20 -05:00
|
|
|
describe '#link_to_previous_page' do
|
|
|
|
before do
|
2014-03-18 19:57:26 -04:00
|
|
|
60.times {|i| User.create! :name => "user#{i}"}
|
2011-12-29 14:13:20 -05:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-12-29 14:13:20 -05:00
|
|
|
context 'having previous pages' do
|
|
|
|
before do
|
2014-03-18 19:57:26 -04:00
|
|
|
@users = User.page(3)
|
2011-12-29 14:13:20 -05:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-12-29 14:13:20 -05:00
|
|
|
context 'the default behaviour' do
|
|
|
|
subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
|
2014-03-18 19:57:26 -04:00
|
|
|
it { should match(/page=2/) }
|
2014-03-27 21:23:48 -04:00
|
|
|
it { should match(/rel="prev"/) }
|
2011-12-29 14:13:20 -05:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-12-29 14:13:20 -05:00
|
|
|
context 'overriding rel=' do
|
|
|
|
subject { helper.link_to_previous_page @users, 'Previous', :rel => 'external', :params => {:controller => 'users', :action => 'index'} }
|
2012-06-20 00:21:50 -04:00
|
|
|
it { should match(/rel="external"/) }
|
2011-12-29 14:13:20 -05:00
|
|
|
end
|
2014-03-12 23:46:19 -04:00
|
|
|
|
|
|
|
context 'with params' do
|
|
|
|
before do
|
|
|
|
helper.params[:status] = "active"
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should match(/status=active/) }
|
|
|
|
end
|
2011-12-29 14:13:20 -05:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-12-29 14:13:20 -05:00
|
|
|
context 'the first page' do
|
|
|
|
before do
|
|
|
|
@users = User.page(1)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-12-29 14:13:20 -05:00
|
|
|
subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should_not be }
|
|
|
|
end
|
2014-09-13 23:41:30 -04:00
|
|
|
|
|
|
|
context 'out of range' do
|
|
|
|
before { @users = User.page(5) }
|
|
|
|
|
|
|
|
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should_not be }
|
|
|
|
end
|
2011-12-29 14:13:20 -05:00
|
|
|
end
|
|
|
|
|
2011-05-05 10:41:17 -04:00
|
|
|
describe '#link_to_next_page' do
|
2011-07-23 05:24:49 -04:00
|
|
|
before do
|
|
|
|
50.times {|i| User.create! :name => "user#{i}"}
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-05-05 10:41:17 -04:00
|
|
|
context 'having more page' do
|
|
|
|
before do
|
|
|
|
@users = User.page(1)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-11-04 15:09:02 -04:00
|
|
|
context 'the default behaviour' do
|
|
|
|
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
|
2014-03-18 19:57:26 -04:00
|
|
|
it { should match(/page=2/) }
|
2012-02-14 05:26:04 -05:00
|
|
|
it { should match(/rel="next"/) }
|
2011-11-04 15:09:02 -04:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-11-04 15:09:02 -04:00
|
|
|
context 'overriding rel=' do
|
|
|
|
subject { helper.link_to_next_page @users, 'More', :rel => 'external', :params => {:controller => 'users', :action => 'index'} }
|
2012-02-14 05:26:04 -05:00
|
|
|
it { should match(/rel="external"/) }
|
2011-11-04 15:09:02 -04:00
|
|
|
end
|
2014-03-12 23:46:19 -04:00
|
|
|
|
|
|
|
context 'with params' do
|
|
|
|
before do
|
|
|
|
helper.params[:status] = "active"
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:43:09 -04:00
|
|
|
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
|
2014-03-12 23:46:19 -04:00
|
|
|
it { should match(/status=active/) }
|
|
|
|
end
|
2011-05-05 10:41:17 -04:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-05-05 10:41:17 -04:00
|
|
|
context 'the last page' do
|
|
|
|
before do
|
|
|
|
@users = User.page(2)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2011-05-05 10:41:17 -04:00
|
|
|
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should_not be }
|
|
|
|
end
|
2014-09-13 23:41:30 -04:00
|
|
|
|
|
|
|
context 'out of range' do
|
|
|
|
before { @users = User.page(5) }
|
|
|
|
|
|
|
|
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should_not be }
|
|
|
|
end
|
2011-05-05 10:41:17 -04:00
|
|
|
end
|
2011-07-23 05:24:49 -04:00
|
|
|
|
|
|
|
describe '#page_entries_info' do
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'on a model without namespace' do
|
2011-07-23 05:24:49 -04:00
|
|
|
before do
|
|
|
|
@users = User.page(1).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'having no entries' do
|
|
|
|
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
|
2013-01-30 01:38:38 -05:00
|
|
|
it { should == 'No users found' }
|
2012-07-29 13:53:50 -04:00
|
|
|
|
|
|
|
context 'setting the entry name option to "member"' do
|
|
|
|
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'No members found' }
|
|
|
|
end
|
2012-02-19 10:57:59 -05:00
|
|
|
end
|
2011-07-23 05:24:49 -04:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'having 1 entry' do
|
|
|
|
before do
|
2012-05-17 18:00:31 -04:00
|
|
|
User.create! :name => 'user1'
|
2012-02-19 10:57:59 -05:00
|
|
|
@users = User.page(1).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>1</b> user' }
|
|
|
|
|
|
|
|
context 'setting the entry name option to "member"' do
|
|
|
|
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>1</b> member' }
|
|
|
|
end
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'having more than 1 but less than a page of entries' do
|
|
|
|
before do
|
2012-05-17 18:00:31 -04:00
|
|
|
10.times {|i| User.create! :name => "user#{i}"}
|
2012-02-19 10:57:59 -05:00
|
|
|
@users = User.page(1).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>all 10</b> users' }
|
|
|
|
|
|
|
|
context 'setting the entry name option to "member"' do
|
|
|
|
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>all 10</b> members' }
|
|
|
|
end
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'having more than one page of entries' do
|
|
|
|
before do
|
2012-05-17 18:00:31 -04:00
|
|
|
50.times {|i| User.create! :name => "user#{i}"}
|
2012-02-19 10:57:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'the first page' do
|
|
|
|
before do
|
|
|
|
@users = User.page(1).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying users <b>1 - 25</b> of <b>50</b> in total' }
|
|
|
|
|
|
|
|
context 'setting the entry name option to "member"' do
|
|
|
|
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying members <b>1 - 25</b> of <b>50</b> in total' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'the next page' do
|
|
|
|
before do
|
|
|
|
@users = User.page(2).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying users <b>26 - 50</b> of <b>50</b> in total' }
|
|
|
|
|
|
|
|
context 'setting the entry name option to "member"' do
|
|
|
|
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying members <b>26 - 50</b> of <b>50</b> in total' }
|
|
|
|
end
|
|
|
|
end
|
2016-05-08 18:23:36 -04:00
|
|
|
|
|
|
|
describe 'the last page' do
|
|
|
|
before do
|
|
|
|
User.max_pages_per 4
|
|
|
|
@users = User.page(4).per(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
after { User.max_pages_per nil }
|
|
|
|
|
|
|
|
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
it { should == 'Displaying users <b>31 - 40</b> of <b>50</b> in total' }
|
|
|
|
end
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
|
|
|
end
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'on a model with namespace' do
|
2011-07-23 05:24:49 -04:00
|
|
|
before do
|
2012-02-19 10:57:59 -05:00
|
|
|
@addresses = User::Address.page(1).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'having no entries' do
|
|
|
|
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
|
2013-01-30 01:38:38 -05:00
|
|
|
it { should == 'No addresses found' }
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'having 1 entry' do
|
2011-07-23 05:24:49 -04:00
|
|
|
before do
|
2012-02-19 10:57:59 -05:00
|
|
|
User::Address.create!
|
|
|
|
@addresses = User::Address.page(1).per(25)
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>1</b> address' }
|
2011-07-23 05:24:49 -04:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'setting the entry name option to "place"' do
|
|
|
|
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>1</b> place' }
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'having more than 1 but less than a page of entries' do
|
2011-07-23 05:24:49 -04:00
|
|
|
before do
|
2012-02-19 10:57:59 -05:00
|
|
|
10.times {|i| User::Address.create!}
|
|
|
|
@addresses = User::Address.page(1).per(25)
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>all 10</b> addresses' }
|
2011-07-23 05:24:49 -04:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
context 'setting the entry name option to "place"' do
|
|
|
|
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying <b>all 10</b> places' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'having more than one page of entries' do
|
|
|
|
before do
|
|
|
|
50.times {|i| User::Address.create!}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'the first page' do
|
|
|
|
before do
|
|
|
|
@addresses = User::Address.page(1).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying addresses <b>1 - 25</b> of <b>50</b> in total' }
|
|
|
|
|
|
|
|
context 'setting the entry name option to "place"' do
|
|
|
|
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying places <b>1 - 25</b> of <b>50</b> in total' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'the next page' do
|
|
|
|
before do
|
|
|
|
@addresses = User::Address.page(2).per(25)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-19 10:57:59 -05:00
|
|
|
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying addresses <b>26 - 50</b> of <b>50</b> in total' }
|
|
|
|
|
|
|
|
context 'setting the entry name option to "place"' do
|
|
|
|
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
|
|
|
|
it { should == 'Displaying places <b>26 - 50</b> of <b>50</b> in total' }
|
|
|
|
end
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-02 12:00:31 -04:00
|
|
|
|
|
|
|
context 'on a PaginatableArray' do
|
|
|
|
before do
|
|
|
|
@numbers = Kaminari.paginate_array(%w{one two three}).page(1)
|
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-09-02 12:00:31 -04:00
|
|
|
subject { helper.page_entries_info @numbers }
|
|
|
|
it { should == 'Displaying <b>all 3</b> entries' }
|
|
|
|
end
|
2011-07-23 05:24:49 -04:00
|
|
|
end
|
2012-02-01 20:49:10 -05:00
|
|
|
|
|
|
|
describe '#rel_next_prev_link_tags' do
|
|
|
|
before do
|
2014-03-27 21:28:13 -04:00
|
|
|
31.times {|i| User.create! :name => "user#{i}"}
|
2012-02-01 20:49:10 -05:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2014-03-27 21:28:13 -04:00
|
|
|
subject { helper.rel_next_prev_link_tags users, :params => {:controller => 'users', :action => 'index'} }
|
|
|
|
|
2012-02-01 20:49:10 -05:00
|
|
|
context 'the first page' do
|
2014-03-27 21:28:13 -04:00
|
|
|
let(:users) { User.page(1).per(10) }
|
2012-02-01 20:49:10 -05:00
|
|
|
|
2012-06-20 00:21:50 -04:00
|
|
|
it { should_not match(/rel="prev"/) }
|
2014-01-30 23:09:10 -05:00
|
|
|
it { should match(/rel="next"/) }
|
2014-01-30 23:47:21 -05:00
|
|
|
it { should match(/\?page=2/) }
|
2012-02-01 20:49:10 -05:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2014-03-27 21:28:13 -04:00
|
|
|
context 'the second page' do
|
|
|
|
let(:users) { User.page(2).per(10) }
|
2012-02-01 20:49:10 -05:00
|
|
|
|
2012-06-20 00:21:50 -04:00
|
|
|
it { should match(/rel="prev"/) }
|
2014-03-27 21:28:13 -04:00
|
|
|
it { should_not match(/\?page=1/) }
|
2014-01-30 23:09:10 -05:00
|
|
|
it { should match(/rel="next"/) }
|
2014-03-27 21:28:13 -04:00
|
|
|
it { should match(/\?page=3/) }
|
2012-02-01 20:49:10 -05:00
|
|
|
end
|
2013-12-01 22:24:58 -05:00
|
|
|
|
2012-02-01 20:49:10 -05:00
|
|
|
context 'the last page' do
|
2014-03-27 21:28:13 -04:00
|
|
|
let(:users) { User.page(4).per(10) }
|
2012-02-01 20:49:10 -05:00
|
|
|
|
2012-06-20 00:21:50 -04:00
|
|
|
it { should match(/rel="prev"/) }
|
2014-01-31 22:30:25 -05:00
|
|
|
it { should match(/\?page=3"/) }
|
2014-01-30 23:09:10 -05:00
|
|
|
it { should_not match(/rel="next"/) }
|
2012-02-01 20:49:10 -05:00
|
|
|
end
|
|
|
|
end
|
2011-04-26 08:56:36 -04:00
|
|
|
end
|