From 3750e58e12b5e07c80ac40772c7c47f1010b9ae8 Mon Sep 17 00:00:00 2001 From: Ben Munat Date: Wed, 11 Jun 2008 16:26:35 -0700 Subject: [PATCH] Add :from option to Calculations module --- activerecord/lib/active_record/calculations.rb | 12 +++++-- activerecord/test/cases/calculations_test.rb | 46 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index caa8c53..2ca1a0a 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -1,6 +1,6 @@ module ActiveRecord module Calculations #:nodoc: - CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include] + CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include, :from] def self.included(base) base.extend(ClassMethods) end @@ -27,6 +27,8 @@ module ActiveRecord # * :select: By default, this is * as in SELECT * FROM, but can be changed if you, for example, want to do a join but not # include the joined columns. # * :distinct: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ... + # * :from - By default, this is the table name of the class, but can be changed to an alternate table name (or even the name + # of a database view). # # Examples for counting all: # Person.count # returns the total count of all people @@ -178,8 +180,12 @@ module ActiveRecord sql = "SELECT COUNT(*) AS #{aggregate_alias}" if use_workaround sql << ", #{options[:group_field]} AS #{options[:group_alias]}" if options[:group] - sql << " FROM (SELECT #{distinct}#{column_name}" if use_workaround - sql << " FROM #{connection.quote_table_name(table_name)} " + if options[:from] + sql << " FROM #{options[:from]} " + else + sql << " FROM (SELECT #{distinct}#{column_name}" if use_workaround + sql << " FROM #{connection.quote_table_name(table_name)} " + end if merged_includes.any? join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merged_includes, options[:joins]) sql << join_dependency.join_associations.collect{|join| join.association_join }.join diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index aefb13e..29e6001 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -1,6 +1,7 @@ require "cases/helper" require 'models/company' require 'models/topic' +require 'models/edge' Company.has_many :accounts @@ -274,4 +275,49 @@ class CalculationsTest < ActiveRecord::TestCase def test_should_sum_expression assert_equal 636, Account.sum("2 * credit_limit") end + + def test_count_with_from_option + assert_equal Company.count(:all), Company.count(:all, :from => 'companies') + assert_equal Account.count(:all, :conditions => "credit_limit = 50"), + Account.count(:all, :from => 'accounts', :conditions => "credit_limit = 50") + assert_equal Company.count(:type, :conditions => {:type => "Firm"}), + Company.count(:type, :conditions => {:type => "Firm"}, :from => 'companies') + end + + def test_sum_with_from_option + assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts') + assert_equal Account.sum(:credit_limit, :conditions => "credit_limit > 50"), + Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50") + end + + def test_average_with_from_option + assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts') + assert_equal Account.average(:credit_limit, :conditions => "credit_limit > 50"), + Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50") + end + + def test_minimum_with_from_option + assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts') + assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit > 50"), + Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50") + end + + def test_maximum_with_from_option + assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts') + assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit > 50"), + Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50") + end + + def test_from_option_with_specified_index + if Edge.connection.adapter_name == 'MySQL' + assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)') + assert_equal Edge.count(:all, :conditions => 'sink_id < 5'), + Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5') + end + end + + def test_from_option_with_table_different_than_class + assert_equal Account.count(:all), Company.count(:all, :from => 'accounts') + end + end -- 1.5.5.3