From 21ad994d42527fd97c8168b0d0ccdd88cec91251 Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Tue, 24 Jun 2014 18:19:21 -0700 Subject: [PATCH] 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 --- lib/kaminari/models/array_extension.rb | 10 +++++++--- spec/models/array_spec.rb | 13 ++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/kaminari/models/array_extension.rb b/lib/kaminari/models/array_extension.rb index 6d28485..b7c0816 100644 --- a/lib/kaminari/models/array_extension.rb +++ b/lib/kaminari/models/array_extension.rb @@ -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 diff --git a/spec/models/array_spec.rb b/spec/models/array_spec.rb index 30bd508..40938dc 100644 --- a/spec/models/array_spec.rb +++ b/spec/models/array_spec.rb @@ -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