Skip to content

Commit

Permalink
Do not allow passing the column name to sum when a block is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jan 17, 2019
1 parent 67356f2 commit 91ddb30
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
10 changes: 4 additions & 6 deletions activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ def maximum(column_name)
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,
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -923,9 +923,9 @@ def test_count_with_block_and_column_name_raises_an_error
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

Expand Down

0 comments on commit 91ddb30

Please sign in to comment.