Regression for paginatable array

Sometimes, we want to make an array just paginatable without changing
its elements. Make it so if total count is explicitly given and it is
greater than the length of the given array. #516
This commit is contained in:
Yuki Nishijima 2014-06-24 18:19:21 -07:00
parent 13d2736730
commit 21ad994d42
2 changed files with 15 additions and 8 deletions

View File

@ -17,11 +17,15 @@ module Kaminari
extend Kaminari::PageScopeMethods
end
if @_total_count
original_array = original_array.first(@_total_count)
if @_total_count.present? && @_total_count <= original_array.count
original_array = original_array.first(@_total_count)[@_offset_value, @_limit_value]
end
super(original_array[@_offset_value, @_limit_value] || [])
if @_total_count.nil?
original_array = original_array[@_offset_value, @_limit_value]
end
super(original_array || [])
end
def entry_name

View File

@ -142,27 +142,30 @@ describe Kaminari::PaginatableArray do
end
context 'when setting total count explicitly' do
context "total_count > size of the given array" do
context "array 1..10, page 5, per 10, total_count 9999" do
subject { Kaminari::PaginatableArray.new((1..10).to_a, :total_count => 9999).page(5).per(10) }
it { should have(0).items }
its(:first) { should be_nil }
it { should have(10).items }
its(:first) { should == 1 }
its(:current_page) { should == 5 }
its(:total_count) { should == 9999 }
end
context "total_count == size of the given array" do
context "array 1..15, page 1, per 10, total_count 15" do
subject { Kaminari.paginate_array((1..15).to_a, :total_count => 15).page(1).per(10) }
it { should have(10).items }
its(:first) { should == 1 }
its(:current_page) { should == 1 }
its(:total_count) { should == 15 }
end
context "total_count < size of the given array" do
context "array 1..25, page 2, per 10, total_count 15" do
subject { Kaminari.paginate_array((1..25).to_a, :total_count => 15).page(2).per(10) }
it { should have(5).items }
its(:first) { should == 11 }
its(:current_page) { should == 2 }
its(:total_count) { should == 15 }
end
end