Skip to content

Commit

Permalink
Active Record: fix size method for grouped relations
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Sep 27, 2011
1 parent 9bd5aec commit 465eda2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/will_paginate/active_record.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/finders/active_record_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 465eda2

Please sign in to comment.