Adding initial pagination support based on kaminari
This commit is contained in:
parent
6a896c27b3
commit
6723564969
4 changed files with 143 additions and 21 deletions
|
@ -21,6 +21,8 @@ module CouchRest
|
|||
|
||||
module ClassMethods
|
||||
|
||||
# Add views and other design document features
|
||||
# to the current model.
|
||||
def design(*args, &block)
|
||||
mapper = DesignMapper.new(self)
|
||||
mapper.create_view_method(:all)
|
||||
|
@ -30,6 +32,23 @@ module CouchRest
|
|||
req_design_doc_refresh
|
||||
end
|
||||
|
||||
# Override the default page pagination value:
|
||||
#
|
||||
# class Person < CouchRest::Model::Base
|
||||
# paginates_per 10
|
||||
# end
|
||||
#
|
||||
def paginates_per(val)
|
||||
@_default_per_page = val
|
||||
end
|
||||
|
||||
# The models number of documents to return
|
||||
# by default when performing pagination.
|
||||
# Returns 25 unless explicitly overridden via <tt>paginates_per</tt>
|
||||
def default_per_page
|
||||
@_default_per_page || 25
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
#
|
||||
|
|
|
@ -14,7 +14,7 @@ module CouchRest
|
|||
class View
|
||||
include Enumerable
|
||||
|
||||
attr_accessor :model, :name, :query, :result
|
||||
attr_accessor :owner, :model, :name, :query, :result
|
||||
|
||||
# Initialize a new View object. This method should not be called from
|
||||
# outside CouchRest Model.
|
||||
|
@ -22,11 +22,13 @@ module CouchRest
|
|||
if parent.is_a?(Class) && parent < CouchRest::Model::Base
|
||||
raise "Name must be provided for view to be initialized" if name.nil?
|
||||
self.model = parent
|
||||
self.owner = parent
|
||||
self.name = name.to_s
|
||||
# Default options:
|
||||
self.query = { :reduce => false }
|
||||
elsif parent.is_a?(self.class)
|
||||
self.model = (new_query.delete(:proxy) || parent.model)
|
||||
self.owner = parent.owner
|
||||
self.name = parent.name
|
||||
self.query = parent.query.dup
|
||||
else
|
||||
|
@ -174,7 +176,6 @@ module CouchRest
|
|||
# modified appropriatly. Errors will be raised if the methods
|
||||
# are combined in an incorrect fashion.
|
||||
#
|
||||
|
||||
|
||||
# Find all entries in the index whose key matches the value provided.
|
||||
#
|
||||
|
@ -300,6 +301,46 @@ module CouchRest
|
|||
@docs = nil
|
||||
end
|
||||
|
||||
# == Kaminari compatible pagination support
|
||||
#
|
||||
# Based on the really simple support for scoped pagination in the
|
||||
# the Kaminari gem, we provide compatible methods here to perform
|
||||
# the same actions you'd expect.
|
||||
#
|
||||
|
||||
alias :total_count :count
|
||||
|
||||
def page(page)
|
||||
limit(owner.default_per_page).skip(owner.default_per_page * ([page.to_i, 1].max - 1))
|
||||
end
|
||||
|
||||
def per(num)
|
||||
raise "View#page must be called before #per!" if limit_value.nil? || offset_value.nil?
|
||||
if (n = num.to_i) <= 0
|
||||
self
|
||||
else
|
||||
limit(num).skip(offset_value / limit_value * n)
|
||||
end
|
||||
end
|
||||
|
||||
def offset_value
|
||||
query[:skip]
|
||||
end
|
||||
|
||||
def limit_value
|
||||
query[:limit]
|
||||
end
|
||||
|
||||
def num_pages
|
||||
(total_count.to_f / limit_value).ceil
|
||||
end
|
||||
|
||||
def current_page
|
||||
(offset_value / limit_value) + 1
|
||||
end
|
||||
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def include_docs!
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue