1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract conditional to a method to avoid duplication

Also use if/else block to not use short circuit return
This commit is contained in:
Rafael Mendonça França 2012-06-19 00:36:30 -03:00
parent 1f98493e4a
commit 60ffe41980
2 changed files with 22 additions and 15 deletions

View file

@ -108,7 +108,7 @@ module ActiveRecord
if relation.equal?(self) 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) construct_relation_for_association_calculations.calculate(operation, column_name, options)
else else
perform_calculation(operation, column_name, options) perform_calculation(operation, column_name, options)
@ -156,10 +156,9 @@ module ActiveRecord
column_name = "#{table_name}.#{column_name}" column_name = "#{table_name}.#{column_name}"
end end
if eager_loading? || (includes_values.present? && (column_name || references_eager_loaded_tables?)) if has_include?(column_name)
return construct_relation_for_association_calculations.pluck(column_name) construct_relation_for_association_calculations.pluck(column_name)
end else
result = klass.connection.select_all(select(column_name).arel, nil, bind_values) result = klass.connection.select_all(select(column_name).arel, nil, bind_values)
key = result.columns.first key = result.columns.first
@ -177,6 +176,7 @@ module ActiveRecord
column.type_cast(value) column.type_cast(value)
end end
end end
end
# Pluck all the ID's for the relation using the table's primary key # Pluck all the ID's for the relation using the table's primary key
# #
@ -190,6 +190,10 @@ module ActiveRecord
private 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 = {}) def perform_calculation(operation, column_name, options = {})
operation = operation.to_s.downcase operation = operation.to_s.downcase

View file

@ -423,6 +423,7 @@ class CalculationsTest < ActiveRecord::TestCase
def test_maximum_with_not_auto_table_name_prefix_if_column_included def test_maximum_with_not_auto_table_name_prefix_if_column_included
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
# TODO: Investigate why PG isn't being typecast
if current_adapter?(:PostgreSQLAdapter) if current_adapter?(:PostgreSQLAdapter)
assert_equal "7", Company.includes(:contracts).maximum(:developer_id) assert_equal "7", Company.includes(:contracts).maximum(:developer_id)
else else
@ -433,6 +434,7 @@ class CalculationsTest < ActiveRecord::TestCase
def test_minimum_with_not_auto_table_name_prefix_if_column_included def test_minimum_with_not_auto_table_name_prefix_if_column_included
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
# TODO: Investigate why PG isn't being typecast
if current_adapter?(:PostgreSQLAdapter) if current_adapter?(:PostgreSQLAdapter)
assert_equal "7", Company.includes(:contracts).minimum(:developer_id) assert_equal "7", Company.includes(:contracts).minimum(:developer_id)
else else
@ -443,6 +445,7 @@ class CalculationsTest < ActiveRecord::TestCase
def test_sum_with_not_auto_table_name_prefix_if_column_included def test_sum_with_not_auto_table_name_prefix_if_column_included
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
# TODO: Investigate why PG isn't being typecast
if current_adapter?(:MysqlAdapter) || current_adapter?(:PostgreSQLAdapter) if current_adapter?(:MysqlAdapter) || current_adapter?(:PostgreSQLAdapter)
assert_equal "7", Company.includes(:contracts).sum(:developer_id) assert_equal "7", Company.includes(:contracts).sum(:developer_id)
else else