Permalink
Browse files

Active Record: fix `size` method for grouped relations

  • Loading branch information...
mislav committed Sep 27, 2011
1 parent 9bd5aec commit 465eda2fd118e66995ffb3b8dd0b01d8d7579a9b
Showing with 11 additions and 7 deletions.
  1. +4 −7 lib/will_paginate/active_record.rb
  2. +7 −0 spec/finders/active_record_spec.rb
@@ -73,7 +73,7 @@ def count
# workaround for Active Record 3.0
def size
- if !loaded? and limit_value
+ if !loaded? and limit_value and group_values.empty?
[super, limit_value].min
else
super
@@ -83,12 +83,9 @@ def size
# overloaded to be pagination-aware
def empty?
if !loaded? and offset_value
- rel_count = count
- if rel_count.respond_to?(:size) and !rel_count.is_a?(Integer)
- rel_count.size <= offset_value
- else
- rel_count <= offset_value
- end
+ result = count
+ result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
+ result <= offset_value
else
super
end
@@ -146,6 +146,13 @@
}.should run_queries(1)
end
+ it "supports `size` for grouped queries" do
+ topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3
+ lambda {
+ topics.size.should == {nil=>2, 1=>2}
+ }.should run_queries(1)
+ end
+
it "overrides total_entries count with a fixed value" do
lambda {
topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999

0 comments on commit 465eda2

Please sign in to comment.