From 413a7602e8f8616742c9195605359c57f34799e1 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Mon, 20 Jul 2009 15:52:14 -0700 Subject: [PATCH] added amount_pages to a paginated collection --- history.txt | 17 +++++++++++++++++ lib/couchrest/mixins/collection.rb | 5 ++++- spec/couchrest/more/extended_doc_view_spec.rb | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/history.txt b/history.txt index 1d886de..a026ff0 100644 --- a/history.txt +++ b/history.txt @@ -1,3 +1,20 @@ +== 0.40 + +=== Notes + + This release slightly modifies the API and if you were using a previous version of CouchRest, +you will need to make the following modifications. + + + +* Major enhancements + + * + +* Minor enhancements + + * Added #amount_pages to a paginated collection. (Matt Aimonetti) + == 0.31 * Major enhancements diff --git a/lib/couchrest/mixins/collection.rb b/lib/couchrest/mixins/collection.rb index b492dbe..e388146 100644 --- a/lib/couchrest/mixins/collection.rb +++ b/lib/couchrest/mixins/collection.rb @@ -83,6 +83,8 @@ module CouchRest DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 30 + + attr_accessor :amount_pages # Create a new CollectionProxy to represent the specified view. If a # container class is specified, the proxy will create an object of the @@ -110,7 +112,8 @@ module CouchRest # See Collection.paginate def paginate(options = {}) page, per_page = parse_options(options) - results = @database.view(@view_name, pagination_options(page, per_page)) + results = @database.view(@view_name, pagination_options(page, per_page)) + @amount_pages ||= (results['total_rows'].to_f / per_page.to_f).ceil remember_where_we_left_off(results, page) convert_to_container_array(results) end diff --git a/spec/couchrest/more/extended_doc_view_spec.rb b/spec/couchrest/more/extended_doc_view_spec.rb index 7c5bbd2..f735c4c 100644 --- a/spec/couchrest/more/extended_doc_view_spec.rb +++ b/spec/couchrest/more/extended_doc_view_spec.rb @@ -365,6 +365,11 @@ describe "ExtendedDocument views" do articles.paginated_each(:per_page => 3) do |a| a.should_not be_nil end + end + it "should have the amount of paginated pages" do + articles = Article.by_date :key => Date.today + articles.paginate(:per_page => 3) + articles.amount_pages.should == 3 end it "should provide a class method to access the collection directly" do articles = Article.collection_proxy_for('Article', 'by_date', :descending => true,