Skip to content

Instantly share code, notes, and snippets.

@tcopeland
Created September 18, 2017 18:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcopeland/9715f83c7cb516209491996bf2442476 to your computer and use it in GitHub Desktop.
Save tcopeland/9715f83c7cb516209491996bf2442476 to your computer and use it in GitHub Desktop.
custom visitor
module Arel
module Visitors
class MyWhereSsqlWithoutSql < Arel::Visitors::ToSql
def initialize(inner_visitor, *args, &block)
@inner_visitor = inner_visitor
super(*args, &block)
end
private
def visit_Arel_Nodes_SelectCore o, collector
collector << ""
wheres = o.wheres.map do |where|
Nodes::SqlLiteral.new(@inner_visitor.accept(where, collector.class.new).value)
end
inject_join wheres, collector, ' AND '
end
end
end
end
module Arel
class SelectManager < Arel::TreeManager
def where_sql_without_where(engine=Table.engine)
return if @ctx.wheres.empty?
viz = Visitors::MyWhereSsqlWithoutSql.new(engine.connection.visitor, engine.connection)
Nodes::SqlLiteral.new viz.accept(@ctx, Collectors::SQLString.new).value
end
end
end
### And then to run it:
>> Lead.where(status: 1).arel.where_sql_without_where
=> "\"leads\".\"status\" = $1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment