Skip to content
Browse files

Refactoring: Calculations now use construct_finder_sql instead they o…

…wn method.
  • Loading branch information...
1 parent b9088dc commit 3b27a4856f9a1a4d8f4e6d80343fcd5a80c40b35 @miloops miloops committed Jun 23, 2009
Showing with 8 additions and 25 deletions.
  1. +4 −6 activerecord/lib/active_record/base.rb
  2. +4 −19 activerecord/lib/active_record/calculations.rb
View
10 activerecord/lib/active_record/base.rb
@@ -1696,11 +1696,9 @@ def arel_table(table = table_name)
@arel_table = Arel::Table.new(table)
end
- def construct_finder_arel(options)
- scope = scope(:find)
-
+ def construct_finder_arel(options, scope = scope(:find))
# TODO add lock to Arel
- arel_table(table_name).
+ arel_table(options[:from] || table_name).
join(construct_join(options[:joins], scope)).
where(construct_conditions(options[:conditions], scope)).
project(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))).
@@ -1711,8 +1709,8 @@ def construct_finder_arel(options)
)
end
- def construct_finder_sql(options)
- construct_finder_arel(options).to_sql
+ def construct_finder_sql(options, scope = scope(:find))
+ construct_finder_arel(options, scope).to_sql
end
def construct_join(joins, scope)
View
23 activerecord/lib/active_record/calculations.rb
@@ -169,11 +169,11 @@ def execute_simple_calculation(operation, column_name, options) #:nodoc:
(column_name == :all ? "*" : column_name.to_s))
end
- value = construct_calculation_arel(options.merge(
+ value = construct_finder_sql(options.merge(
:select => operation == 'count' ? column.count(options[:distinct]) : column.send(operation)
- ))
+ ), nil)
- type_cast_calculated_value(connection.select_value(value.to_sql), column_for(column_name), operation)
+ type_cast_calculated_value(connection.select_value(value), column_for(column_name), operation)
end
def execute_grouped_calculation(operation, column_name, options) #:nodoc:
@@ -194,7 +194,7 @@ def execute_grouped_calculation(operation, column_name, options) #:nodoc:
options[:select] << ", #{group_field} AS #{group_alias}"
- calculated_data = connection.select_all(construct_calculation_arel(options).to_sql)
+ calculated_data = connection.select_all(construct_finder_sql(options, nil))
if association
key_ids = calculated_data.collect { |row| row[group_alias] }
@@ -212,21 +212,6 @@ def execute_grouped_calculation(operation, column_name, options) #:nodoc:
end
protected
-
- def construct_calculation_arel(options)
- scope = scope(:find)
-
- arel_table(options[:from] || table_name).
- join(options[:joins]).
- where(options[:conditions]).
- project(options[:select]).
- group(construct_group(options[:group], options[:having], scope)).
- order(options[:order].to_s).
- take(construct_limit(options, scope)).
- skip(construct_offset(options, scope)
- )
- end
-
def construct_count_options_from_args(*args)
options = {}
column_name = :all

0 comments on commit 3b27a48

Please sign in to comment.
Something went wrong with that request. Please try again.