Permalink
Browse files

Adding support to Mongoid::Criteria

  • Loading branch information...
1 parent bedc7ab commit 2fc58a5ef69d3952dad65f77bbd5ce9041d8b898 Lucas Souza committed Oct 20, 2011
@@ -4,7 +4,8 @@
module WillPaginateMongoid
class Engine < ::Rails::Engine
initializer "including extensions" do
- ::Mongoid::Document.send :include, WillPaginateMongoid::MongoidPaginator::Document
+ ::Mongoid::Document.send :include, WillPaginateMongoid::MongoidPaginator
+ ::Mongoid::Criteria.send :include, WillPaginateMongoid::MongoidPaginator
end
end
end
@@ -2,32 +2,30 @@ module WillPaginateMongoid
DEFAULT_PER_PAGE = 10
module MongoidPaginator
- class << self
- def base_options(options)
+ extend ActiveSupport::Concern
+
+ included do
+ def self.paginate(options = {})
+ options = base_options options
+
+ WillPaginate::Collection.create(options[:page], options[:per_page]) do |pager|
+ fill_pager_with self.skip(options[:offset]).limit(options[:per_page]), self.count, pager
+ end
+ end
+
+ private
+
+ def self.base_options(options)
options[:page] ||= 1
options[:per_page] ||= 10
options[:offset] = (options[:page].to_i - 1) * options[:per_page]
options
end
- def fill_pager_with(medias, size, pager)
+ def self.fill_pager_with(medias, size, pager)
pager.replace medias.to_a
pager.total_entries = size
end
end
-
- module Document
- extend ActiveSupport::Concern
-
- included do
- def self.paginate(options = {})
- options = MongoidPaginator.base_options options
-
- WillPaginate::Collection.create(options[:page], options[:per_page]) do |pager|
- MongoidPaginator.fill_pager_with self.skip(options[:offset]).limit(options[:per_page]), self.count, pager
- end
- end
- end
- end
end
end
@@ -9,13 +9,13 @@ class Game
end
describe :paginate do
- before :each do
- @games = 30.times.map do |i|
- Game.create :title => "title_#{i}"
+ context "when called in a Mongoid::Document" do
+ before :each do
+ @games = 30.times.map do |i|
+ Game.create :title => "title_#{i}"
+ end
end
- end
- context "when called in a Mongoid::Document" do
context "and parameters is given" do
subject { Game.paginate :page => 2, :per_page => 10 }
it { should be_a_instance_of WillPaginate::Collection }
@@ -30,5 +30,27 @@ class Game
its(:total_entries) { should == 30 }
end
end
+
+ context "when called in a Mongoid::Criteria" do
+ before :each do
+ @games = 30.times.map do |i|
+ Game.create :title => "title"
+ end
+ end
+
+ context "and parameters is given" do
+ subject { Game.where(:title => "title").paginate :page => 2, :per_page => 10 }
+ it { should be_a_instance_of WillPaginate::Collection }
+ its(:to_a) { should == @games[10..19] }
+ its(:total_entries) { should == Game.count }
+ end
+
+ context "and parameters is not given" do
+ subject { Game.where(:title => "title").paginate }
+ it { should be_a_instance_of WillPaginate::Collection }
+ its(:to_a) { should == @games[0..9] }
+ its(:total_entries) { should == 30 }
+ end
+ end
end
end

0 comments on commit 2fc58a5

Please sign in to comment.