From 7c1b14f04158102f68610128ae230941e85263a3 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 16 Aug 2016 05:33:22 +0900 Subject: Refactor `NullRelation#calculate` Before: ```ruby def calculate(operation, _column_name) if [:count, :sum].include? operation group_values.any? ? Hash.new : 0 elsif [:average, :minimum, :maximum].include?(operation) && group_values.any? Hash.new else nil end end ``` After: ```ruby def calculate(operation, _column_name) case operation when :count, :sum group_values.any? ? Hash.new : 0 when :average, :minimum, :maximum group_values.any? ? Hash.new : nil end end ``` --- activerecord/lib/active_record/null_relation.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/null_relation.rb b/activerecord/lib/active_record/null_relation.rb index 254550c378..2bb7ed6d5e 100644 --- a/activerecord/lib/active_record/null_relation.rb +++ b/activerecord/lib/active_record/null_relation.rb @@ -41,12 +41,11 @@ module ActiveRecord end def calculate(operation, _column_name) - if [:count, :sum].include? operation + case operation + when :count, :sum group_values.any? ? Hash.new : 0 - elsif [:average, :minimum, :maximum].include?(operation) && group_values.any? - Hash.new - else - nil + when :average, :minimum, :maximum + group_values.any? ? Hash.new : nil end end -- cgit v1.2.3