From a483ae6477fab482e95b3415bb9b0cf54f198699 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 21 May 2013 10:35:35 -0700 Subject: push partion logic down and initialization logic up --- activerecord/lib/active_record/relation/merger.rb | 26 ++++++++++------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'activerecord/lib/active_record/relation/merger.rb') diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb index 58ac239190..eb72d551da 100644 --- a/activerecord/lib/active_record/relation/merger.rb +++ b/activerecord/lib/active_record/relation/merger.rb @@ -99,7 +99,10 @@ module ActiveRecord end def merge_multi_values - relation.where_values = merged_wheres + rhs_wheres = values[:where] || [] + lhs_wheres = relation.where_values + + relation.where_values = merged_wheres(lhs_wheres, rhs_wheres) relation.bind_values = merged_binds if values[:reordering] @@ -131,30 +134,23 @@ module ActiveRecord end end - def merged_wheres - rhs_wheres = values[:where] || [] - lhs_wheres = relation.where_values - - if rhs_wheres.empty? || lhs_wheres.empty? - lhs_wheres + rhs_wheres - else - reject_overwrites(lhs_wheres, rhs_wheres) + rhs_wheres - end + def merged_wheres(lhs_wheres, rhs_wheres) + partition_overwrites(lhs_wheres, rhs_wheres).last + rhs_wheres end # Remove equalities from the existing relation with a LHS which is # present in the relation being merged in. - def reject_overwrites(lhs_wheres, rhs_wheres) - partition_overwrites(lhs_wheres, rhs_wheres).last - end - + # returns [things_to_remove, things_to_keep] def partition_overwrites(lhs_wheres, rhs_wheres) + if lhs_wheres.empty? || rhs_wheres.empty? + return [[], lhs_wheres] + end + nodes = rhs_wheres.find_all do |w| w.respond_to?(:operator) && w.operator == :== end seen = Set.new(nodes) { |node| node.left } - # returns [deleted, keepers] lhs_wheres.partition do |w| w.respond_to?(:operator) && w.operator == :== && seen.include?(w.left) end -- cgit v1.2.3