From a0bf0195a5c27c8c8493b1a44987154a9dc0d526 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 26 Sep 2006 17:02:45 +0000 Subject: Deprecation: count class method should be called with an options hash rather than two args for conditions and joins. Closes #6287. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations/has_many_association.rb | 2 +- activerecord/lib/active_record/calculations.rb | 37 ++++++++++++++-------- 2 files changed, 24 insertions(+), 15 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 226698611c..d0af68eea7 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -137,7 +137,7 @@ module ActiveRecord elsif @reflection.options[:counter_sql] @reflection.klass.count_by_sql(@counter_sql) else - @reflection.klass.count(@counter_sql) + @reflection.klass.count(:conditions => @counter_sql) end @target = [] and loaded if count == 0 diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index e0ac96c879..aab0fa2a74 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -9,7 +9,7 @@ module ActiveRecord # Count operates using three different approaches. # # * Count all: By not passing any parameters to count, it will return a count of all the rows for the model. - # * Count by conditions or joins: For backwards compatibility, you can pass in +conditions+ and +joins+ as individual parameters. + # * Count by conditions or joins: This API has been deprecated and will be removed in Rails 2.0 # * Count using options will find the row count matched by the options used. # # The last approach, count using options, accepts an option hash as the only parameter. The options are: @@ -29,7 +29,7 @@ module ActiveRecord # Examples for counting all: # Person.count # returns the total count of all people # - # Examples for count by +conditions+ and +joins+ (for backwards compatibility): + # Examples for count by +conditions+ and +joins+ (this has been deprecated): # Person.count("age > 26") # returns the number of people older than 26 # Person.find("age > 26 AND job.salary > 60000", "LEFT JOIN jobs on jobs.person_id = person.id") # returns the total number of rows matching the conditions and joins fetched by SELECT COUNT(*). # @@ -128,24 +128,33 @@ module ActiveRecord def construct_count_options_from_legacy_args(*args) options = {} column_name = :all - # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}). - if args.size >= 0 && args.size <= 2 - if args.first.is_a?(Hash) - options = args.first + + # We need to handle + # count() + # count(options={}) + # count(column_name=:all, options={}) + # count(conditions=nil, joins=nil) # deprecated + if args.size > 2 + raise ArgumentError, "Unexpected parameters passed to count(options={}): #{args.inspect}" + elsif args.size > 0 + if args[0].is_a?(Hash) + options = args[0] elsif args[1].is_a?(Hash) - options = args[1] - column_name = args.first + column_name, options = args else - # Handle legacy paramter options: def count(conditions=nil, joins=nil) - options.merge!(:conditions => args[0]) if args.length > 0 - options.merge!(:joins => args[1]) if args.length > 1 + # Deprecated count(conditions, joins=nil) + ActiveSupport::Deprecation.warn( + "You called count(#{args[0].inspect}, #{args[1].inspect}), which is a deprecated API call. Instead you should use " + + "count(column_name, options). Passing the conditions and joins as string parameters will be removed in Rails 2.0." + ) + options.merge!(:conditions => args[0]) + options.merge!(:joins => args[1]) if args[1] end - else - raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})") end + [column_name, options] end - + def construct_calculation_sql(operation, column_name, options) #:nodoc: scope = scope(:find) merged_includes = merge_includes(scope ? scope[:include] : [], options[:include]) -- cgit v1.2.3