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 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

View file

@ -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,