lib/mongoid/contextual/aggregable/mongo.rb in mongoid-9.0.0 vs lib/mongoid/contextual/aggregable/mongo.rb in mongoid-9.0.1
- old
+ new
@@ -94,14 +94,17 @@
# aggregable.sum(:likes)
#
# @example Get the sum for the provided block.
# aggregable.sum(&:likes)
#
- # @param [ Symbol ] field The field to sum.
+ # @param [ Symbol | Numeric ] field The field to sum, or the initial
+ # value of the sum when a block is given.
#
# @return [ Float ] The sum value.
def sum(field = nil)
- block_given? ? super() : aggregates(field)["sum"] || 0
+ return super(field || 0) if block_given?
+
+ aggregates(field)["sum"] || 0
end
private
# Get the aggregation pipeline for provided field.