Skip to content
Browse files

Pagination is now passing criteria to count when getting total entries.

  • Loading branch information...
1 parent 6606fd6 commit a8ea631e7b6f89ebbf51e8bedcec11ef22516998 @jnunemaker jnunemaker committed
Showing with 25 additions and 10 deletions.
  1. +10 −6 lib/plucky/query.rb
  2. +15 −4 test/plucky/test_query.rb
View
16 lib/plucky/query.rb
@@ -37,12 +37,16 @@ def per_page(limit=nil)
end
def paginate(opts={})
- total = count
- page = opts.delete(:page)
- limit = opts.delete(:per_page) || per_page
- paginator = Pagination::Paginator.new(total, page, limit)
- clone.update(opts).limit(paginator.limit).skip(paginator.skip).all.tap do |docs|
- docs.extend(Pagination::Decorator).paginator(paginator)
+ page = opts.delete(:page)
+ limit = opts.delete(:per_page) || per_page
+ query = clone.update(opts)
+ total = query.count
+ paginator = Pagination::Paginator.new(total, page, limit)
+ query[:limit] = paginator.limit
+ query[:skip] = paginator.skip
+ query.all.tap do |docs|
+ docs.extend(Pagination::Decorator)
+ docs.paginator(paginator)
end
end
View
19 test/plucky/test_query.rb
@@ -158,16 +158,27 @@ class QueryTest < Test::Unit::TestCase
docs.should respond_to(:total_entries)
end
- should "work with options" do
- subject.paginate(:name => 'John').should == [@john]
- end
-
should "not modify the original query" do
subject.paginate(:name => 'John')
subject[:page].should be_nil
subject[:per_page].should be_nil
subject[:name].should be_nil
end
+
+ context "with options" do
+ setup do
+ @result = @query.sort(:age).paginate(:age.gt => 27, :per_page => 10)
+ end
+ subject { @result }
+
+ should "only return matching" do
+ subject.should == [@john, @steve]
+ end
+
+ should "correctly count matching" do
+ subject.total_entries.should == 2
+ end
+ end
end
context "#all" do

0 comments on commit a8ea631

Please sign in to comment.
Something went wrong with that request. Please try again.