Skip to content
This repository
Browse code

refactoring join_sql to return nil if there are no sources

  • Loading branch information...
commit f0bf1cf7627562c87b6717b6443a56e3060dfda3 1 parent f4c12fc
Aaron Patterson tenderlove authored
2  lib/arel/select_manager.rb
@@ -128,6 +128,8 @@ def take limit
128 128 end
129 129
130 130 def join_sql
  131 + return nil unless @ctx.froms
  132 +
131 133 viz = Visitors::JoinSql.new @engine
132 134 Nodes::SqlLiteral.new viz.accept @ctx
133 135 end
21 lib/arel/table.rb
@@ -49,40 +49,39 @@ def joins manager
49 49 end
50 50
51 51 def join relation, klass = Nodes::InnerJoin
52   - return select_manager unless relation
  52 + return from(self) unless relation
53 53
54   - sm = SelectManager.new(@engine)
55 54 case relation
56 55 when String, Nodes::SqlLiteral
57 56 raise if relation.blank?
58   - sm.from Nodes::StringJoin.new(self, relation)
  57 + from Nodes::StringJoin.new(self, relation)
59 58 else
60   - sm.from klass.new(self, relation, nil)
  59 + from klass.new(self, relation, nil)
61 60 end
62 61 end
63 62
64 63 def group *columns
65   - select_manager.group(*columns)
  64 + from(self).group(*columns)
66 65 end
67 66
68 67 def order *expr
69   - select_manager.order(*expr)
  68 + from(self).order(*expr)
70 69 end
71 70
72 71 def where condition
73   - select_manager.where condition
  72 + from(self).where condition
74 73 end
75 74
76 75 def project *things
77   - select_manager.project(*things)
  76 + from(self).project(*things)
78 77 end
79 78
80 79 def take amount
81   - select_manager.take amount
  80 + from(self).take amount
82 81 end
83 82
84 83 def having expr
85   - select_manager.having expr
  84 + from(self).having expr
86 85 end
87 86
88 87 def columns
@@ -98,7 +97,7 @@ def [] name
98 97 end
99 98
100 99 def select_manager
101   - SelectManager.new(@engine, self)
  100 + SelectManager.new(@engine)
102 101 end
103 102
104 103 private
5 spec/select_manager_spec.rb
@@ -299,6 +299,11 @@ def execute sql, name = nil, *args
299 299 manager.join_sql.should be_like %{ 'hello' }
300 300 check manager.joins(manager).should == manager.join_sql
301 301 end
  302 +
  303 + it 'returns nil join sql' do
  304 + manager = Arel::SelectManager.new Table.engine
  305 + manager.join_sql.should be_nil
  306 + end
302 307 end
303 308
304 309 describe 'order_clauses' do
7 spec/table_spec.rb
@@ -12,6 +12,13 @@ module Arel
12 12 end
13 13 end
14 14
  15 + describe 'select_manager' do
  16 + it 'should return an empty select manager' do
  17 + sm = @relation.select_manager
  18 + sm.to_sql.should be_like 'SELECT'
  19 + end
  20 + end
  21 +
15 22 describe 'having' do
16 23 it 'adds a having clause' do
17 24 mgr = @relation.having @relation[:id].eq(10)

0 comments on commit f0bf1cf

Please sign in to comment.
Something went wrong with that request. Please try again.