提交 ad9983f6 编写于 作者: C Carlos Antonio da Silva

Deprecate Relation#sum with a block.

To perform a sum calculation over the array of elements, use to_a.sum(&block).

Please check the discussion in f9cb645d
for more context.
上级 76a6bfd6
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* Deprecate calling `Relation#sum` with a block. To perform a calculation over
the array result of the relation, use `to_a.sum(&block)`.
*Carlos Antonio da Silva*
* Fix postgresql adapter to handle BC timestamps correctly * Fix postgresql adapter to handle BC timestamps correctly
HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years) HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years)
......
...@@ -234,7 +234,7 @@ def association_instance_set(name, association) ...@@ -234,7 +234,7 @@ def association_instance_set(name, association)
# others.size | X | X | X # others.size | X | X | X
# others.length | X | X | X # others.length | X | X | X
# others.count | X | X | X # others.count | X | X | X
# others.sum(args*,&block) | X | X | X # others.sum(*args) | X | X | X
# others.empty? | X | X | X # others.empty? | X | X | X
# others.clear | X | X | X # others.clear | X | X | X
# others.delete(other,other,...) | X | X | X # others.delete(other,other,...) | X | X | X
......
require 'active_support/deprecation'
module ActiveRecord module ActiveRecord
module Calculations module Calculations
# Count the records. # Count the records.
...@@ -51,6 +53,10 @@ def maximum(column_name, options = {}) ...@@ -51,6 +53,10 @@ def maximum(column_name, options = {})
# Person.sum('age') # => 4562 # Person.sum('age') # => 4562
def sum(*args) def sum(*args)
if block_given? if block_given?
ActiveSupport::Deprecation.warn(
"Calling #sum with a block is deprecated and will be removed in Rails 4.1. " \
"If you want to perform sum calculation over the array of elements, use `to_a.sum(&block)`."
)
self.to_a.sum(*args) {|*block_args| yield(*block_args)} self.to_a.sum(*args) {|*block_args| yield(*block_args)}
else else
calculate(:sum, *args) calculate(:sum, *args)
......
...@@ -1658,6 +1658,12 @@ def test_collection_association_with_private_kernel_method ...@@ -1658,6 +1658,12 @@ def test_collection_association_with_private_kernel_method
assert_deprecated { klass.has_many :foo, :counter_sql => 'lol' } assert_deprecated { klass.has_many :foo, :counter_sql => 'lol' }
end end
test "sum calculation with block for array compatibility is deprecated" do
assert_deprecated do
posts(:welcome).comments.sum { |c| c.id }
end
end
test "has many associations on new records use null relations" do test "has many associations on new records use null relations" do
post = Post.new post = Post.new
......
...@@ -389,8 +389,10 @@ def test_sum_with_from_option ...@@ -389,8 +389,10 @@ def test_sum_with_from_option
Account.where("credit_limit > 50").from('accounts').sum(:credit_limit) Account.where("credit_limit > 50").from('accounts').sum(:credit_limit)
end end
def test_sum_array_compatibility def test_sum_array_compatibility_deprecation
assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit) assert_deprecated do
assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit)
end
end end
def test_average_with_from_option def test_average_with_from_option
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册