Skip to content

Commit 76a6bfd

Browse files
Revert "Yield only one argument instead of splatting."
This reverts commit f9cb645. Conflicts: activerecord/CHANGELOG.md Revert "Allow blocks for count with ActiveRecord::Relation. Document and test that sum allows blocks" This reverts commit 9cc2bf6. Conflicts: activerecord/lib/active_record/relation/calculations.rb
1 parent ae934ae commit 76a6bfd

File tree

3 files changed

+3
-37
lines changed

3 files changed

+3
-37
lines changed

activerecord/CHANGELOG.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,6 @@
736736

737737
*Marc-André Lafortune*
738738

739-
* Allow blocks for `count` with `ActiveRecord::Relation`, to work similar as
740-
`Array#count`:
741-
742-
Person.where("age > 26").count { |person| person.gender == 'female' }
743-
744-
*Chris Finne & Carlos Antonio da Silva*
745-
746739
* Added support to `CollectionAssociation#delete` for passing `fixnum`
747740
or `string` values as record ids. This finds the records responding
748741
to the `id` and executes delete on them.

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,9 @@ module Calculations
1313
#
1414
# Person.count(:age, distinct: true)
1515
# # => counts the number of different age values
16-
#
17-
# Person.where("age > 26").count { |person| person.gender == 'female' }
18-
# # => queries people where "age > 26" then count the loaded results filtering by gender
1916
def count(column_name = nil, options = {})
20-
if block_given?
21-
self.to_a.count { |item| yield item }
22-
else
23-
column_name, options = nil, column_name if column_name.is_a?(Hash)
24-
calculate(:count, column_name, options)
25-
end
17+
column_name, options = nil, column_name if column_name.is_a?(Hash)
18+
calculate(:count, column_name, options)
2619
end
2720

2821
# Calculates the average value on a given column. Returns +nil+ if there's
@@ -56,13 +49,9 @@ def maximum(column_name, options = {})
5649
# +calculate+ for examples with options.
5750
#
5851
# Person.sum('age') # => 4562
59-
# # => returns the total sum of all people's age
60-
#
61-
# Person.where('age > 100').sum { |person| person.age - 100 }
62-
# # queries people where "age > 100" then perform a sum calculation with the block returns
6352
def sum(*args)
6453
if block_given?
65-
self.to_a.sum(*args) { |item| yield item }
54+
self.to_a.sum(*args) {|*block_args| yield(*block_args)}
6655
else
6756
calculate(:sum, *args)
6857
end

activerecord/test/cases/calculations_test.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -383,22 +383,6 @@ def test_count_with_from_option
383383
Company.where(:type => "Firm").from('companies').count(:type)
384384
end
385385

386-
def test_count_with_block_acts_as_array
387-
accounts = Account.where('id > 0')
388-
assert_equal Account.count, accounts.count { true }
389-
assert_equal 0, accounts.count { false }
390-
assert_equal Account.where('credit_limit > 50').size, accounts.count { |account| account.credit_limit > 50 }
391-
assert_equal Account.count, Account.count { true }
392-
assert_equal 0, Account.count { false }
393-
end
394-
395-
def test_sum_with_block_acts_as_array
396-
accounts = Account.where('id > 0')
397-
assert_equal Account.sum(:credit_limit), accounts.sum { |account| account.credit_limit }
398-
assert_equal Account.sum(:credit_limit) + Account.count, accounts.sum{ |account| account.credit_limit + 1 }
399-
assert_equal 0, accounts.sum { |account| 0 }
400-
end
401-
402386
def test_sum_with_from_option
403387
assert_equal Account.sum(:credit_limit), Account.from('accounts').sum(:credit_limit)
404388
assert_equal Account.where("credit_limit > 50").sum(:credit_limit),

0 commit comments

Comments
 (0)