Changeset 7189
- Timestamp:
- 07/16/07 20:26:10 (3 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/has_many_association.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/calculations.rb (modified) (5 diffs)
- trunk/activerecord/test/associations_test.rb (modified) (1 diff)
- trunk/activerecord/test/base_test.rb (modified) (1 diff)
- trunk/activerecord/test/calculations_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r7188 r7189 1 1 *SVN* 2 3 * Remove deprecated count(conditions=nil, joins=nil) usage. Closes #8993 [lifofifo] 2 4 3 5 * Change belongs_to so that the foreign_key assumption is taken from the association name, not the class name. Closes #8992 [hasmanyjosh] trunk/activerecord/lib/active_record/associations/has_many_association.rb
r6998 r7189 28 28 @reflection.klass.count_by_sql(@finder_sql) 29 29 else 30 column_name, options = @reflection.klass.send(:construct_count_options_from_ legacy_args, *args)30 column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args) 31 31 options[:conditions] = options[:conditions].nil? ? 32 32 @finder_sql : trunk/activerecord/lib/active_record/calculations.rb
r6919 r7189 7 7 8 8 module ClassMethods 9 # Count operates using t hreedifferent approaches.9 # Count operates using two different approaches. 10 10 # 11 11 # * Count all: By not passing any parameters to count, it will return a count of all the rows for the model. 12 # * Count by conditions or joins: This API has been deprecated and will be removed in Rails 2.013 12 # * Count using options will find the row count matched by the options used. 14 13 # 15 # The lastapproach, count using options, accepts an option hash as the only parameter. The options are:14 # The second approach, count using options, accepts an option hash as the only parameter. The options are: 16 15 # 17 16 # * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro. … … 30 29 # Person.count # returns the total count of all people 31 30 # 32 # Examples for count by +conditions+ and +joins+ (this has been deprecated):33 # Person.count("age > 26") # returns the number of people older than 2634 # 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(*).35 #36 31 # Examples for count with options: 37 32 # Person.count(:conditions => "age > 26") … … 43 38 # Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead. 44 39 def count(*args) 45 calculate(:count, *construct_count_options_from_ legacy_args(*args))40 calculate(:count, *construct_count_options_from_args(*args)) 46 41 end 47 42 … … 126 121 127 122 protected 128 def construct_count_options_from_ legacy_args(*args)123 def construct_count_options_from_args(*args) 129 124 options = {} 130 125 column_name = :all … … 134 129 # count(options={}) 135 130 # count(column_name=:all, options={}) 136 # count(conditions=nil, joins=nil) # deprecated 137 if args.size > 2 131 if args[0].is_a?(Hash) 132 options = args[0] 133 elsif args[1].is_a?(Hash) 134 column_name, options = args 135 else 138 136 raise ArgumentError, "Unexpected parameters passed to count(options={}): #{args.inspect}" 139 elsif args.size > 0 140 if args[0].is_a?(Hash) 141 options = args[0] 142 elsif args[1].is_a?(Hash) 143 column_name, options = args 144 else 145 # Deprecated count(conditions, joins=nil) 146 ActiveSupport::Deprecation.warn( 147 "You called count(#{args[0].inspect}, #{args[1].inspect}), which is a deprecated API call. " + 148 "Instead you should use count(column_name, options). Passing the conditions and joins as " + 149 "string parameters will be removed in Rails 2.0.", caller(2) 150 ) 151 options.merge!(:conditions => args[0]) 152 options.merge!(:joins => args[1]) if args[1] 153 end 154 end 137 end if args.size > 0 155 138 156 139 [column_name, options] trunk/activerecord/test/associations_test.rb
r7188 r7189 434 434 435 435 def test_counting_with_single_conditions 436 assert_deprecated 'count' do 437 assert_equal 2, Firm.find(:first).plain_clients.count('1=1') 438 end 436 assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => '1=1') 439 437 end 440 438 trunk/activerecord/test/base_test.rb
r7144 r7189 1331 1331 def test_count_with_join 1332 1332 res = Post.count_by_sql "SELECT COUNT(*) FROM posts LEFT JOIN comments ON posts.id=comments.post_id WHERE posts.#{QUOTED_TYPE} = 'Post'" 1333 res2 = nil 1334 assert_deprecated 'count' do 1335 res2 = Post.count("posts.#{QUOTED_TYPE} = 'Post'", 1336 "LEFT JOIN comments ON posts.id=comments.post_id") 1337 end 1333 1334 res2 = Post.count(:conditions => "posts.#{QUOTED_TYPE} = 'Post'", :joins => "LEFT JOIN comments ON posts.id=comments.post_id") 1338 1335 assert_equal res, res2 1339 1336 trunk/activerecord/test/calculations_test.rb
r6919 r7189 234 234 end 235 235 236 def test_deprecated_count_with_string_parameters237 assert_deprecated('count') { Account.count('credit_limit > 50') }238 end239 240 236 def test_count_with_no_parameters_isnt_deprecated 241 237 assert_not_deprecated { Account.count }