1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Merge pull request #634 from kouyaf77/bugfix

Fixed a bug where paginates_per does not work with subclasses on mongoid
This commit is contained in:
Yuki Nishijima 2014-12-15 00:03:16 -05:00
parent a99068ead9
commit 5db0781a6a
3 changed files with 42 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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