Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
  2. index 634236e..5e33cf1 100644
  3. --- a/activerecord/lib/active_record/calculations.rb
  4. +++ b/activerecord/lib/active_record/calculations.rb
  5. @@ -285,11 +285,15 @@ module ActiveRecord
  6. operation = operation.to_s.downcase
  7. case operation
  8. when 'count' then value.to_i
  9. - when 'sum' then value =~ /\./ ? value.to_f : value.to_i
  10. - when 'avg' then value && value.to_f
  11. - else column ? column.type_cast(value) : value
  12. + when 'sum' then type_cast_using_column(value || '0', column)
  13. + when 'avg' then value && value.to_d
  14. + else type_cast_using_column(value, column)
  15. end
  16. end
  17. +
  18. + def type_cast_using_column(value, column)
  19. + column ? column.type_cast(value) : value
  20. + end
  21. end
  22. end
  23. end
  24. diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
  25. index 754fd58..0fa6150 100644
  26. --- a/activerecord/test/cases/calculations_test.rb
  27. +++ b/activerecord/test/cases/calculations_test.rb
  28. @@ -18,8 +18,8 @@ class CalculationsTest < ActiveRecord::TestCase
  29.  
  30. def test_should_average_field
  31. value = Account.average(:credit_limit)
  32. - assert_kind_of Float, value
  33. - assert_in_delta 53.0, value, 0.001
  34. + assert_kind_of BigDecimal, value
  35. + assert_equal BigDecimal.new('53.0'), value
  36. end
  37.  
  38. def test_should_return_nil_as_average
  39. @@ -273,7 +273,7 @@ class CalculationsTest < ActiveRecord::TestCase
  40. end
  41.  
  42. def test_should_sum_expression
  43. - assert_equal 636, Account.sum("2 * credit_limit")
  44. + assert_equal '636', Account.sum("2 * credit_limit")
  45. end
  46.  
  47. def test_count_with_from_option
Add Comment
Please, Sign In to add comment