From 83b1ff13e3a9ad1333081b1dc3a01fcd29d06965 Mon Sep 17 00:00:00 2001 From: Ben Simpson Date: Fri, 30 Dec 2011 15:01:49 -0500 Subject: [PATCH] Allow count, total count to pass parameters to super --- lib/kaminari/models/active_record_relation_methods.rb | 10 +++++----- .../active_record_relation_methods_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/kaminari/models/active_record_relation_methods.rb b/lib/kaminari/models/active_record_relation_methods.rb index aab7445..7ef7bc2 100644 --- a/lib/kaminari/models/active_record_relation_methods.rb +++ b/lib/kaminari/models/active_record_relation_methods.rb @@ -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 diff --git a/spec/models/active_record/active_record_relation_methods_spec.rb b/spec/models/active_record/active_record_relation_methods_spec.rb index a03b042..a519760 100644 --- a/spec/models/active_record/active_record_relation_methods_spec.rb +++ b/spec/models/active_record/active_record_relation_methods_spec.rb @@ -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