Skip to content
This repository
Browse code

Move Relation#spawn and Relation#merge to a separate module

  • Loading branch information...
commit 8edfa8f82fecb23af506278d510ea900e6021e6b 1 parent 3eca0ab
Pratik authored
1  activerecord/lib/active_record.rb
@@ -55,6 +55,7 @@ module ActiveRecord
55 55
       autoload :FinderMethods
56 56
       autoload :CalculationMethods
57 57
       autoload :PredicateBuilder
  58
+      autoload :SpawnMethods
58 59
     end
59 60
 
60 61
     autoload :Base
47  activerecord/lib/active_record/relation.rb
... ...
@@ -1,6 +1,6 @@
1 1
 module ActiveRecord
2 2
   class Relation
3  
-    include QueryMethods, FinderMethods, CalculationMethods
  3
+    include QueryMethods, FinderMethods, CalculationMethods, SpawnMethods
4 4
 
5 5
     delegate :length, :collect, :map, :each, :all?, :to => :to_a
6 6
 
@@ -28,41 +28,6 @@ def create!(*args, &block)
28 28
       with_create_scope { @klass.create!(*args, &block) }
29 29
     end
30 30
 
31  
-    def merge(r)
32  
-      raise ArgumentError, "Cannot merge a #{r.klass.name} relation with #{@klass.name} relation" if r.klass != @klass
33  
-
34  
-      merged_relation = spawn(table).eager_load(r.eager_load_associations).preload(r.preload_associations).includes(r.include_associations)
35  
-      merged_relation.readonly = r.readonly
36  
-
37  
-      [self.relation, r.relation].each do |arel|
38  
-        merged_relation = merged_relation.
39  
-          joins(arel.joins(arel)).
40  
-          group(arel.groupings).
41  
-          limit(arel.taken).
42  
-          offset(arel.skipped).
43  
-          select(arel.send(:select_clauses)).
44  
-          from(arel.sources)
45  
-      end
46  
-
47  
-      relation_order = r.send(:order_clause)
48  
-      merged_order = relation_order.present? ? relation_order : order_clause
49  
-      merged_relation = merged_relation.order(merged_order)
50  
-
51  
-      merged_wheres = @relation.wheres
52  
-
53  
-      r.wheres.each do |w|
54  
-        if w.is_a?(Arel::Predicates::Equality)
55  
-          merged_wheres = merged_wheres.reject {|p| p.is_a?(Arel::Predicates::Equality) && p.operand1.name == w.operand1.name }
56  
-        end
57  
-
58  
-        merged_wheres << w
59  
-      end
60  
-
61  
-      merged_relation.where(*merged_wheres)
62  
-    end
63  
-
64  
-    alias :& :merge
65  
-
66 31
     def respond_to?(method, include_private = false)
67 32
       return true if @relation.respond_to?(method, include_private) || Array.method_defined?(method)
68 33
 
@@ -164,16 +129,6 @@ def reset
164 129
       self
165 130
     end
166 131
 
167  
-    def spawn(relation = @relation)
168  
-      relation = Relation.new(@klass, relation)
169  
-      relation.readonly = @readonly
170  
-      relation.preload_associations = @preload_associations
171  
-      relation.eager_load_associations = @eager_load_associations
172  
-      relation.include_associations = @include_associations
173  
-      relation.table = table
174  
-      relation
175  
-    end
176  
-
177 132
     def table
178 133
       @table ||= Arel::Table.new(@klass.table_name, Arel::Sql::Engine.new(@klass))
179 134
     end
49  activerecord/lib/active_record/relation/spawn_methods.rb
... ...
@@ -0,0 +1,49 @@
  1
+module ActiveRecord
  2
+  module SpawnMethods
  3
+    def spawn(relation = @relation)
  4
+      relation = Relation.new(@klass, relation)
  5
+      relation.readonly = @readonly
  6
+      relation.preload_associations = @preload_associations
  7
+      relation.eager_load_associations = @eager_load_associations
  8
+      relation.include_associations = @include_associations
  9
+      relation.table = table
  10
+      relation
  11
+    end
  12
+
  13
+    def merge(r)
  14
+      raise ArgumentError, "Cannot merge a #{r.klass.name} relation with #{@klass.name} relation" if r.klass != @klass
  15
+
  16
+      merged_relation = spawn(table).eager_load(r.eager_load_associations).preload(r.preload_associations).includes(r.include_associations)
  17
+      merged_relation.readonly = r.readonly
  18
+
  19
+      [self.relation, r.relation].each do |arel|
  20
+        merged_relation = merged_relation.
  21
+          joins(arel.joins(arel)).
  22
+          group(arel.groupings).
  23
+          limit(arel.taken).
  24
+          offset(arel.skipped).
  25
+          select(arel.send(:select_clauses)).
  26
+          from(arel.sources)
  27
+      end
  28
+
  29
+      relation_order = r.send(:order_clause)
  30
+      merged_order = relation_order.present? ? relation_order : order_clause
  31
+      merged_relation = merged_relation.order(merged_order)
  32
+
  33
+      merged_wheres = @relation.wheres
  34
+
  35
+      r.wheres.each do |w|
  36
+        if w.is_a?(Arel::Predicates::Equality)
  37
+          merged_wheres = merged_wheres.reject {|p| p.is_a?(Arel::Predicates::Equality) && p.operand1.name == w.operand1.name }
  38
+        end
  39
+
  40
+        merged_wheres << w
  41
+      end
  42
+
  43
+      merged_relation.where(*merged_wheres)
  44
+    end
  45
+
  46
+    alias :& :merge
  47
+
  48
+  end
  49
+end

0 notes on commit 8edfa8f

Please sign in to comment.
Something went wrong with that request. Please try again.