1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

refactoring join_sql to return nil if there are no sources

This commit is contained in:
Aaron Patterson 2010-09-28 16:57:33 -07:00
parent f4c12fcc4a
commit f0bf1cf762
4 changed files with 24 additions and 11 deletions

View file

@ -128,6 +128,8 @@ module Arel
end
def join_sql
return nil unless @ctx.froms
viz = Visitors::JoinSql.new @engine
Nodes::SqlLiteral.new viz.accept @ctx
end

View file

@ -49,40 +49,39 @@ module Arel
end
def join relation, klass = Nodes::InnerJoin
return select_manager unless relation
return from(self) unless relation
sm = SelectManager.new(@engine)
case relation
when String, Nodes::SqlLiteral
raise if relation.blank?
sm.from Nodes::StringJoin.new(self, relation)
from Nodes::StringJoin.new(self, relation)
else
sm.from klass.new(self, relation, nil)
from klass.new(self, relation, nil)
end
end
def group *columns
select_manager.group(*columns)
from(self).group(*columns)
end
def order *expr
select_manager.order(*expr)
from(self).order(*expr)
end
def where condition
select_manager.where condition
from(self).where condition
end
def project *things
select_manager.project(*things)
from(self).project(*things)
end
def take amount
select_manager.take amount
from(self).take amount
end
def having expr
select_manager.having expr
from(self).having expr
end
def columns
@ -98,7 +97,7 @@ module Arel
end
def select_manager
SelectManager.new(@engine, self)
SelectManager.new(@engine)
end
private

View file

@ -299,6 +299,11 @@ module Arel
manager.join_sql.should be_like %{ 'hello' }
check manager.joins(manager).should == manager.join_sql
end
it 'returns nil join sql' do
manager = Arel::SelectManager.new Table.engine
manager.join_sql.should be_nil
end
end
describe 'order_clauses' do

View file

@ -12,6 +12,13 @@ module Arel
end
end
describe 'select_manager' do
it 'should return an empty select manager' do
sm = @relation.select_manager
sm.to_sql.should be_like 'SELECT'
end
end
describe 'having' do
it 'adds a having clause' do
mgr = @relation.having @relation[:id].eq(10)