diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-11 16:37:16 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-11 16:37:16 -0700 |
commit | bd6adc877af4687aec636f08e96e19785eea209d (patch) | |
tree | e61b114c5ade9d091611974ed1694370c0d54c57 | |
parent | a1a46fd8c2a3d53e02e2e58ca575b2596646b2c3 (diff) | |
parent | d93a05eec1adc5ca9d752a1e0b33313fc092c3e7 (diff) | |
download | rails-bd6adc877af4687aec636f08e96e19785eea209d.tar.gz rails-bd6adc877af4687aec636f08e96e19785eea209d.tar.bz2 rails-bd6adc877af4687aec636f08e96e19785eea209d.zip |
Merge pull request #204 from trptcolin/visitor_dispatch_caching
Cache visitor dispatch on a per-visitor basis
-rw-r--r-- | lib/arel/visitors/visitor.rb | 9 | ||||
-rw-r--r-- | test/visitors/test_dispatch_contamination.rb | 22 |
2 files changed, 28 insertions, 3 deletions
diff --git a/lib/arel/visitors/visitor.rb b/lib/arel/visitors/visitor.rb index 204657883f..33ec5eafb4 100644 --- a/lib/arel/visitors/visitor.rb +++ b/lib/arel/visitors/visitor.rb @@ -7,12 +7,15 @@ module Arel private - DISPATCH = Hash.new do |hash, klass| - hash[klass] = "visit_#{(klass.name || '').gsub('::', '_')}" + DISPATCH = Hash.new do |hash, visitor_class| + hash[visitor_class] = + Hash.new do |hash, node_class| + hash[node_class] = "visit_#{(node_class.name || '').gsub('::', '_')}" + end end def dispatch - DISPATCH + DISPATCH[self.class] end def visit object, attribute = nil diff --git a/test/visitors/test_dispatch_contamination.rb b/test/visitors/test_dispatch_contamination.rb new file mode 100644 index 0000000000..d3c9e8af2e --- /dev/null +++ b/test/visitors/test_dispatch_contamination.rb @@ -0,0 +1,22 @@ +require 'helper' + +module Arel + module Visitors + describe 'avoiding contamination between visitor dispatch tables' do + before do + @connection = Table.engine.connection + @table = Table.new(:users) + end + + it 'dispatches properly after failing upwards' do + node = Nodes::Union.new(Nodes::True.new, Nodes::False.new) + assert_equal "( TRUE UNION FALSE )", node.to_sql + + node.first # from Nodes::Node's Enumerable mixin + + assert_equal "( TRUE UNION FALSE )", node.to_sql + end + end + end +end + |