diff options
author | Rick Olson <technoweenie@gmail.com> | 2007-07-17 20:16:35 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2007-07-17 20:16:35 +0000 |
commit | a41305640722f1328ff51ce61c98371d4b648213 (patch) | |
tree | 8a3f8e482a2cc72b17d0937833a9803255e9343d | |
parent | b549366216f6cb607bf06c19701912a2e0d1f77c (diff) | |
download | rails-a41305640722f1328ff51ce61c98371d4b648213.tar.gz rails-a41305640722f1328ff51ce61c98371d4b648213.tar.bz2 rails-a41305640722f1328ff51ce61c98371d4b648213.zip |
Fix and properly document/test count(column_name) usage. Closes #8999 [lifofifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 22 | ||||
-rw-r--r-- | activerecord/test/calculations_test.rb | 12 |
3 files changed, 26 insertions, 10 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 3d6413a427..7a32ac5e44 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix and properly document/test count(column_name) usage. Closes #8999 [lifofifo] + * Remove deprecated count(conditions=nil, joins=nil) usage. Closes #8993 [lifofifo] * Change belongs_to so that the foreign_key assumption is taken from the association name, not the class name. Closes #8992 [hasmanyjosh] diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 0a71c92c01..b2d6cef3bc 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -6,12 +6,13 @@ module ActiveRecord end module ClassMethods - # Count operates using two different approaches. + # Count operates using three different approaches. # # * Count all: By not passing any parameters to count, it will return a count of all the rows for the model. + # * Count using column : By passing a column name to count, it will return a count of all the rows for the model with supplied column present # * Count using options will find the row count matched by the options used. # - # The second approach, count using options, accepts an option hash as the only parameter. The options are: + # The third approach, count using options, accepts an option hash as the only parameter. The options are: # # * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro. # * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed). @@ -28,6 +29,9 @@ module ActiveRecord # Examples for counting all: # Person.count # returns the total count of all people # + # Examples for counting by column: + # Person.count(:age) # returns the total count of all people whose age is present in database + # # Examples for count with options: # Person.count(:conditions => "age > 26") # Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job) # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN. @@ -123,19 +127,21 @@ module ActiveRecord def construct_count_options_from_args(*args) options = {} column_name = :all - + # We need to handle # count() + # count(:column_name=:all) # count(options={}) # count(column_name=:all, options={}) - if args[0].is_a?(Hash) - options = args[0] - elsif args[1].is_a?(Hash) + case args.size + when 1 + args[0].is_a?(Hash) ? options = args[0] : column_name = args[0] + when 2 column_name, options = args else - raise ArgumentError, "Unexpected parameters passed to count(options={}): #{args.inspect}" + raise ArgumentError, "Unexpected parameters passed to count(): #{args.inspect}" end if args.size > 0 - + [column_name, options] end diff --git a/activerecord/test/calculations_test.rb b/activerecord/test/calculations_test.rb index faa86c43f2..298a1d1878 100644 --- a/activerecord/test/calculations_test.rb +++ b/activerecord/test/calculations_test.rb @@ -227,12 +227,20 @@ class CalculationsTest < Test::Unit::TestCase assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) } assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) } end - + def test_should_count_selected_field_with_include assert_equal 6, Account.count(:distinct => true, :include => :firm) assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) end - + + def test_count_with_column_parameter + assert_equal 5, Account.count(:firm_id) + end + + def test_count_with_column_and_options_parameter + assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50") + end + def test_count_with_no_parameters_isnt_deprecated assert_not_deprecated { Account.count } end |