Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7189

Show
Ignore:
Timestamp:
07/16/07 20:26:10 (3 years ago)
Author:
rick
Message:

Remove deprecated count(conditions=nil, joins=nil) usage. Closes #8993 [lifofifo]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r7188 r7189  
    11*SVN* 
     2 
     3* Remove deprecated count(conditions=nil, joins=nil) usage.  Closes #8993 [lifofifo] 
    24 
    35* 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  
    2828          @reflection.klass.count_by_sql(@finder_sql) 
    2929        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)           
    3131          options[:conditions] = options[:conditions].nil? ? 
    3232            @finder_sql : 
  • trunk/activerecord/lib/active_record/calculations.rb

    r6919 r7189  
    77 
    88    module ClassMethods 
    9       # Count operates using three different approaches. 
     9      # Count operates using two different approaches. 
    1010      # 
    1111      # * 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.0 
    1312      # * Count using options will find the row count matched by the options used. 
    1413      # 
    15       # The last approach, 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: 
    1615      # 
    1716      # * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro. 
     
    3029      #   Person.count         # returns the total count of all people 
    3130      # 
    32       # Examples for count by +conditions+ and +joins+ (this has been deprecated): 
    33       #   Person.count("age > 26")  # returns the number of people older than 26 
    34       #   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       # 
    3631      # Examples for count with options: 
    3732      #   Person.count(:conditions => "age > 26") 
     
    4338      # Note: Person.count(:all) will not work because it will use :all as the condition.  Use Person.count instead. 
    4439      def count(*args) 
    45         calculate(:count, *construct_count_options_from_legacy_args(*args)) 
     40        calculate(:count, *construct_count_options_from_args(*args)) 
    4641      end 
    4742 
     
    126121 
    127122      protected 
    128         def construct_count_options_from_legacy_args(*args) 
     123        def construct_count_options_from_args(*args) 
    129124          options     = {} 
    130125          column_name = :all 
     
    134129          #   count(options={}) 
    135130          #   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 
    138136            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 
    155138 
    156139          [column_name, options] 
  • trunk/activerecord/test/associations_test.rb

    r7188 r7189  
    434434 
    435435  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') 
    439437  end 
    440438 
  • trunk/activerecord/test/base_test.rb

    r7144 r7189  
    13311331  def test_count_with_join 
    13321332    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") 
    13381335    assert_equal res, res2 
    13391336     
  • trunk/activerecord/test/calculations_test.rb

    r6919 r7189  
    234234  end 
    235235 
    236   def test_deprecated_count_with_string_parameters 
    237     assert_deprecated('count') { Account.count('credit_limit > 50') } 
    238   end 
    239  
    240236  def test_count_with_no_parameters_isnt_deprecated 
    241237    assert_not_deprecated { Account.count }