mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Avoid sanitize_sql when we can use Relation#where instead
This commit is contained in:
parent
ee7f666035
commit
f6cc6651f2
4 changed files with 16 additions and 22 deletions
|
@ -90,8 +90,11 @@ module ActiveRecord
|
||||||
|
|
||||||
scope = scope.joins(join(foreign_table, constraint))
|
scope = scope.joins(join(foreign_table, constraint))
|
||||||
|
|
||||||
unless conditions.empty?
|
conditions.each do |condition|
|
||||||
scope = scope.where(sanitize(conditions, table))
|
condition = interpolate(condition)
|
||||||
|
condition = { (table.table_alias || table.name) => condition } unless i == 0
|
||||||
|
|
||||||
|
scope = scope.where(condition)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -95,8 +95,11 @@ module ActiveRecord
|
||||||
conditions = self.conditions[i].dup
|
conditions = self.conditions[i].dup
|
||||||
conditions << { reflection.type => foreign_klass.base_class.name } if reflection.type
|
conditions << { reflection.type => foreign_klass.base_class.name } if reflection.type
|
||||||
|
|
||||||
unless conditions.empty?
|
conditions.each do |condition|
|
||||||
constraint = constraint.and(sanitize(conditions, table))
|
condition = active_record.send(:sanitize_sql, interpolate(condition), table.table_alias || table.name)
|
||||||
|
condition = Arel.sql(condition) unless condition.is_a?(Arel::Node)
|
||||||
|
|
||||||
|
constraint = constraint.and(condition)
|
||||||
end
|
end
|
||||||
|
|
||||||
relation.from(join(table, constraint))
|
relation.from(join(table, constraint))
|
||||||
|
|
|
@ -40,16 +40,6 @@ module ActiveRecord
|
||||||
def join(table, constraint)
|
def join(table, constraint)
|
||||||
table.create_join(table, table.create_on(constraint), join_type)
|
table.create_join(table, table.create_on(constraint), join_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def sanitize(conditions, table)
|
|
||||||
conditions = conditions.map do |condition|
|
|
||||||
condition = active_record.send(:sanitize_sql, interpolate(condition), table.table_alias || table.name)
|
|
||||||
condition = Arel.sql(condition) unless condition.is_a?(Arel::Node)
|
|
||||||
condition
|
|
||||||
end
|
|
||||||
|
|
||||||
conditions.length == 1 ? conditions.first : Arel::Nodes::And.new(conditions)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -95,8 +95,8 @@ module ActiveRecord
|
||||||
def build_scope
|
def build_scope
|
||||||
scope = klass.scoped
|
scope = klass.scoped
|
||||||
|
|
||||||
scope = scope.where(process_conditions(options[:conditions]))
|
scope = scope.where(interpolate(options[:conditions]))
|
||||||
scope = scope.where(process_conditions(preload_options[:conditions]))
|
scope = scope.where(interpolate(preload_options[:conditions]))
|
||||||
|
|
||||||
scope = scope.select(preload_options[:select] || options[:select] || table[Arel.star])
|
scope = scope.select(preload_options[:select] || options[:select] || table[Arel.star])
|
||||||
scope = scope.includes(preload_options[:include] || options[:include])
|
scope = scope.includes(preload_options[:include] || options[:include])
|
||||||
|
@ -112,13 +112,11 @@ module ActiveRecord
|
||||||
scope
|
scope
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_conditions(conditions)
|
def interpolate(conditions)
|
||||||
if conditions.respond_to?(:to_proc)
|
if conditions.respond_to?(:to_proc)
|
||||||
conditions = klass.send(:instance_eval, &conditions)
|
klass.send(:instance_eval, &conditions)
|
||||||
end
|
else
|
||||||
|
conditions
|
||||||
if conditions
|
|
||||||
klass.send(:sanitize_sql, conditions)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue