Skip to content

Commit 5aea01a

Browse files
committed
extract deprecated #calculate code
1 parent d961f49 commit 5aea01a

File tree

1 file changed

+25
-83
lines changed

1 file changed

+25
-83
lines changed

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 25 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,19 @@
33

44
module ActiveRecord
55
module Calculations
6-
# Count operates using three different approaches.
6+
# Count the records.
77
#
8-
# * Count all: By not passing any parameters to count, it will return a count of all the rows for the model.
9-
# * Count using column: By passing a column name to count, it will return a count of all the
10-
# rows for the model with supplied column present.
11-
# * Count using options will find the row count matched by the options used.
8+
# Person.count
9+
# # => the total count of all people
1210
#
13-
# The third approach, count using options, accepts an option hash as the only parameter. The options are:
11+
# Person.count(:age)
12+
# # => returns the total count of all people whose age is present in database
1413
#
15-
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ].
16-
# See conditions in the intro to ActiveRecord::Base.
17-
# * <tt>:joins</tt>: Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id"
18-
# (rarely needed) or named associations in the same form used for the <tt>:include</tt> option, which will
19-
# perform an INNER JOIN on the associated table(s). If the value is a string, then the records
20-
# will be returned read-only since they will have attributes that do not correspond to the table's columns.
21-
# Pass <tt>:readonly => false</tt> to override.
22-
# * <tt>:include</tt>: Named associations that should be loaded alongside using LEFT OUTER JOINs.
23-
# The symbols named refer to already defined associations. When using named associations, count
24-
# returns the number of DISTINCT items for the model you're counting.
25-
# See eager loading under Associations.
26-
# * <tt>:order</tt>: An SQL fragment like "created_at DESC, name" (really only used with GROUP BY calculations).
27-
# * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
28-
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you, for example,
29-
# want to do a join but not include the joined columns.
30-
# * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as
31-
# SELECT COUNT(DISTINCT posts.id) ...
32-
# * <tt>:from</tt> - By default, this is the table name of the class, but can be changed to an
33-
# alternate table name (or even the name of a database view).
14+
# Person.count(:all)
15+
# # => performs a COUNT(*) (:all is an alias for '*')
3416
#
35-
# Examples for counting all:
36-
# Person.count # returns the total count of all people
37-
#
38-
# Examples for counting by column:
39-
# Person.count(:age) # returns the total count of all people whose age is present in database
40-
#
41-
# Examples for count with options:
42-
# Person.count(:conditions => "age > 26")
43-
#
44-
# # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN.
45-
# Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job)
46-
#
47-
# # finds the number of rows matching the conditions and joins.
48-
# Person.count(:conditions => "age > 26 AND job.salary > 60000",
49-
# :joins => "LEFT JOIN jobs on jobs.person_id = person.id")
50-
#
51-
# Person.count('id', :conditions => "age > 26") # Performs a COUNT(id)
52-
# Person.count(:all, :conditions => "age > 26") # Performs a COUNT(*) (:all is an alias for '*')
53-
#
54-
# Note: <tt>Person.count(:all)</tt> will not work because it will use <tt>:all</tt> as the condition.
55-
# Use Person.count instead.
17+
# Person.count(:age, distinct: true)
18+
# # => counts the number of different age values
5619
def count(column_name = nil, options = {})
5720
column_name, options = nil, column_name if column_name.is_a?(Hash)
5821
calculate(:count, column_name, options)
@@ -98,69 +61,48 @@ def sum(*args)
9861
end
9962

