diff --git a/lib/kaminari/helpers/action_view_extension.rb b/lib/kaminari/helpers/action_view_extension.rb index f7c61ab..c4bcd14 100644 --- a/lib/kaminari/helpers/action_view_extension.rb +++ b/lib/kaminari/helpers/action_view_extension.rb @@ -66,7 +66,7 @@ module Kaminari def link_to_next_page(scope, name, options = {}, &block) next_page = Kaminari::Helpers::NextPage.new self, options.reverse_merge(:current_page => scope.current_page) - link_to_unless scope.last_page?, name, next_page.url, options.except(:params, :param_name).reverse_merge(:rel => 'next') do + link_to_unless scope.last_page? || scope.out_of_range?, name, next_page.url, options.except(:params, :param_name).reverse_merge(:rel => 'next') do block.call if block end end diff --git a/lib/kaminari/helpers/sinatra_helpers.rb b/lib/kaminari/helpers/sinatra_helpers.rb index de8e963..4663ccc 100644 --- a/lib/kaminari/helpers/sinatra_helpers.rb +++ b/lib/kaminari/helpers/sinatra_helpers.rb @@ -145,7 +145,7 @@ module Kaminari::Helpers param_name = options.delete(:param_name) || Kaminari.config.param_name placeholder = options.delete(:placeholder) - unless scope.last_page? + unless scope.last_page? || scope.out_of_range? query = params.merge(param_name => scope.next_page) link_to name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.reverse_merge(:rel => 'next') else diff --git a/lib/kaminari/models/page_scope_methods.rb b/lib/kaminari/models/page_scope_methods.rb index b47134a..eb34948 100644 --- a/lib/kaminari/models/page_scope_methods.rb +++ b/lib/kaminari/models/page_scope_methods.rb @@ -44,7 +44,7 @@ module Kaminari # Next page number in the collection def next_page - current_page + 1 unless last_page? + current_page + 1 unless last_page? || out_of_range? end # Previous page number in the collection @@ -59,7 +59,7 @@ module Kaminari # Last page of the collection? def last_page? - current_page >= total_pages + current_page == total_pages end # Out of range of the collection? diff --git a/spec/models/active_record/scopes_spec.rb b/spec/models/active_record/scopes_spec.rb index 44bfa64..e938c62 100644 --- a/spec/models/active_record/scopes_spec.rb +++ b/spec/models/active_record/scopes_spec.rb @@ -210,10 +210,15 @@ if defined? ActiveRecord its(:last_page?) { should == true } end - context 'not on last page' do + context 'within range' do subject { model_class.page(1).per(10) } its(:last_page?) { should == false } end + + context 'out of range' do + subject { model_class.page(11).per(10) } + its(:last_page?) { should == false } + end end describe '#out_of_range?' do