aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/calculations.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-06-19 00:36:30 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-06-19 21:01:27 -0300
commit60ffe41980f3989637bdf537341288288aa9cc39 (patch)
tree2baf95fdcd21a9b2e551ef73cc3f12687588d07e /activerecord/lib/active_record/relation/calculations.rb
parent1f98493e4a3c53e53666ff740860b20dcd6d08ec (diff)
downloadrails-60ffe41980f3989637bdf537341288288aa9cc39.tar.gz
rails-60ffe41980f3989637bdf537341288288aa9cc39.tar.bz2
rails-60ffe41980f3989637bdf537341288288aa9cc39.zip
Extract conditional to a method to avoid duplication
Also use if/else block to not use short circuit return
Diffstat (limited to 'activerecord/lib/active_record/relation/calculations.rb')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb34
1 files changed, 19 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index d457062341..22c3e6a324 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -108,7 +108,7 @@ module ActiveRecord
if relation.equal?(self)
- if eager_loading? || (includes_values.present? && (column_name || references_eager_loaded_tables?))
+ if has_include?(column_name)
construct_relation_for_association_calculations.calculate(operation, column_name, options)
else
perform_calculation(operation, column_name, options)
@@ -156,25 +156,25 @@ module ActiveRecord
column_name = "#{table_name}.#{column_name}"
end
- if eager_loading? || (includes_values.present? && (column_name || references_eager_loaded_tables?))
- return construct_relation_for_association_calculations.pluck(column_name)
- end
-
- result = klass.connection.select_all(select(column_name).arel, nil, bind_values)
+ if has_include?(column_name)
+ construct_relation_for_association_calculations.pluck(column_name)
+ else
+ result = klass.connection.select_all(select(column_name).arel, nil, bind_values)
- key = result.columns.first
- column = klass.column_types.fetch(key) {
- result.column_types.fetch(key) {
- Class.new { def type_cast(v); v; end }.new
+ key = result.columns.first
+ column = klass.column_types.fetch(key) {
+ result.column_types.fetch(key) {
+ Class.new { def type_cast(v); v; end }.new
+ }
}
- }
- result.map do |attributes|
- raise ArgumentError, "Pluck expects to select just one attribute: #{attributes.inspect}" unless attributes.one?
+ result.map do |attributes|
+ raise ArgumentError, "Pluck expects to select just one attribute: #{attributes.inspect}" unless attributes.one?
- value = klass.initialize_attributes(attributes).values.first
+ value = klass.initialize_attributes(attributes).values.first
- column.type_cast(value)
+ column.type_cast(value)
+ end
end
end
@@ -190,6 +190,10 @@ module ActiveRecord
private
+ def has_include?(column_name)
+ eager_loading? || (includes_values.present? && (column_name || references_eager_loaded_tables?))
+ end
+
def perform_calculation(operation, column_name, options = {})
operation = operation.to_s.downcase