Skip to content
This repository has been archived by the owner on Jul 31, 2019. It is now read-only.

Commit

Permalink
[Finishes sunspot#42] Adds :offset option to paginate method
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin fleischer authored and alindeman committed Oct 30, 2011
1 parent 7e3ac34 commit 2a99fd3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions sunspot/History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion sunspot/lib/sunspot/dsl/paginatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions sunspot/lib/sunspot/query/common_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions sunspot/lib/sunspot/query/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions sunspot/spec/api/query/ordering_pagination_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions sunspot/spec/integration/test_pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.