aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/spawn_methods.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/relation/spawn_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb89
1 files changed, 41 insertions, 48 deletions
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 4ecee8c634..66eae69d92 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -1,49 +1,51 @@
module ActiveRecord
module SpawnMethods
- def spawn(relation = @relation)
- relation = Relation.new(@klass, relation)
- relation.readonly = @readonly
- relation.preload_associations = @preload_associations
- relation.eager_load_associations = @eager_load_associations
- relation.includes_associations = @includes_associations
- relation.create_with_attributes = @create_with_attributes
- relation.table = table
+ def spawn(arel_table = self.table)
+ relation = Relation.new(@klass, arel_table)
+
+ (Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS).each do |query_method|
+ relation.send(:"#{query_method}_values=", send(:"#{query_method}_values"))
+ end
+
+ Relation::SINGLE_VALUE_METHODS.each do |query_method|
+ relation.send(:"#{query_method}_value=", send(:"#{query_method}_value"))
+ end
+
relation
end
def merge(r)
raise ArgumentError, "Cannot merge a #{r.klass.name} relation with #{@klass.name} relation" if r.klass != @klass
- merged_relation = spawn(table).eager_load(r.eager_load_associations).preload(r.preload_associations).includes(r.includes_associations)
- merged_relation.readonly = r.readonly
-
- [self.relation, r.relation].each do |arel|
- merged_relation = merged_relation.
- joins(arel.joins(arel)).
- group(arel.groupings).
- limit(arel.taken).
- offset(arel.skipped).
- select(arel.send(:select_clauses)).
- from(arel.sources).
- having(arel.havings).
- lock(arel.locked)
- end
+ merged_relation = spawn.eager_load(r.eager_load_values).preload(r.preload_values).includes(r.includes_values)
+
+ merged_relation.readonly_value = r.readonly_value unless merged_relation.readonly_value
+ merged_relation.limit_value = r.limit_value unless merged_relation.limit_value
+ merged_relation.lock_value = r.lock_value unless merged_relation.lock_value
+
+ merged_relation = merged_relation.
+ joins(r.joins_values).
+ group(r.group_values).
+ offset(r.offset_value).
+ select(r.select_values).
+ from(r.from_value).
+ having(r.having_values)
- relation_order = r.send(:order_clause)
- merged_order = relation_order.present? ? relation_order : order_clause
- merged_relation = merged_relation.order(merged_order)
+ relation_order = r.order_values
+ merged_order = relation_order.present? ? relation_order : order_values
+ merged_relation.order_values = merged_order
- merged_relation.create_with_attributes = @create_with_attributes
+ merged_relation.create_with_value = @create_with_value
- if @create_with_attributes && r.create_with_attributes
- merged_relation.create_with_attributes = @create_with_attributes.merge(r.create_with_attributes)
+ if @create_with_value && r.create_with_value
+ merged_relation.create_with_value = @create_with_value.merge(r.create_with_value)
else
- merged_relation.create_with_attributes = r.create_with_attributes || @create_with_attributes
+ merged_relation.create_with_value = r.create_with_value || @create_with_value
end
- merged_wheres = @relation.wheres
+ merged_wheres = @where_values
- r.wheres.each do |w|
+ r.where_values.each do |w|
if w.is_a?(Arel::Predicates::Equality)
merged_wheres = merged_wheres.reject {|p| p.is_a?(Arel::Predicates::Equality) && p.operand1.name == w.operand1.name }
end
@@ -51,32 +53,23 @@ module ActiveRecord
merged_wheres << w
end
- merged_relation.where(*merged_wheres)
+ merged_relation.where_values = merged_wheres
+
+ merged_relation
end
alias :& :merge
def except(*skips)
result = Relation.new(@klass, table)
- result.table = table
- [:eager_load, :preload, :includes].each do |load_method|
- result = result.send(load_method, send(:"#{load_method}_associations"))
+ (Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS).each do |method|
+ result.send(:"#{method}_values=", send(:"#{method}_values")) unless skips.include?(method)
end
- result.readonly = self.readonly unless skips.include?(:readonly)
- result.create_with_attributes = @create_with_attributes unless skips.include?(:create_with)
-
- result = result.joins(@relation.joins(@relation)) unless skips.include?(:joins)
- result = result.group(@relation.groupings) unless skips.include?(:group)
- result = result.limit(@relation.taken) unless skips.include?(:limit)
- result = result.offset(@relation.skipped) unless skips.include?(:offset)
- result = result.select(@relation.send(:select_clauses)) unless skips.include?(:select)
- result = result.from(@relation.sources) unless skips.include?(:from)
- result = result.order(order_clause) unless skips.include?(:order)
- result = result.where(*@relation.wheres) unless skips.include?(:where)
- result = result.having(*@relation.havings) unless skips.include?(:having)
- result = result.lock(@relation.locked) unless skips.include?(:lock)
+ Relation::SINGLE_VALUE_METHODS.each do |method|
+ result.send(:"#{method}_value=", send(:"#{method}_value")) unless skips.include?(method)
+ end
result
end