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

Changeset 9082

Show
Ignore:
Timestamp:
03/23/08 05:00:25 (4 months ago)
Author:
bitsweat
Message:

Fix merging blank conditions. Closes #10764 [mcmire, cavalle]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/base.rb

    r9056 r9082  
    14271427        end 
    14281428 
     1429        # Merges conditions so that the result is a valid +condition+ 
     1430        def merge_conditions(*conditions) 
     1431          segments = [] 
     1432 
     1433          conditions.each do |condition| 
     1434            unless condition.blank? 
     1435              sql = sanitize_sql(condition) 
     1436              segments << sql unless sql.blank? 
     1437            end 
     1438          end 
     1439 
     1440          "(#{segments.join(') AND (')})" unless segments.empty? 
     1441        end 
     1442 
    14291443        # Object#to_a is deprecated, though it does have the desired behavior 
    14301444        def safe_to_array(o) 
     
    14991513        def add_conditions!(sql, conditions, scope = :auto) 
    15001514          scope = scope(:find) if :auto == scope 
    1501           segments = [] 
    1502           segments << sanitize_sql(scope[:conditions]) if scope && !scope[:conditions].blank? 
    1503           segments << sanitize_sql(conditions) unless conditions.blank? 
    1504           segments << type_condition if finder_needs_type_condition? 
    1505           segments.delete_if{|s| s.blank?} 
    1506           sql << "WHERE (#{segments.join(") AND (")}) " unless segments.empty? 
     1515          conditions = [conditions] 
     1516          conditions << scope[:conditions] if scope 
     1517          conditions << type_condition if finder_needs_type_condition? 
     1518          merged_conditions = merge_conditions(*conditions) 
     1519          sql << "WHERE #{merged_conditions} " unless merged_conditions.blank? 
    15071520        end 
    15081521 
     
    17461759                      merge = hash[method][key] && params[key] # merge if both scopes have the same key 
    17471760                      if key == :conditions && merge 
    1748                         hash[method][key] = [params[key], hash[method][key]].collect{ |sql| "( %s )" % sanitize_sql(sql) }.join(" AND "
     1761                        hash[method][key] = merge_conditions(params[key], hash[method][key]
    17491762                      elsif key == :include && merge 
    17501763                        hash[method][key] = merge_includes(hash[method][key], params[key]).uniq 
     
    19401953        #     # => "status = NULL , group_id = 1" 
    19411954        def sanitize_sql_hash_for_assignment(attrs) 
    1942           conditions = attrs.map do |attr, value| 
     1955          attrs.map do |attr, value| 
    19431956            "#{connection.quote_column_name(attr)} = #{quote_bound_value(value)}" 
    19441957          end.join(', ') 
  • trunk/activerecord/test/cases/method_scoping_test.rb

    r8681 r9082  
    151151      Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do 
    152152        appended_condition = Developer.instance_eval('current_scoped_methods')[:find][:conditions] 
    153         assert_equal("( name = 'David' ) AND ( salary = 80000 )", appended_condition) 
     153        assert_equal("(name = 'David') AND (salary = 80000)", appended_condition) 
    154154        assert_equal(1, Developer.count) 
    155155      end 
     
    164164      Developer.with_scope(:find => { :conditions => "name = 'David'" }) do 
    165165        merged_option = Developer.instance_eval('current_scoped_methods')[:find] 
    166         assert_equal({ :conditions => "( salary = 80000 ) AND ( name = 'David' )", :limit => 10 }, merged_option) 
     166        assert_equal({ :conditions => "(salary = 80000) AND (name = 'David')", :limit => 10 }, merged_option) 
    167167      end 
    168168    end 
     
    272272      Developer.with_scope(:find => { :conditions => ['salary > ?', 9000] }) do 
    273273        assert_equal %w(David), Developer.find(:all).map { |d| d.name } 
     274      end 
     275    end 
     276  end 
     277 
     278  def test_merged_scoped_find_on_blank_conditions 
     279    [nil, " ", [], {}].each do |blank| 
     280      Developer.with_scope(:find => {:conditions => blank}) do 
     281        Developer.with_scope(:find => {:conditions => blank}) do 
     282          assert_nothing_raised { Developer.find(:first) } 
     283        end 
     284      end 
     285    end 
     286  end 
     287 
     288  def test_merged_scoped_find_on_blank_bind_conditions 
     289    [ [""], ["",{}] ].each do |blank| 
     290      Developer.with_scope(:find => {:conditions => blank}) do 
     291        Developer.with_scope(:find => {:conditions => blank}) do 
     292          assert_nothing_raised { Developer.find(:first) } 
     293        end 
    274294      end 
    275295    end