1
0
Fork 0
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:
Jon Leighton 2012-01-14 20:29:28 +00:00
parent ee7f666035
commit f6cc6651f2
4 changed files with 16 additions and 22 deletions

View file

@ -90,8 +90,11 @@ module ActiveRecord
scope = scope.joins(join(foreign_table, constraint))
unless conditions.empty?
scope = scope.where(sanitize(conditions, table))
conditions.each do |condition|
condition = interpolate(condition)
condition = { (table.table_alias || table.name) => condition } unless i == 0
scope = scope.where(condition)
end
end
end

View file

@ -95,8 +95,11 @@ module ActiveRecord
conditions = self.conditions[i].dup
conditions << { reflection.type => foreign_klass.base_class.name } if reflection.type
unless conditions.empty?
constraint = constraint.and(sanitize(conditions, table))
conditions.each 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)
constraint = constraint.and(condition)
end
relation.from(join(table, constraint))

View file

@ -40,16 +40,6 @@ module ActiveRecord
def join(table, constraint)
table.create_join(table, table.create_on(constraint), join_type)
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

View file

@ -95,8 +95,8 @@ module ActiveRecord
def build_scope
scope = klass.scoped
scope = scope.where(process_conditions(options[:conditions]))
scope = scope.where(process_conditions(preload_options[:conditions]))
scope = scope.where(interpolate(options[:conditions]))
scope = scope.where(interpolate(preload_options[:conditions]))
scope = scope.select(preload_options[:select] || options[:select] || table[Arel.star])
scope = scope.includes(preload_options[:include] || options[:include])
@ -112,13 +112,11 @@ module ActiveRecord
scope
end
def process_conditions(conditions)
def interpolate(conditions)
if conditions.respond_to?(:to_proc)
conditions = klass.send(:instance_eval, &conditions)
end
if conditions
klass.send(:sanitize_sql, conditions)
klass.send(:instance_eval, &conditions)
else
conditions
end
end
end