Skip to content

Commit

Permalink
Arel: add the possibility to instantiate directly a TreeManager with …
Browse files Browse the repository at this point in the history
…an ast
  • Loading branch information
jlestavel committed Feb 28, 2022
1 parent d6e6686 commit f44c784
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/arel/delete_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class DeleteManager < Arel::TreeManager
include TreeManager::StatementMethods

def initialize(table = nil)
@ast = Nodes::DeleteStatement.new(table)
super(Nodes::DeleteStatement.new(table))
end

def from(relation)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/arel/insert_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Arel # :nodoc: all
class InsertManager < Arel::TreeManager
def initialize(table = nil)
@ast = Nodes::InsertStatement.new(table)
super(Nodes::InsertStatement.new(table))
end

def into(table)
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/arel/select_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class SelectManager < Arel::TreeManager
STRING_OR_SYMBOL_CLASS = [Symbol, String]

def initialize(table = nil)
@ast = Nodes::SelectStatement.new(table)
@ctx = @ast.cores.last
super(Nodes::SelectStatement.new(table))
@ctx = ast.cores.last
end

def initialize_copy(other)
Expand Down
13 changes: 8 additions & 5 deletions activerecord/lib/arel/tree_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def where(expr)

attr_reader :ast

def initialize(ast)
@ast = ast
end

def initialize_copy(other)
super(other.clone)
end

def to_dot
collector = Arel::Collectors::PlainString.new
collector = Visitors::Dot.new.accept @ast, collector
Expand All @@ -51,10 +59,5 @@ def to_sql(engine = Table.engine)
collector = engine.connection.visitor.accept @ast, collector
collector.value
end

def initialize_copy(other)
super
@ast = @ast.clone
end
end
end
2 changes: 1 addition & 1 deletion activerecord/lib/arel/update_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UpdateManager < Arel::TreeManager
include TreeManager::StatementMethods

def initialize(table = nil)
@ast = Nodes::UpdateStatement.new(table)
super(Nodes::UpdateStatement.new(table))
end

###
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/arel/tree_manager_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require_relative "helper"

module Arel
class TreeManagerTest < Arel::Spec
describe "to_sql" do
it "generates SQL" do
users_table = Table.new(:users)
tree_manager = Arel::TreeManager.new(users_table[:first_name])
assert_equal('"users"."first_name"', tree_manager.to_sql)
end
end
end
end

0 comments on commit f44c784

Please sign in to comment.