Skip to content

Commit

Permalink
Merge pull request #29262 from kamipo/deprecate_passing_arguments_wit…
Browse files Browse the repository at this point in the history
…h_block_to_count_and_sum

Deprecate passing arguments and block at the same time to `count` and `sum` in `ActiveRecord::Calculations`
  • Loading branch information
guilleiguaran committed May 29, 2017
2 parents acdf234 + 36417cf commit 109bca9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Deprecate passing arguments and block at the same time to `count` and `sum` in `ActiveRecord::Calculations`.

*Ryuta Kamizono*

* Loading model schema from database is now thread-safe.

Fixes #28589.
Expand Down
22 changes: 20 additions & 2 deletions activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ module Calculations
# Note: not all valid {Relation#select}[rdoc-ref:QueryMethods#select] expressions are valid #count expressions. The specifics differ
# between databases. In invalid cases, an error from the database is thrown.
def count(column_name = nil)
return super() if block_given?
if block_given?
unless column_name.nil?
ActiveSupport::Deprecation.warn \
"When `count' is called with a block, it ignores other arguments. " \
"This behavior is now deprecated and will result in an ArgumentError in Rails 5.3."
end

return super()
end

calculate(:count, column_name)
end

Expand Down Expand Up @@ -73,7 +82,16 @@ def maximum(column_name)
#
# Person.sum(:age) # => 4562
def sum(column_name = nil)
return super() if block_given?
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 5.3."
end

return super()
end

calculate(:sum, column_name)
end

Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,16 @@ def permit!
def test_group_by_attribute_with_custom_type
assert_equal({ "proposed" => 2, "published" => 2 }, Book.group(:status).count)
end

def test_deprecate_count_with_block_and_column_name
assert_deprecated do
assert_equal 6, Account.count(:firm_id) { true }
end
end

def test_deprecate_sum_with_block_and_column_name
assert_deprecated do
assert_equal 6, Account.sum(:firm_id) { 1 }
end
end
end

0 comments on commit 109bca9

Please sign in to comment.