mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
Added MongoMapper support
This commit is contained in:
parent
86cc954510
commit
5bbc32741f
5 changed files with 125 additions and 1 deletions
|
@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|||
s.add_development_dependency 'bundler', ['>= 1.0.0']
|
||||
s.add_development_dependency 'sqlite3', ['>= 0']
|
||||
s.add_development_dependency 'mongoid', ['>= 2']
|
||||
s.add_development_dependency 'mongo_mapper', ['>= 0.9']
|
||||
s.add_development_dependency 'rspec', ['>= 0']
|
||||
s.add_development_dependency 'rspec-rails', ['>= 0']
|
||||
s.add_development_dependency 'rr', ['>= 0']
|
||||
|
|
18
lib/kaminari/models/mongo_mapper_extension.rb
Normal file
18
lib/kaminari/models/mongo_mapper_extension.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
require File.join(File.dirname(__FILE__), 'plucky_criteria_methods')
|
||||
|
||||
module Kaminari
|
||||
module MongoMapperExtension
|
||||
module Document
|
||||
extend ActiveSupport::Concern
|
||||
include Kaminari::ConfigurationMethods
|
||||
|
||||
included do
|
||||
# Fetch the values at the specified page number
|
||||
# Model.page(5)
|
||||
scope :page, Proc.new {|num|
|
||||
limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
18
lib/kaminari/models/plucky_criteria_methods.rb
Normal file
18
lib/kaminari/models/plucky_criteria_methods.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
module Kaminari
|
||||
module PluckyCriteriaMethods
|
||||
extend ActiveSupport::Concern
|
||||
module InstanceMethods
|
||||
def limit_value #:nodoc:
|
||||
options[:limit]
|
||||
end
|
||||
|
||||
def offset_value #:nodoc:
|
||||
options[:skip]
|
||||
end
|
||||
|
||||
def total_count #:nodoc:
|
||||
count
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,7 @@
|
|||
require 'rails'
|
||||
# ensure ORMs are loaded *before* initializing Kaminari
|
||||
begin; require 'mongoid'; rescue LoadError; end
|
||||
begin; require 'mongo_mapper'; rescue LoadError; end
|
||||
|
||||
require File.join(File.dirname(__FILE__), 'config')
|
||||
require File.join(File.dirname(__FILE__), 'helpers/action_view_extension')
|
||||
|
@ -20,10 +21,16 @@ module Kaminari
|
|||
::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
|
||||
::Mongoid::Criteria.send :include, Kaminari::MongoidExtension::Criteria
|
||||
end
|
||||
if defined? ::MongoMapper
|
||||
require File.join(File.dirname(__FILE__), 'models/mongo_mapper_extension')
|
||||
::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
|
||||
::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
|
||||
::Plucky::Query.send :include, Kaminari::PageScopeMethods
|
||||
end
|
||||
require File.join(File.dirname(__FILE__), 'models/array_extension')
|
||||
ActiveSupport.on_load(:action_view) do
|
||||
::ActionView::Base.send :include, Kaminari::ActionViewExtension
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
80
spec/models/mongo_mapper_spec.rb
Normal file
80
spec/models/mongo_mapper_spec.rb
Normal file
|
@ -0,0 +1,80 @@
|
|||
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
||||
require 'mongo_mapper'
|
||||
require File.expand_path('../../lib/kaminari/models/mongo_mapper_extension', File.dirname(__FILE__))
|
||||
|
||||
describe Kaminari::MongoMapperExtension do
|
||||
before :all do
|
||||
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
|
||||
MongoMapper.database = "kaminari_test"
|
||||
class Developer
|
||||
include ::MongoMapper::Document
|
||||
key :salary, Integer
|
||||
end
|
||||
end
|
||||
before do
|
||||
stub(subject).count { 300 } # in order to avoid DB access...
|
||||
end
|
||||
|
||||
describe '#page' do
|
||||
context 'page 1' do
|
||||
subject { Developer.page(1) }
|
||||
it { should be_a Plucky::Query }
|
||||
its(:current_page) { should == 1 }
|
||||
its(:limit_value) { should == 25 }
|
||||
its(:num_pages) { should == 12 }
|
||||
it { should skip(0) }
|
||||
end
|
||||
|
||||
context 'page 2' do
|
||||
subject { Developer.page 2 }
|
||||
it { should be_a Plucky::Query }
|
||||
its(:current_page) { should == 2 }
|
||||
its(:limit_value) { should == 25 }
|
||||
its(:num_pages) { should == 12 }
|
||||
it { should skip 25 }
|
||||
end
|
||||
|
||||
context 'page "foobar"' do
|
||||
subject { Developer.page 'foobar' }
|
||||
it { should be_a Plucky::Query }
|
||||
its(:current_page) { should == 1 }
|
||||
its(:limit_value) { should == 25 }
|
||||
its(:num_pages) { should == 12 }
|
||||
it { should skip 0 }
|
||||
end
|
||||
|
||||
context 'with criteria before' do
|
||||
it "should have the proper criteria source" do
|
||||
Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
|
||||
end
|
||||
|
||||
subject { Developer.where(:salary => 1).page 2 }
|
||||
its(:current_page) { should == 2 }
|
||||
its(:limit_value) { should == 25 }
|
||||
its(:num_pages) { should == 12 }
|
||||
it { should skip 25 }
|
||||
end
|
||||
|
||||
context 'with criteria after' do
|
||||
it "should have the proper criteria source" do
|
||||
Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
|
||||
end
|
||||
|
||||
subject { Developer.page(2).where(:salary => 1) }
|
||||
its(:current_page) { should == 2 }
|
||||
its(:limit_value) { should == 25 }
|
||||
its(:num_pages) { should == 12 }
|
||||
it { should skip 25 }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe '#per' do
|
||||
subject { Developer.page(2).per(10) }
|
||||
it { should be_a Plucky::Query }
|
||||
its(:current_page) { should == 2 }
|
||||
its(:limit_value) { should == 10 }
|
||||
its(:num_pages) { should == 30 }
|
||||
it { should skip 10 }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue