Permalink
Browse files

fix pagination again...

  • Loading branch information...
1 parent 06ef1e7 commit 67c93865fbcf0bceda290c773b264fff909c4724 @ryanong committed Jul 25, 2011
@@ -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)
@@ -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
@@ -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

0 comments on commit 67c9386

Please sign in to comment.