From 4368cb9a3766c4fd4b31215cd1670065e641656c Mon Sep 17 00:00:00 2001 From: Irina Dumitrascu Date: Wed, 7 Oct 2009 00:39:57 +0300 Subject: [PATCH] test & patch --- activerecord/lib/active_record/calculations.rb | 1 + activerecord/test/cases/calculations_test.rb | 27 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 646fed1..978ece8 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -250,6 +250,7 @@ module ActiveRecord association = reflect_on_association(group_attr.to_sym) associated = association && association.macro == :belongs_to # only count belongs_to associations group_field = associated ? association.primary_key_name : group_attr + group_field = "#{connection.quote_table_name(table_name)}.#{group_field}" if column_names.include?(group_field.to_s) group_alias = column_alias_for(group_field) group_column = column_for group_field sql = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias)) diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index c2e0276..da5fec5 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -12,7 +12,7 @@ class NumericData < ActiveRecord::Base end class CalculationsTest < ActiveRecord::TestCase - fixtures :companies, :accounts, :topics + fixtures :companies, :accounts, :topics, :edges def test_should_sum_field assert_equal 318, Account.sum(:credit_limit) @@ -178,6 +178,7 @@ class CalculationsTest < ActiveRecord::TestCase column.expects(:name).at_least_once.returns(:firm_id) column.expects(:type_cast).with("ABC").returns("ABC") Account.expects(:columns).at_least_once.returns([column]) + Account.expects(:column_names).at_least_once.returns([:firm_id]) c = Account.count(:all, :group => :firm) first_key = c.keys.first @@ -193,6 +194,30 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 1, c[companies(:first_client)] end + def test_should_calculate_grouped_association_with_self_association_and_count_on_field_name + c = Edge.count( + :source_id, + :group => :source_id, + :joins => 'INNER JOIN (edges AS second) ON (edges.sink_id = second.source_id )' + ) + assert_equal 3, c.length + assert_equal 1, c[1] + end + + def test_should_calculate_grouped_association_with_self_association_and_count_on_full_field_name + c = Edge.count( + "second.source_id", + :group => "second.source_id", + :joins => 'INNER JOIN (edges AS second) ON (edges.sink_id = second.source_id )' + ) + assert_equal 3, c.length + assert_equal 1, c[4] + end + + def test_should_sum_field + assert_equal 318, Account.sum(:credit_limit) + end + def test_should_not_modify_options_when_using_includes options = {:conditions => 'companies.id > 1', :include => :firm} options_copy = options.dup -- 1.6.3.3