10063
# This calculates aggregate values in the given column. Methods for count, sum, average,
101-
# minimum, and maximum have been added as shortcuts. Options such as <tt>:conditions</tt>,
102-
# <tt>:order</tt>, <tt>:group</tt>, <tt>:having</tt>, and <tt>:joins</tt> can be passed to customize the query.
64+
# minimum, and maximum have been added as shortcuts.
10365
#
10466
# There are two basic forms of output:
67+
#
10568
# * Single aggregate value: The single value is type cast to Fixnum for COUNT, Float
10669
# for AVG, and the given column's type for everything else.
107-
# * Grouped values: This returns an ordered hash of the values and groups them by the
108-
# <tt>:group</tt> option. It takes either a column name, or the name of a belongs_to association.
10970
#
110-
# values = Person.maximum(:age, :group => 'last_name')
71+
# * Grouped values: This returns an ordered hash of the values and groups them. It
72+
# takes either a column name, or the name of a belongs_to association.
73+
#
74+
# values = Person.group('last_name').maximum(:age)
11175
# puts values["Drake"]
11276
# => 43
11377
#
11478
# drake = Family.find_by_last_name('Drake')
115-
# values = Person.maximum(:age, :group => :family) # Person belongs_to :family
79+
# values = Person.group(:family).maximum(:age) # Person belongs_to :family
11680
# puts values[drake]
11781
# => 43
11882
#
11983
# values.each do |family, max_age|
12084
# ...
12185
# end
12286
#
123-
# Options:
124-
# * <tt>:conditions</tt> - An SQL fragment like "administrator = 1" or [ "user_name = ?", username ].
125-
# See conditions in the intro to ActiveRecord::Base.
126-
# * <tt>:include</tt>: Eager loading, see Associations for details. Since calculations don't load anything,
127-
# the purpose of this is to access fields on joined tables in your conditions, order, or group clauses.
128-
# * <tt>:joins</tt> - An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id".
129-
# (Rarely needed).
130-
# The records will be returned read-only since they will have attributes that do not correspond to the
131-
# table's columns.
132-
# * <tt>:order</tt> - An SQL fragment like "created_at DESC, name" (really only used with GROUP BY calculations).
133-
# * <tt>:group</tt> - An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
134-
# * <tt>:select</tt> - By default, this is * as in SELECT * FROM, but can be changed if you for example
135-
# want to do a join, but not include the joined columns.
136-
# * <tt>:distinct</tt> - Set this to true to make this a distinct calculation, such as
137-
# SELECT COUNT(DISTINCT posts.id) ...
138-
#
13987
# Examples:
14088
# Person.calculate(:count, :all) # The same as Person.count
14189
# Person.average(:age) # SELECT AVG(age) FROM people...
142-
# Person.minimum(:age, :conditions => ['last_name != ?', 'Drake']) # Selects the minimum age for
143-
# # everyone with a last name other than 'Drake'
14490
#
14591
# # Selects the minimum age for any family without any minors
146-
# Person.minimum(:age, :having => 'min(age) > 17', :group => :last_name)
92+
# Person.group(:last_name).having("min(age) > 17").minimum(:age)
14793
#
14894
# Person.sum("2 * age")
14995
def calculate(operation, column_name, options = {})
150-
if options.except(:distinct).present?
151-
apply_finder_options(options.except(:distinct)).calculate(operation, column_name, :distinct => options[:distinct])
152-
else
153-
relation = with_default_scope
154-
155-
if relation.equal?(self)
156-
if eager_loading? || (includes_values.present? && references_eager_loaded_tables?)
157-
construct_relation_for_association_calculations.calculate(operation, column_name, options)
158-
else
159-
perform_calculation(operation, column_name, options)
160-
end
96+
relation = with_default_scope
97+
98+
if relation.equal?(self)
99+
if eager_loading? || (includes_values.present? && references_eager_loaded_tables?)
100+
construct_relation_for_association_calculations.calculate(operation, column_name, options)
161101
else
162-
relation.calculate(operation, column_name, options)
102+
perform_calculation(operation, column_name, options)
163103
end
104+
else
105+
relation.calculate(operation, column_name, options)
164106
end
165107
rescue ThrowResult
166108
0

0 commit comments

Comments
 (0)