lib/couchrest/mixins/collection.rb in norr-couchrest-0.33.02 vs lib/couchrest/mixins/collection.rb in norr-couchrest-0.33.06
- old
+ new
@@ -1,16 +1,7 @@
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)
base.extend(ClassMethods)
end
@@ -91,12 +82,10 @@
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
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
# given type for each row that comes back from the view. If no
# container class is specified, the raw results are returned.
#
@@ -120,15 +109,12 @@
# See Collection.paginate
def paginate(options = {})
page, per_page = parse_options(options)
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)
results = convert_to_container_array(results)
- results.extend(PaginatedResults)
- results.amount_pages = @amount_pages
results
end
# See Collection.paginated_each
def paginated_each(options = {}, &block)
@@ -202,11 +188,12 @@
end
def pagination_options(page, per_page)
view_options = @view_options.clone
if @last_key && @last_docid && @last_page == page - 1
- view_options.delete(:key)
- options = { :startkey => @last_key, :startkey_docid => @last_docid, :limit => per_page, :skip => 1 }
+ key = view_options.delete(:key)
+ end_key = view_options[:endkey] || key
+ options = { :startkey => @last_key, :endkey => end_key, :startkey_docid => @last_docid, :limit => per_page, :skip => 1 }
else
options = { :limit => per_page, :skip => per_page * (page - 1) }
end
view_options.merge(options)
end