Skip to content

Commit

Permalink
Merge pull request #14803 from kuldeepaggarwal/null_relation_sum_fix
Browse files Browse the repository at this point in the history
Fixed a problem where `sum` used with a `group` was not returning a Hash.
  • Loading branch information
matthewd committed May 22, 2014
2 parents a0155b2 + 6d36c1d commit 110d3d0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
* When a `group` is set, `sum`, `size`, `average`, `minimum` and `maximum`
on a NullRelation should return a Hash.

*Kuldeep Aggarwal*

* Fixed serialized fields returning serialized data after being updated with
`update_column`.

Expand Down
20 changes: 17 additions & 3 deletions activerecord/lib/active_record/null_relation.rb
Expand Up @@ -23,7 +23,7 @@ def delete(_id_or_array)
end

def size
0
calculate :size, nil
end

def empty?
Expand All @@ -47,14 +47,28 @@ def count(*)
end

def sum(*)
0
calculate :sum, nil
end

def average(*)
calculate :average, nil
end

def minimum(*)
calculate :minimum, nil
end

def maximum(*)
calculate :maximum, nil
end

def calculate(operation, _column_name, _options = {})
# TODO: Remove _options argument as soon we remove support to
# activerecord-deprecated_finders.
if operation == :count
if [:count, :sum, :size].include? operation
group_values.any? ? Hash.new : 0
elsif [:average, :minimum, :maximum].include?(operation) && group_values.any?
Hash.new
else
nil
end
Expand Down
44 changes: 44 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -366,6 +366,14 @@ def test_null_relation_where_values_hash
assert_equal({ 'salary' => 100_000 }, Developer.none.where(salary: 100_000).where_values_hash)
end

def test_null_relation_sum
ac = Aircraft.new
assert_equal Hash.new, ac.engines.group(:id).sum(:id)
assert_equal 0, ac.engines.count
ac.save
assert_equal Hash.new, ac.engines.group(:id).sum(:id)
assert_equal 0, ac.engines.count
end

def test_null_relation_count
ac = Aircraft.new
Expand All @@ -376,6 +384,42 @@ def test_null_relation_count
assert_equal 0, ac.engines.count
end

def test_null_relation_size
ac = Aircraft.new
assert_equal Hash.new, ac.engines.group(:id).size
assert_equal 0, ac.engines.size
ac.save
assert_equal Hash.new, ac.engines.group(:id).size
assert_equal 0, ac.engines.size
end

def test_null_relation_average
ac = Aircraft.new
assert_equal Hash.new, ac.engines.group(:car_id).average(:id)
assert_equal nil, ac.engines.average(:id)
ac.save
assert_equal Hash.new, ac.engines.group(:car_id).average(:id)
assert_equal nil, ac.engines.average(:id)
end

def test_null_relation_minimum
ac = Aircraft.new
assert_equal Hash.new, ac.engines.group(:car_id).minimum(:id)
assert_equal nil, ac.engines.minimum(:id)
ac.save
assert_equal Hash.new, ac.engines.group(:car_id).minimum(:id)
assert_equal nil, ac.engines.minimum(:id)
end

def test_null_relation_maximum
ac = Aircraft.new
assert_equal Hash.new, ac.engines.group(:car_id).maximum(:id)
assert_equal nil, ac.engines.maximum(:id)
ac.save
assert_equal Hash.new, ac.engines.group(:car_id).maximum(:id)
assert_equal nil, ac.engines.maximum(:id)
end

def test_joins_with_nil_argument
assert_nothing_raised { DependentFirm.joins(nil).first }
end
Expand Down

0 comments on commit 110d3d0

Please sign in to comment.