Browse files

Active Record 3.0: preserve pagination after `scoped` call

AR 3.1 is not affected.

Fixes #175
  • Loading branch information...
1 parent 93d23e9 commit 1c3e836e597b4c936bc23843d09915f92663a665 @mislav committed Sep 17, 2011
Showing with 22 additions and 5 deletions.
  1. +15 −5 lib/will_paginate/active_record.rb
  2. +7 −0 spec/finders/active_record_spec.rb
View
20 lib/will_paginate/active_record.rb
@@ -97,11 +97,12 @@ def total_pages
end
def clone
- other = super
- other.current_page = current_page unless other.current_page
- other.total_entries = nil if defined? @total_entries_queried
- other.wp_count_options = @wp_count_options if defined? @wp_count_options
- other
+ copy_will_paginate_data super
+ end
+
+ # workaround for Active Record 3.0
+ def scoped(options = nil)
+ copy_will_paginate_data super
end
def to_a
@@ -113,6 +114,15 @@ def to_a
end
end
end
+
+ private
+
+ def copy_will_paginate_data(other)
+ other.current_page = current_page unless other.current_page
+ other.total_entries = nil if defined? @total_entries_queried
+ other.wp_count_options = @wp_count_options if defined? @wp_count_options
+ other
+ end
end
module Pagination
View
7 spec/finders/active_record_spec.rb
@@ -91,6 +91,13 @@
rel = Developer.page(3).limit(3)
rel.offset.should == 6
end
+
+ it "keeps pagination data after 'scoped'" do
+ rel = Developer.page(2).scoped
+ rel.per_page.should == 10
+ rel.offset.should == 10
+ rel.current_page.should == 2
+ end
end
describe "counting" do

0 comments on commit 1c3e836

Please sign in to comment.