Skip to content

Commit

Permalink
Switching pagination over to will_paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 1, 2009
1 parent 80fefd0 commit 330ce3b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions History.txt
@@ -0,0 +1,2 @@
0.2.5:
- Switching pagination over to will_paginate
11 changes: 9 additions & 2 deletions lib/mongoid/document.rb
Expand Up @@ -101,7 +101,14 @@ def index(name, options = { :unique => false })
# Find all documents in paginated fashion given the supplied arguments.
# If no parameters are passed just default to offset 0 and limit 20.
def paginate(selector = {}, params = {})
collection.find(selector[:conditions], Mongoid::Paginator.new(params).options).collect { |doc| new(doc) }
WillPaginate::Collection.create(
params[:page] || 1,
params[:per_page] || 20,
0) do |pager|
results = collection.find(selector[:conditions], { :limit => pager.per_page, :offset => pager.offset })
pager.total_entries = results.count
pager.replace(results.collect { |doc| new(doc) })
end
end

end
Expand Down Expand Up @@ -168,7 +175,7 @@ def update_attributes(attributes)

class << self

# Adds the association to the associations hash with the type as the key,
# Adds the association to the associations hash with the type as the key,
# then adds the accessors for the association.
def add_association(type, class_name, name)
define_method(name) do
Expand Down
4 changes: 2 additions & 2 deletions mongoid.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{mongoid}
s.version = "0.2.4"
s.version = "0.2.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Durran Jordan"]
s.date = %q{2009-09-27}
s.date = %q{2009-10-01}
s.email = %q{durran@gmail.com}
s.extra_rdoc_files = [
"README.textile"
Expand Down
12 changes: 8 additions & 4 deletions spec/unit/mongoid/document_spec.rb
Expand Up @@ -364,19 +364,23 @@

describe "#paginate" do

before do
@cursor = stub(:count => 100, :collect => [])
end

context "when pagination parameters are passed" do

it "delegates offset and limit to find_all" do
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 20}).returns([])
it "delegates to will paginate with the results" do
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 20}).returns(@cursor)
Person.paginate({ :conditions => { :test => "Test" } }, { :page => 2, :per_page => 20 })
end

end

context "when pagination parameters are not passed" do

it "passes the default offset and limit to find_all" do
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 0}).returns([])
it "delegates to will paginate with default values" do
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 0}).returns(@cursor)
Person.paginate(:conditions => { :test => "Test" })
end

Expand Down

0 comments on commit 330ce3b

Please sign in to comment.