Permalink
Browse files

[Finishes #42] Adds :offset option to paginate method

  • Loading branch information...
1 parent 7e3ac34 commit 2a99fd395a49f4137fd4d2ced0610367ecabf4fe benjamin fleischer committed with alindeman Oct 30, 2011
View
1 sunspot/History.txt
@@ -10,6 +10,7 @@
* Packaged Solr correctly uses the `pid_dir` configuration option from `sunspot.yml` (Russen Guggemos)
* Specs run correctly in 1.9.2 (Larry Sprock)
* Documentation improvements (Thibaut Barrère, gjb83, Breno Santos Salgado)
+* Adds :offset option to paginate method (Benjamin Fleischer)
== 1.2.1 2010-12-28
* Decreased default reindexing batch size from 500 to 50
View
6 sunspot/lib/sunspot/dsl/paginatable.rb
@@ -17,11 +17,15 @@ module Paginatable
# How many results to return per page. The default is the value in
# +Sunspot.config.pagination.default_per_page+
#
+ # :offset<Integer,String>::
+ # Applies a shift to paginated records. The default is 0.
+ #
def paginate(options = {})
page = options.delete(:page)
per_page = options.delete(:per_page)
+ offset = options.delete(:offset)
raise ArgumentError, "unknown argument #{options.keys.first.inspect} passed to paginate" unless options.empty?
- @query.paginate(page, per_page)
+ @query.paginate(page, per_page, offset)
end
end
end
View
5 sunspot/lib/sunspot/query/common_query.rb
@@ -35,12 +35,13 @@ def add_function(function)
function
end
- def paginate(page, per_page)
+ def paginate(page, per_page, offset = nil)
if @pagination
+ @pagination.offset = offset
@pagination.page = page
@pagination.per_page = per_page
else
- @components << @pagination = Pagination.new(page, per_page)
+ @components << @pagination = Pagination.new(page, per_page, offset)
end
end
View
12 sunspot/lib/sunspot/query/pagination.rb
@@ -6,10 +6,10 @@ module Query
# reference to it and updates it if pagination is changed.
#
class Pagination #:nodoc:
- attr_reader :page, :per_page
+ attr_reader :page, :per_page, :offset
- def initialize(page = nil, per_page = nil)
- self.page, self.per_page = page, per_page
+ def initialize(page = nil, per_page = nil, offset = nil)
+ self.offset, self.page, self.per_page = offset, page, per_page
end
def to_params
@@ -24,10 +24,14 @@ def per_page=(per_page)
@per_page = per_page.to_i if per_page
end
+ def offset=(offset)
+ @offset = offset.to_i
+ end
+
private
def start
- (@page - 1) * @per_page
+ (@page - 1) * @per_page + @offset
end
def rows
View
14 sunspot/spec/api/query/ordering_pagination_examples.rb
@@ -25,6 +25,20 @@
connection.should have_last_search_with(:rows => 15, :start => 0)
end
+ it 'paginates with an offset' do
+ search do
+ paginate :per_page => 15, :offset => 3
+ end
+ connection.should have_last_search_with(:rows => 15, :start => 3)
+ end
+
+ it 'paginates with an offset as a string' do
+ search do
+ paginate :per_page => 15, :offset => '3'
+ end
+ connection.should have_last_search_with(:rows => 15, :start => 3)
+ end
+
it 'paginates from string argument' do
search do
paginate :page => '3', :per_page => '15'
View
11 sunspot/spec/integration/test_pagination.rb
@@ -29,4 +29,15 @@
end.results
results.should == @posts[10,10]
end
+
+ it 'should return pages with offsets' do
+ results = Sunspot.search(Post) do
+ order_by :blog_id
+ paginate :page => 2, :per_page => 5, :offset => 3
+ end.results
+
+ # page 1 is 3, 4, 5, 6, 7
+ # page 2 is 8, 9, 10, 11, 12
+ results.should == @posts[8,5]
+ end
end

0 comments on commit 2a99fd3

Please sign in to comment.