Skip to content

Commit

Permalink
Merge pull request #372 from edariedl/fix_count_with_select_statement
Browse files Browse the repository at this point in the history
Fix count method to work with select statement.
  • Loading branch information
mislav committed Jun 19, 2014
2 parents bf331ce + 2c92c69 commit 813722f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/will_paginate/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def count(*args)
rel = self.except(*excluded)
# TODO: hack. decide whether to keep
rel = rel.apply_finder_options(@wp_count_options) if defined? @wp_count_options
rel.count(*args)

column_name = (select_for_count(rel) || :all)
rel.count(column_name)
else
super(*args)
end
Expand Down Expand Up @@ -143,6 +145,13 @@ def copy_will_paginate_data(other)
other.wp_count_options = @wp_count_options if defined? @wp_count_options
other
end

def select_for_count(rel)
if rel.select_values.present?
select = rel.select_values.join(", ")
select if select !~ /[,*]/
end
end
end

module Pagination
Expand Down
4 changes: 4 additions & 0 deletions spec/finders/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@
it "should count with group" do
Developer.group(:salary).page(1).total_entries.should == 4
end

it "should count with select" do
Topic.select('title, content').page(1).total_entries.should == 4
end

it "removes :reorder for count with group" do
Project.group(:id).reorder(:id).page(1).total_entries
Expand Down

0 comments on commit 813722f

Please sign in to comment.