From 53521a9e39b9d8af4165d7703c36dc905f1f8f67 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 21 Jul 2017 09:11:47 -0400 Subject: Refactor `substitute_binds` to perform substitution immediately I'm honestly not sure if replacing bind params with their concrete values is something that belongs in Arel at all, as it's something that will need to be coupled to the quoting mechanism of the caller, and could just be accomplished by using `Quoted` instead. Still, with the new structure we can provide a much simpler API around substitution. The expectation of the quoter responding to `quote` is a reasonably minimal API. I originally used `DelegateClass` here, with the one line override of `add_bind`, but realized that we have some funky code going on where the collector returns the next collector to use (in practice `self` is always returned, and I don't see why we'd ever want to do this). Removing that would likely be worthwhile, but would be a larger refactoring --- lib/arel/collectors/substitute_binds.rb | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'lib/arel/collectors') diff --git a/lib/arel/collectors/substitute_binds.rb b/lib/arel/collectors/substitute_binds.rb index 62589c44e8..99d2215aaa 100644 --- a/lib/arel/collectors/substitute_binds.rb +++ b/lib/arel/collectors/substitute_binds.rb @@ -2,36 +2,27 @@ module Arel module Collectors class SubstituteBinds - def initialize - @parts = [] + def initialize(quoter, delegate_collector) + @quoter = quoter + @delegate = delegate_collector end def << str - @parts << str + delegate << str self end def add_bind bind - @parts << bind - self + self << quoter.quote(bind) end - def value; @parts; end - - def substitute_binds bvs - bvs = bvs.dup - @parts.map do |val| - if Arel::Nodes::BindParam === val - bvs.shift - else - val - end - end + def value + delegate.value end - def compile bvs - substitute_binds(bvs).join - end + protected + + attr_reader :quoter, :delegate end end end -- cgit v1.2.3