Skip to content
Browse files

Fix the HasManyAssociation#count method so it uses the new ActiveReco…

…rd::Base#count syntax, while maintaining backwards compatibility. [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4237 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9d8e348 commit df7b74668ebc0ae9978d4c27125db7fec730b110 @technoweenie technoweenie committed Apr 19, 2006
View
11 activerecord/lib/active_record/associations/has_many_association.rb
@@ -36,15 +36,18 @@ def find_first(conditions = nil, orderings = nil)
end
# Count the number of associated records. All arguments are optional.
- def count(runtime_conditions = nil)
+ def count(*args)
if @reflection.options[:counter_sql]
@reflection.klass.count_by_sql(@counter_sql)
elsif @reflection.options[:finder_sql]
@reflection.klass.count_by_sql(@finder_sql)
else
- sql = @finder_sql
- sql += " AND (#{sanitize_sql(runtime_conditions)})" if runtime_conditions
- @reflection.klass.count(sql)
+ column_name, options = @reflection.klass.send(:construct_count_options_from_legacy_args, *args)
+ options[:conditions] = options[:conditions].nil? ?
+ @finder_sql :
+ @finder_sql + " AND (#{sanitize_sql(options[:conditions])})"
+
+ @reflection.klass.count(column_name, options)
end
end
View
39 activerecord/lib/active_record/calculations.rb
@@ -42,23 +42,7 @@ module ClassMethods
#
# Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead.
def count(*args)
- options = {}
- column_name = :all
- # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}).
- if args.size >= 0 && args.size <= 2
- if args.first.is_a?(Hash)
- options = args.first
- elsif args[1].is_a?(Hash)
- options = args[1]
- column_name = args.first
- else
- # Handle legacy paramter options: def count(conditions=nil, joins=nil)
- options.merge!(:conditions => args[0]) if args.length > 0
- options.merge!(:joins => args[1]) if args.length > 1
- end
- else
- raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})")
- end
+ column_name, options = construct_count_options_from_legacy_args(*args)
if options[:include] || scope(:find, :include)
count_with_associations(options)
@@ -146,6 +130,27 @@ def calculate(operation, column_name, options = {})
end
protected
+ def construct_count_options_from_legacy_args(*args)
+ options = {}
+ column_name = :all
+ # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}).
+ if args.size >= 0 && args.size <= 2
+ if args.first.is_a?(Hash)
+ options = args.first
+ elsif args[1].is_a?(Hash)
+ options = args[1]
+ column_name = args.first
+ else
+ # Handle legacy paramter options: def count(conditions=nil, joins=nil)
+ options.merge!(:conditions => args[0]) if args.length > 0
+ options.merge!(:joins => args[1]) if args.length > 1
+ end
+ else
+ raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})")
+ end
+ [column_name, options]
+ end
+
def construct_calculation_sql(aggregate, aggregate_alias, options) #:nodoc:
scope = scope(:find)
sql = "SELECT #{aggregate} AS #{aggregate_alias}"
View
20 activerecord/test/associations_test.rb
@@ -283,10 +283,26 @@ def force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.each {|f| }
end
- def test_counting
+ def test_counting_with_counter_sql
assert_equal 2, Firm.find(:first).clients.count
end
-
+
+ def test_counting
+ assert_equal 2, Firm.find(:first).plain_clients.count
+ end
+
+ def test_counting_with_single_conditions
+ assert_equal 2, Firm.find(:first).plain_clients.count('1=1')
+ end
+
+ def test_counting_with_single_hash
+ assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => '1=1')
+ end
+
+ def test_counting_with_column_name_and_hash
+ assert_equal 2, Firm.find(:first).plain_clients.count(:all, :conditions => '1=1')
+ end
+
def test_finding
assert_equal 2, Firm.find(:first).clients.length
end
View
1 activerecord/test/fixtures/company.rb
@@ -28,6 +28,7 @@ class Firm < Company
has_many :no_clients_using_counter_sql, :class_name => "Client",
:finder_sql => 'SELECT * FROM companies WHERE client_of = 1000',
:counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
+ has_many :plain_clients, :class_name => 'Client'
has_one :account, :foreign_key => "firm_id", :dependent => :destroy
end
View
4 activerecord/test/reflection_test.rb
@@ -137,8 +137,8 @@ def test_association_reflection_in_modules
end
def test_reflection_of_all_associations
- assert_equal 13, Firm.reflect_on_all_associations.size
- assert_equal 11, Firm.reflect_on_all_associations(:has_many).size
+ assert_equal 14, Firm.reflect_on_all_associations.size
+ assert_equal 12, Firm.reflect_on_all_associations(:has_many).size
assert_equal 2, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end

0 comments on commit df7b746

Please sign in to comment.
Something went wrong with that request. Please try again.