Added #amount_pages to a paginated result array

This commit is contained in:
Matt Aimonetti 2009-08-03 12:19:07 -07:00
parent a17df45fc3
commit 889a923dbf
2 changed files with 15 additions and 4 deletions

View file

@ -1,5 +1,14 @@
module CouchRest module CouchRest
module Mixins module Mixins
module PaginatedResults
def amount_pages
@amount_pages ||= 0
end
def amount_pages=(value)
@amount_pages = value
end
end
module Collection module Collection
def self.included(base) def self.included(base)
@ -115,7 +124,10 @@ module CouchRest
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 @amount_pages ||= (results['total_rows'].to_f / per_page.to_f).ceil
remember_where_we_left_off(results, page) 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 end
# See Collection.paginated_each # See Collection.paginated_each

View file

@ -375,8 +375,7 @@ describe "ExtendedDocument views" do
end end
it "should have the amount of paginated pages" do it "should have the amount of paginated pages" do
articles = Article.by_date :key => Date.today articles = Article.by_date :key => Date.today
articles.paginate(:per_page => 3) articles.paginate(:per_page => 3).amount_pages.should == 3
articles.amount_pages.should == 3
end end
it "should provide a class method to access the collection directly" do it "should provide a class method to access the collection directly" do
articles = Article.collection_proxy_for('Article', 'by_date', :descending => true, articles = Article.collection_proxy_for('Article', 'by_date', :descending => true,