1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Inject the visitor rather than relying on other objects internals

This is ultimately messy no matter what, and increases the coupling to the
database backend, but we can at least contain it somewhat into an object
that's already coupled.
This commit is contained in:
Sean Griffin 2015-12-17 12:54:27 -07:00
parent f726dbe080
commit cd395b9419
2 changed files with 7 additions and 2 deletions

View file

@ -179,7 +179,7 @@ module Arel
def where_sql engine = Table.engine def where_sql engine = Table.engine
return if @ctx.wheres.empty? return if @ctx.wheres.empty?
viz = Visitors::WhereSql.new engine.connection viz = Visitors::WhereSql.new(engine.connection.visitor, engine.connection)
Nodes::SqlLiteral.new viz.accept(@ctx, Collectors::SQLString.new).value Nodes::SqlLiteral.new viz.accept(@ctx, Collectors::SQLString.new).value
end end

View file

@ -1,12 +1,17 @@
module Arel module Arel
module Visitors module Visitors
class WhereSql < Arel::Visitors::ToSql class WhereSql < Arel::Visitors::ToSql
def initialize(inner_visitor, *args, &block)
@inner_visitor = inner_visitor
super(*args, &block)
end
private private
def visit_Arel_Nodes_SelectCore o, collector def visit_Arel_Nodes_SelectCore o, collector
collector << "WHERE " collector << "WHERE "
wheres = o.wheres.map do |where| wheres = o.wheres.map do |where|
Nodes::SqlLiteral.new(@connection.visitor.accept(where, collector.class.new).value) Nodes::SqlLiteral.new(@inner_visitor.accept(where, collector.class.new).value)
end end
inject_join wheres, collector, ' AND ' inject_join wheres, collector, ' AND '