Permalink
Browse files

Handle copied arrays

  • Loading branch information...
1 parent fc850ff commit 234f1d3967e62b3e4f9c0dd371429538d2dcc0e3 @mmangino committed Mar 21, 2012
Showing with 18 additions and 2 deletions.
  1. +12 −2 lib/will_paginate/collection.rb
  2. +6 −0 spec/collection_spec.rb
View
14 lib/will_paginate/collection.rb
@@ -133,9 +133,19 @@ def replace(array)
result
end
+ # We want to return paginated collections when mapped. Unfortunately
+ # when arrays are copied in C code for slices or other operations,
+ # they are initialized using the array initializer, not ours. This
+ # means that current page is lost and creating a new array is impossible.
+ # we treat copied arrays as non-paginated collections and just delegate to super.
+
def map(&block)
- self.class.create(current_page, per_page, total_entries) do |pager|
- pager.replace(super(&block))
+ if current_page.nil?
+ super(&block)
+ else
+ self.class.create(current_page, per_page, total_entries) do |pager|
+ pager.replace(super(&block))
+ end
end
end
end
View
6 spec/collection_spec.rb
@@ -136,6 +136,12 @@
mapped.per_page.should == 5
mapped.total_entries.should == 100
end
+
+ it "should not raise an error when the array is copied by C code" do
+ collection = create(2,5,100) {|p| p.replace([1,2,3])}
+ copied = collection[0..1]
+ copied.map {|a| a*2}
+ end
end
private

0 comments on commit 234f1d3

Please sign in to comment.