aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/calculations.rb
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-05-06 14:16:03 -0300
committerEmilio Tagua <miloops@gmail.com>2009-05-06 14:16:03 -0300
commit8885b2d6c1855742600d0afdb9dfc002acb62e5e (patch)
tree7c217e880d84e3bda88902a89540355d41be39c7 /activerecord/lib/active_record/calculations.rb
parent118b19a9fa59587e72af315bfbc0e6812025f12b (diff)
downloadrails-8885b2d6c1855742600d0afdb9dfc002acb62e5e.tar.gz
rails-8885b2d6c1855742600d0afdb9dfc002acb62e5e.tar.bz2
rails-8885b2d6c1855742600d0afdb9dfc002acb62e5e.zip
Refactor to calculations. Migration's versions are string not integer. ARel submodule updated.
Diffstat (limited to 'activerecord/lib/active_record/calculations.rb')
-rw-r--r--activerecord/lib/active_record/calculations.rb17
1 files changed, 8 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index c941be2837..9d04686a5f 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -170,12 +170,12 @@ module ActiveRecord
elsif !options[:select].blank?
column_name = options[:select]
end
- construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).count(options[:distinct]))).select_value
+ construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).count(options[:distinct])))
else
- construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).send(operation))).select_value
+ construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).send(operation)))
end
- type_cast_calculated_value(value, column_for(column_name), operation)
+ type_cast_calculated_value(connection.select_value(value.to_sql), column_for(column_name), operation)
end
def execute_grouped_calculation(operation, column_name, options) #:nodoc:
@@ -190,12 +190,11 @@ module ActiveRecord
aggregate_alias = column_alias_for(operation, column_name)
- if operation == 'count' && column_name == :all
- options[:select] = "COUNT(*) AS count_all, #{group_field} AS #{group_alias}"
- else
- arel_column = Arel::Attribute.new(arel_table, column_name).send(operation)
- options[:select] = "#{arel_column.as(aggregate_alias).to_sql}, #{group_field} AS #{group_alias}"
- end
+ options[:select] = (operation == 'count' && column_name == :all) ?
+ "COUNT(*) AS count_all" :
+ Arel::Attribute.new(arel_table, column_name).send(operation).as(aggregate_alias).to_sql
+
+ options[:select] << ", #{group_field} AS #{group_alias}"
calculated_data = connection.select_all(construct_calculation_arel(options).to_sql)