Class: Immunio::ArelNodeVisitor

Inherits:
Arel::Visitors::Visitor
  • Object
show all
Defined in:
lib/immunio/plugins/active_record.rb

Overview

Arel AST visitor to collect params and modifiers. Based on Arel::Visitors::DepthFirst.

Whenever a query is built in Rails, a tree of Ruby objects (AST) is built to represent that query. Arel (the engine building this tree) uses a Visitor to build the SQL statement represented by tree. We use the same base class as Arel, but instead of building some SQL, we track the params and modifiers.

See en.wikipedia.org/wiki/Visitor_pattern

Constant Summary collapse

VISITABLES =

Only accepts statements to avoid duplicates in params if branches are visited multiple times.

[Arel::Nodes::SelectStatement, Arel::Nodes::InsertStatement,
Arel::Nodes::UpdateStatement, Arel::Nodes::DeleteStatement].freeze
IGNORED_EXPRESSIONS =
["", "*", "1 AS one"].freeze
DISPATCH =

Copied from Arel::Visitors::Visitor to ensure the cached dispatch table isn’t shared with other Arel visitors under Rails 3.2.

Hash.new do |hash, klass|
  hash[klass] = "visit_#{(klass.name || '').gsub('::', '_')}"
end

Instance Method Summary collapse

Constructor Details

#initialize(connection_id) ⇒ ArelNodeVisitor

Returns a new instance of ArelNodeVisitor.



133
134
135
136
# File 'lib/immunio/plugins/active_record.rb', line 133

def initialize(connection_id)
  @connection_id = connection_id
  super()
end

Instance Method Details

#accept(object) ⇒ Object

Entry point into the visitor.



139
140
141
142
143
# File 'lib/immunio/plugins/active_record.rb', line 139

def accept(object)
  if VISITABLES.include?(object.class)
    visit object, {}
  end
end

#dispatchObject



145
146
147
# File 'lib/immunio/plugins/active_record.rb', line 145

def dispatch
  DISPATCH
end