Permalink
Browse files

Will Paginate: fix count queries when conditions are used in combinat…

…ion with dynamic finders. Thanks, David Richards. [#83 state:resolved]

git-svn-id: svn://errtheblog.com/svn/plugins/will_paginate@328 1eaa51fe-a21a-0410-9c2e-ae7a00a434c4
  • Loading branch information...
1 parent 4aebf93 commit 8ff23142f15400ba929ec67e887dd3cc02211c05 mislav committed Aug 18, 2007
Showing with 13 additions and 5 deletions.
  1. +7 −5 lib/will_paginate/finder.rb
  2. +6 −0 test/finder_test.rb
View
12 lib/will_paginate/finder.rb
@@ -112,15 +112,17 @@ def wp_count!(options, args, finder)
# this allows you to specify :select, :order, or anything else just for the count query
count_options.update(options.delete(:count)) if options.key? :count
# extract the conditions from calls like "paginate_by_foo_and_bar"
- wp_extract_finder_conditions(finder, args, count_options) unless count_options[:conditions]
+ conditions = wp_extract_finder_conditions(finder, args, count_options)
# scope_out adds a 'with_finder' method which acts like with_scope, if it's present
- # then exectute the count with the scoping provided by the with_finder
+ # then execute the count with the scoping provided by the with_finder
count = nil
+ counter = Proc.new { count = count(count_options) }
+
if respond_to?(scoper = finder.sub(/^find/, 'with'))
- send(scoper) { count = count(count_options) }
+ send(scoper, &counter)
else
- count = count(count_options)
+ with_scope(:find => { :conditions => conditions }, &counter)
end
count.respond_to?(:length) ? count.length : count
@@ -150,7 +152,7 @@ def wp_extract_finder_conditions(finder, arguments, count_options)
attribute_names = extract_attribute_names_from_match(match)
raise "I can't make sense of #{finder}" unless all_attributes_exists?(attribute_names)
- count_options[:conditions] = construct_attributes_from_arguments(attribute_names, arguments)
+ construct_attributes_from_arguments(attribute_names, arguments)
end
end
end
View
6 test/finder_test.rb
@@ -140,6 +140,12 @@ def test_paginate_with_dynamic_finder
assert_equal 8, entries.total_entries
assert_equal entries, Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
+ # dynamic finder + conditions
+ entries = Developer.paginate_by_salary(100000, :page => 1,
+ :conditions => ['id > ?', 6])
+ assert_equal 4, entries.total_entries
+ assert_equal (7..10).to_a, entries.map(&:id)
+
assert_raises RuntimeError do
Developer.paginate_by_inexistent_attribute 100000, :page => 1
end

0 comments on commit 8ff2314

Please sign in to comment.