diff --git a/lib/kaminari/models/mongoid_extension.rb b/lib/kaminari/models/mongoid_extension.rb index eb50cd0..2ebbc84 100644 --- a/lib/kaminari/models/mongoid_extension.rb +++ b/lib/kaminari/models/mongoid_extension.rb @@ -7,13 +7,28 @@ module Kaminari include Kaminari::ConfigurationMethods included do - # Fetch the values at the specified page number - # Model.page(5) - scope Kaminari.config.page_method_name, Proc.new {|num| - limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1)) - } do - include Kaminari::MongoidCriteriaMethods - include Kaminari::PageScopeMethods + Kaminari::MongoidExtension::Document.send(:define_scope, self) + + class << self + def inherited_with_kaminari(kls) + inherited_without_kaminari(kls) + Kaminari::MongoidExtension::Document.send(:define_scope, kls) + end + alias_method_chain :inherited, :kaminari + end + end + + private + def self.define_scope(kls) + kls.class_eval do + # Fetch the values at the specified page number + # Model.page(5) + scope Kaminari.config.page_method_name, Proc.new {|num| + limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1)) + } do + include Kaminari::MongoidCriteriaMethods + include Kaminari::PageScopeMethods + end end end end diff --git a/spec/fake_app/mongoid/models.rb b/spec/fake_app/mongoid/models.rb index b2da0fd..d917b2d 100644 --- a/spec/fake_app/mongoid/models.rb +++ b/spec/fake_app/mongoid/models.rb @@ -12,6 +12,14 @@ class User::Address include ::Mongoid::Document end +class Product + include ::Mongoid::Document +end + +class Device < Product + paginates_per 100 +end + class MongoMongoidExtensionDeveloper include ::Mongoid::Document field :salary, :type => Integer diff --git a/spec/models/mongoid/mongoid_spec.rb b/spec/models/mongoid/mongoid_spec.rb index b599237..41ff832 100644 --- a/spec/models/mongoid/mongoid_spec.rb +++ b/spec/models/mongoid/mongoid_spec.rb @@ -207,5 +207,17 @@ if defined? Mongoid its(:total_pages) { should == 2 } end end + + describe '#paginates_per' do + context 'when paginates_per is not defined in superclass' do + subject { Product.all.page 1 } + its(:limit_value) { should == 25 } + end + + context 'when paginates_per is defined in subclass' do + subject { Device.all.page 1 } + its(:limit_value) { should == 100 } + end + end end end