Do not allow passing the column name to `sum` when a block is passed

This commit is contained in:
Rafael Mendonça França 2019-01-14 22:19:58 -05:00
parent 67356f2034
commit 91ddb30083
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
3 changed files with 11 additions and 9 deletions

View File

@ -1,3 +1,7 @@
* Do not allow passing the column name to `sum` when a block is passed.
*Rafael Mendonça França*
* Do not allow passing the column name to `count` when a block is passed.
*Rafael Mendonça França*

View File

@ -84,15 +84,13 @@ module ActiveRecord
def sum(column_name = nil)
if block_given?
unless column_name.nil?
ActiveSupport::Deprecation.warn \
"When `sum' is called with a block, it ignores other arguments. " \
"This behavior is now deprecated and will result in an ArgumentError in Rails 6.0."
raise ArgumentError, "Column name argument is not supported when a block is passed."
end
return super()
super()
else
calculate(:sum, column_name)
end
calculate(:sum, column_name)
end
# This calculates aggregate values in the given column. Methods for #count, #sum, #average,

View File

@ -923,9 +923,9 @@ class CalculationsTest < ActiveRecord::TestCase
end
end
def test_deprecate_sum_with_block_and_column_name
assert_deprecated do
assert_equal 6, Account.sum(:firm_id) { 1 }
def test_sum_with_block_and_column_name_raises_an_error
assert_raises(ArgumentError) do
Account.sum(:firm_id) { 1 }
end
end