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:
parent
a99068ead9
commit
5db0781a6a
3 changed files with 42 additions and 7 deletions
|
@ -7,13 +7,28 @@ module Kaminari
|
||||||
include Kaminari::ConfigurationMethods
|
include Kaminari::ConfigurationMethods
|
||||||
|
|
||||||
included do
|
included do
|
||||||
# Fetch the values at the specified page number
|
Kaminari::MongoidExtension::Document.send(:define_scope, self)
|
||||||
# Model.page(5)
|
|
||||||
scope Kaminari.config.page_method_name, Proc.new {|num|
|
class << self
|
||||||
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
|
def inherited_with_kaminari(kls)
|
||||||
} do
|
inherited_without_kaminari(kls)
|
||||||
include Kaminari::MongoidCriteriaMethods
|
Kaminari::MongoidExtension::Document.send(:define_scope, kls)
|
||||||
include Kaminari::PageScopeMethods
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,14 @@ class User::Address
|
||||||
include ::Mongoid::Document
|
include ::Mongoid::Document
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Product
|
||||||
|
include ::Mongoid::Document
|
||||||
|
end
|
||||||
|
|
||||||
|
class Device < Product
|
||||||
|
paginates_per 100
|
||||||
|
end
|
||||||
|
|
||||||
class MongoMongoidExtensionDeveloper
|
class MongoMongoidExtensionDeveloper
|
||||||
include ::Mongoid::Document
|
include ::Mongoid::Document
|
||||||
field :salary, :type => Integer
|
field :salary, :type => Integer
|
||||||
|
|
|
@ -207,5 +207,17 @@ if defined? Mongoid
|
||||||
its(:total_pages) { should == 2 }
|
its(:total_pages) { should == 2 }
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue