diff --git a/lib/couchrest/mixins/collection.rb b/lib/couchrest/mixins/collection.rb index e388146..a5f7d99 100644 --- a/lib/couchrest/mixins/collection.rb +++ b/lib/couchrest/mixins/collection.rb @@ -1,5 +1,14 @@ module CouchRest module Mixins + module PaginatedResults + def amount_pages + @amount_pages ||= 0 + end + def amount_pages=(value) + @amount_pages = value + end + end + module Collection def self.included(base) @@ -115,7 +124,10 @@ module CouchRest 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) + results = convert_to_container_array(results) + results.extend(PaginatedResults) + results.amount_pages = @amount_pages + results end # See Collection.paginated_each @@ -181,7 +193,7 @@ module CouchRest @target.inspect end - def convert_to_container_array(results) + def convert_to_container_array(results) if @container_class.nil? results else diff --git a/spec/couchrest/more/extended_doc_view_spec.rb b/spec/couchrest/more/extended_doc_view_spec.rb index f532e95..d23061a 100644 --- a/spec/couchrest/more/extended_doc_view_spec.rb +++ b/spec/couchrest/more/extended_doc_view_spec.rb @@ -375,8 +375,7 @@ describe "ExtendedDocument views" do 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 + articles.paginate(:per_page => 3).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,