Skip to content

Commit

Permalink
fix pagination again...
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanong committed Jul 25, 2011
1 parent 06ef1e7 commit 67c9386
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
22 changes: 13 additions & 9 deletions lib/mongoid_spacial/spacial/geo_near_results.rb
Expand Up @@ -48,23 +48,27 @@ def initialize(document,results,opts = {})
end
end

def page(page, options = {})
def page(*args)
new_collection = self.clone
original = options.delete(:original)
new_collection.opts.merge!(options)
new_collection.opts[:paginator] ||= Mongoid::Spacial.paginator
new_collection.page!(*args)
new_collection
end

start = (new_collection.current_page-1)*new_collection.limit_value # assuming current_page is 1 based.
def page!(page, options = {})
original = options.delete(:original)
self.opts.merge!(options)
self.opts[:paginator] ||= Mongoid::Spacial.paginator
self.opts[:page] = page
start = (self.current_page-1)*self.limit_value # assuming current_page is 1 based.

if original
@_paginated_array = @_original_array.clone
new_collection.replace(@_original_array[new_collection.opts[:skip]+start, new_collection.limit_value] || [])
self.replace(@_paginated_array[self.opts[:skip]+start, self.limit_value] || [])
else
@_paginated_array ||= self.to_a
new_collection.replace(@_paginated_array[new_collection.opts[:skip]+start, new_collection.limit_value])
self.replace(@_paginated_array[self.opts[:skip]+start, self.limit_value])
end

new_collection
true
end

def per(num)
Expand Down
9 changes: 7 additions & 2 deletions spec/functional/mongoid/spacial/geo_near_results_spec.rb
Expand Up @@ -16,8 +16,12 @@
end

context ":paginator :array" do
let!(:bars) { Bar.geo_near([1,1]) }
let!(:sorted_bars) { Bar.geo_near([1,1]).sort_by {|b| b.name.to_i}}
let(:bars) { Bar.geo_near([1,1]) }
let(:sorted_bars) {
bars = Bar.geo_near([1,1])
bars.sort_by! {|b| b.name.to_i}
bars
}
[nil,1,2].each do |page|
it "page=#{page} should have 25" do
bars.page(page).size.should == 25
Expand Down Expand Up @@ -48,6 +52,7 @@
it "page=10 per=5" do
bars.per(5).page(10).should == bars[45..50]
end

end

context ":paginator :kaminari" do
Expand Down

0 comments on commit 67c9386

Please sign in to comment.