1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Allow count, total count to pass parameters to super

This commit is contained in:
Ben Simpson 2011-12-30 15:01:49 -05:00 committed by Ben Simpson
parent b4b1e578ad
commit 83b1ff13e3
2 changed files with 15 additions and 5 deletions

View file

@ -3,12 +3,12 @@ module Kaminari
# a workaround for AR 3.0.x that returns 0 for #count when page > 1
# if +limit_value+ is specified, load all the records and count them
if ActiveRecord::VERSION::STRING < '3.1'
def count #:nodoc:
limit_value ? length : super
def count(column_name = nil, options = {}) #:nodoc:
limit_value ? length : super(column_name, options)
end
end
def total_count #:nodoc:
def total_count(column_name = nil, options = {}) #:nodoc:
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
@total_count ||= begin
c = except(:offset, :limit, :order)
@ -22,8 +22,8 @@ module Kaminari
c.length
else
# .group returns an OrderdHash that responds to #count
c = c.count
c.respond_to?(:count) ? c.count : c
c = c.count(column_name, options)
c.respond_to?(:count) ? c.count(column_name, options) : c
end
end
end

View file

@ -25,6 +25,16 @@ if defined? ActiveRecord
User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
end
end
context "when total_count receives options" do
it "should return a distinct total count" do
User.page(1).total_count(:name, distinct: true).should == 4
end
end
context "when count receives options" do
it "should return a distinct set by column" do
User.page(1).count(:name, distinct: true).should == 4
end
end
end
end
end