Changeset 9082
- Timestamp:
- 03/23/08 05:00:25 (4 months ago)
- Files:
-
- trunk/activerecord/lib/active_record/base.rb (modified) (4 diffs)
- trunk/activerecord/test/cases/method_scoping_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/base.rb
r9056 r9082 1427 1427 end 1428 1428 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 1429 1443 # Object#to_a is deprecated, though it does have the desired behavior 1430 1444 def safe_to_array(o) … … 1499 1513 def add_conditions!(sql, conditions, scope = :auto) 1500 1514 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? 1507 1520 end 1508 1521 … … 1746 1759 merge = hash[method][key] && params[key] # merge if both scopes have the same key 1747 1760 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]) 1749 1762 elsif key == :include && merge 1750 1763 hash[method][key] = merge_includes(hash[method][key], params[key]).uniq … … 1940 1953 # # => "status = NULL , group_id = 1" 1941 1954 def sanitize_sql_hash_for_assignment(attrs) 1942 conditions =attrs.map do |attr, value|1955 attrs.map do |attr, value| 1943 1956 "#{connection.quote_column_name(attr)} = #{quote_bound_value(value)}" 1944 1957 end.join(', ') trunk/activerecord/test/cases/method_scoping_test.rb
r8681 r9082 151 151 Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do 152 152 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) 154 154 assert_equal(1, Developer.count) 155 155 end … … 164 164 Developer.with_scope(:find => { :conditions => "name = 'David'" }) do 165 165 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) 167 167 end 168 168 end … … 272 272 Developer.with_scope(:find => { :conditions => ['salary > ?', 9000] }) do 273 273 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 274 294 end 275 295 end