diff --git a/lib/kaminari/config.rb b/lib/kaminari/config.rb index 0c5025c..1e709ee 100644 --- a/lib/kaminari/config.rb +++ b/lib/kaminari/config.rb @@ -24,6 +24,7 @@ module Kaminari config_accessor :left config_accessor :right config_accessor :page_method_name + config_accessor :max_pages def param_name config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name @@ -45,5 +46,6 @@ module Kaminari config.right = 0 config.page_method_name = :page config.param_name = :page + config.max_pages = nil end end diff --git a/lib/kaminari/models/configuration_methods.rb b/lib/kaminari/models/configuration_methods.rb index bfff9ac..eab0ef7 100644 --- a/lib/kaminari/models/configuration_methods.rb +++ b/lib/kaminari/models/configuration_methods.rb @@ -29,6 +29,20 @@ module Kaminari def max_per_page @_max_per_page || Kaminari.config.max_per_page end + + # Overrides the max_pages value per model + # class Article < ActiveRecord::Base + # max_pages_per 100 + # end + def max_pages_per(val) + @_max_pages = val + end + + # This model's max_pages value + # returns max_pages value unless explicitly overridden via max_pages_per + def max_pages + @_max_pages || Kaminari.config.max_pages + end end end end diff --git a/lib/kaminari/models/page_scope_methods.rb b/lib/kaminari/models/page_scope_methods.rb index 7008bc0..60a9557 100644 --- a/lib/kaminari/models/page_scope_methods.rb +++ b/lib/kaminari/models/page_scope_methods.rb @@ -18,7 +18,12 @@ module Kaminari # Total number of pages def total_pages - (total_count.to_f / limit_value).ceil + total_pages_count = (total_count.to_f / limit_value).ceil + if max_pages.present? && max_pages < total_pages_count + max_pages + else + total_pages_count + end end #FIXME for compatibility. remove num_pages at some time in the future alias num_pages total_pages diff --git a/spec/config/config_spec.rb b/spec/config/config_spec.rb index 3215a4d..260d07b 100644 --- a/spec/config/config_spec.rb +++ b/spec/config/config_spec.rb @@ -73,4 +73,19 @@ describe Kaminari::Configuration do end end end + + describe 'max_pages' do + context 'by default' do + its(:max_pages) { should == nil } + end + context 'configure via config block' do + before do + Kaminari.configure {|c| c.max_pages = 5} + end + its(:max_pages) { should == 5 } + after do + Kaminari.configure {|c| c.max_pages = nil} + end + end + end end diff --git a/spec/models/active_record/max_pages_spec.rb b/spec/models/active_record/max_pages_spec.rb new file mode 100644 index 0000000..91d8c95 --- /dev/null +++ b/spec/models/active_record/max_pages_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +if defined? ActiveRecord + + describe 'max pages' do + describe 'AR::Base' do + subject { ActiveRecord::Base } + it { should_not respond_to :max_pages_per } + end + + subject { User.page 0 } + + context 'by default' do + its(:max_pages) { should == nil } + end + + context 'when explicitly set via max_pages_per' do + before { User.max_pages_per 3 } + its(:max_pages) { should == 3 } + after { User.max_pages_per nil } + end + end +end diff --git a/spec/models/active_record/scopes_spec.rb b/spec/models/active_record/scopes_spec.rb index 2911e2f..a555e34 100644 --- a/spec/models/active_record/scopes_spec.rb +++ b/spec/models/active_record/scopes_spec.rb @@ -98,6 +98,26 @@ if defined? ActiveRecord subject { model_class.page(5).per('aho') } its(:total_pages) { should == 4 } end + + context 'with max_pages < total pages count from database' do + before { model_class.max_pages_per 3 } + subject { model_class.page } + its(:total_pages) { should == 3 } + after { model_class.max_pages_per nil } + end + + context 'with max_pages > total pages count from database' do + before { model_class.max_pages_per 11 } + subject { model_class.page } + its(:total_pages) { should == 4 } + after { model_class.max_pages_per nil } + end + + context 'with max_pages is nil' do + before { model_class.max_pages_per nil } + subject { model_class.page } + its(:total_pages) { should == 4 } + end end