Created
October 4, 2008 19:02
-
-
Save lifo/14793 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb | |
index 634236e..5e33cf1 100644 | |
--- a/activerecord/lib/active_record/calculations.rb | |
+++ b/activerecord/lib/active_record/calculations.rb | |
@@ -285,11 +285,15 @@ module ActiveRecord | |
operation = operation.to_s.downcase | |
case operation | |
when 'count' then value.to_i | |
- when 'sum' then value =~ /\./ ? value.to_f : value.to_i | |
- when 'avg' then value && value.to_f | |
- else column ? column.type_cast(value) : value | |
+ when 'sum' then type_cast_using_column(value || '0', column) | |
+ when 'avg' then value && value.to_d | |
+ else type_cast_using_column(value, column) | |
end | |
end | |
+ | |
+ def type_cast_using_column(value, column) | |
+ column ? column.type_cast(value) : value | |
+ end | |
end | |
end | |
end | |
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb | |
index 754fd58..0fa6150 100644 | |
--- a/activerecord/test/cases/calculations_test.rb | |
+++ b/activerecord/test/cases/calculations_test.rb | |
@@ -18,8 +18,8 @@ class CalculationsTest < ActiveRecord::TestCase | |
def test_should_average_field | |
value = Account.average(:credit_limit) | |
- assert_kind_of Float, value | |
- assert_in_delta 53.0, value, 0.001 | |
+ assert_kind_of BigDecimal, value | |
+ assert_equal BigDecimal.new('53.0'), value | |
end | |
def test_should_return_nil_as_average | |
@@ -273,7 +273,7 @@ class CalculationsTest < ActiveRecord::TestCase | |
end | |
def test_should_sum_expression | |
- assert_equal 636, Account.sum("2 * credit_limit") | |
+ assert_equal '636', Account.sum("2 * credit_limit") | |
end | |
def test_count_with_from_option |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment