mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #14803 from kuldeepaggarwal/null_relation_sum_fix
Fixed a problem where `sum` used with a `group` was not returning a Hash.
This commit is contained in:
commit
110d3d0c0b
3 changed files with 66 additions and 3 deletions
|
@ -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`.
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def size
|
||||
0
|
||||
calculate :size, nil
|
||||
end
|
||||
|
||||
def empty?
|
||||
|
@ -47,14 +47,28 @@ module ActiveRecord
|
|||
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
|
||||
|
|
|
@ -366,6 +366,14 @@ class RelationTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
@ -376,6 +384,42 @@ class RelationTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue