Skip to content

Commit

Permalink
Revert "MONGOID-5735 Deprecate aggregable API (#5778)" (#5802)
Browse files Browse the repository at this point in the history
This reverts commit 8154e7c.
  • Loading branch information
comandeo-mongo authored Mar 7, 2024
1 parent ea01485 commit 39802ca
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 39 deletions.
9 changes: 0 additions & 9 deletions docs/release-notes/mongoid-9.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ inferred from the model name, you can specify ``--collection``:
For more information see the `GitHub Repository <https://github.com/mongodb/mongoid-railsmdb>`_.


Aggregable API is deprecated
----------------------------

Mongoid 9 deprecated aggregable API in modules
``Mongoid::Contextual::Aggregable::Memory``,
``Mongoid::Contextual::Aggregable::Mongo``, and
``Mongoid::Contextual::Aggregable::None``.


Support for Ruby 2.6 and JRuby 9.3 Dropped
-------------------------------------------

Expand Down
10 changes: 0 additions & 10 deletions lib/mongoid/contextual/aggregable/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ module Memory
# @return [ Hash ] A Hash containing the aggregate values.
# If no documents are present, then returned Hash will have
# count, sum of 0 and max, min, avg of nil.
# @deprecated
def aggregates(field)
%w(count sum avg min max).each_with_object({}) do |method, hash|
hash[method] = send(method, field)
end
end
Mongoid.deprecate(self, :aggregates)

# Get the average value of the provided field.
#
Expand All @@ -31,15 +29,13 @@ def aggregates(field)
# @param [ Symbol ] field The field to average.
#
# @return [ Numeric ] The average.
# @deprecated
def avg(field)
total = count { |doc| !doc.send(field).nil? }
return nil unless total > 0

total = total.to_f if total.is_a?(Integer)
sum(field) / total
end
Mongoid.deprecate(self, :avg)

# Get the max value of the provided field. If provided a block, will
# return the Document with the greatest value for the field, in
Expand All @@ -57,13 +53,11 @@ def avg(field)
#
# @return [ Numeric | Document ] The max value or document with the max
# value.
# @deprecated
def max(field = nil)
return super() if block_given?

aggregate_by(field, :max)
end
Mongoid.deprecate(self, :max)

# Get the min value of the provided field. If provided a block, will
# return the Document with the smallest value for the field, in
Expand All @@ -81,13 +75,11 @@ def max(field = nil)
#
# @return [ Numeric | Document ] The min value or document with the min
# value.
# @deprecated
def min(field = nil)
return super() if block_given?

aggregate_by(field, :min)
end
Mongoid.deprecate(self, :min)

# Get the sum value of the provided field. If provided a block, will
# return the sum in accordance with Ruby's enumerable API.
Expand All @@ -101,13 +93,11 @@ def min(field = nil)
# @param [ Symbol ] field The field to sum.
#
# @return [ Numeric ] The sum value.
# @deprecated
def sum(field = nil)
return super() if block_given?

aggregate_by(field, :sum) || 0
end
Mongoid.deprecate(self, :sum)

private

Expand Down
10 changes: 0 additions & 10 deletions lib/mongoid/contextual/aggregable/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ module Mongo
# @return [ Hash ] A Hash containing the aggregate values.
# If no documents are found, then returned Hash will have
# count, sum of 0 and max, min, avg of nil.
# @deprecated
def aggregates(field)
result = collection.aggregate(pipeline(field), session: _session).to_a
if result.empty?
Expand All @@ -35,7 +34,6 @@ def aggregates(field)
result.first
end
end
Mongoid.deprecate(self, :aggregates)

# Get the average value of the provided field.
#
Expand All @@ -45,11 +43,9 @@ def aggregates(field)
# @param [ Symbol ] field The field to average.
#
# @return [ Float ] The average.
# @deprecated
def avg(field)
aggregates(field)["avg"]
end
Mongoid.deprecate(self, :avg)

# Get the max value of the provided field. If provided a block, will
# return the Document with the greatest value for the field, in
Expand All @@ -67,11 +63,9 @@ def avg(field)
#
# @return [ Float | Document ] The max value or document with the max
# value.
# @deprecated
def max(field = nil)
block_given? ? super() : aggregates(field)["max"]
end
Mongoid.deprecate(self, :max)

# Get the min value of the provided field. If provided a block, will
# return the Document with the smallest value for the field, in
Expand All @@ -89,11 +83,9 @@ def max(field = nil)
#
# @return [ Float | Document ] The min value or document with the min
# value.
# @deprecated
def min(field = nil)
block_given? ? super() : aggregates(field)["min"]
end
Mongoid.deprecate(self, :min)

# Get the sum value of the provided field. If provided a block, will
# return the sum in accordance with Ruby's enumerable API.
Expand All @@ -107,11 +99,9 @@ def min(field = nil)
# @param [ Symbol ] field The field to sum.
#
# @return [ Float ] The sum value.
# @deprecated
def sum(field = nil)
block_given? ? super() : aggregates(field)["sum"] || 0
end
Mongoid.deprecate(self, :sum)

private

Expand Down
10 changes: 0 additions & 10 deletions lib/mongoid/contextual/aggregable/none.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ module None
# @param [ String | Symbol ] _field The field name.
#
# @return [ Hash ] A Hash with count, sum of 0 and max, min, avg of nil.
# @deprecated
def aggregates(_field)
Aggregable::EMPTY_RESULT.dup
end
Mongoid.deprecate(self, :aggregates)

# Always returns zero.
#
Expand All @@ -28,11 +26,9 @@ def aggregates(_field)
# @param [ Symbol ] _field The field to sum.
#
# @return [ Integer ] Always zero.
# @deprecated
def sum(_field = nil)
0
end
Mongoid.deprecate(self, :sum)

# Always returns nil.
#
Expand All @@ -41,11 +37,9 @@ def sum(_field = nil)
# @param [ Symbol ] _field The field to avg.
#
# @return [ nil ] Always nil.
# @deprecated
def avg(_field)
nil
end
Mongoid.deprecate(self, :avg)

# Always returns nil.
#
Expand All @@ -54,11 +48,9 @@ def avg(_field)
# @param [ Symbol ] _field The field to min.
#
# @return [ nil ] Always nil.
# @deprecated
def min(_field = nil)
nil
end
Mongoid.deprecate(self, :min)

# Always returns nil.
#
Expand All @@ -67,9 +59,7 @@ def min(_field = nil)
# @param [ Symbol ] _field The field to max.
#
# @return [ nil ] Always nil.
# @deprecated
alias :max :min
Mongoid.deprecate(self, :max)
end
end
end
Expand Down

0 comments on commit 39802ca

Please sign in to comment.