diff options
-rw-r--r-- | lib/arel/select_manager.rb | 20 | ||||
-rw-r--r-- | test/test_select_manager.rb | 17 |
2 files changed, 28 insertions, 9 deletions
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index 3e0d4038d7..b4103144fe 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -97,10 +97,8 @@ module Arel self end - def having expr - expr = Nodes::SqlLiteral.new(expr) if String === expr - - @ctx.having = Nodes::Having.new(expr) + def having *exprs + @ctx.having = Nodes::Having.new(collapse(exprs, @ctx.having)) self end @@ -211,16 +209,22 @@ switch to `compile_insert` end private - def collapse exprs - return exprs.first if exprs.length == 1 - - create_and exprs.compact.map { |expr| + def collapse exprs, existing = nil + exprs = exprs.unshift(existing.expr) if existing + exprs = exprs.compact.map { |expr| if String === expr + # FIXME: Don't do this automatically Arel.sql(expr) else expr end } + + if exprs.length == 1 + exprs.first + else + create_and exprs + end end end end diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index 7c8da972e1..271a8ae0b0 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -99,13 +99,28 @@ module Arel end end - describe '#having' do + describe 'having' do it 'converts strings to SQLLiterals' do table = Table.new :users mgr = table.from table mgr.having 'foo' mgr.to_sql.must_be_like %{ SELECT FROM "users" HAVING foo } end + + it 'can have multiple items specified separately' do + table = Table.new :users + mgr = table.from table + mgr.having 'foo' + mgr.having 'bar' + mgr.to_sql.must_be_like %{ SELECT FROM "users" HAVING foo AND bar } + end + + it 'can have multiple items specified together' do + table = Table.new :users + mgr = table.from table + mgr.having 'foo', 'bar' + mgr.to_sql.must_be_like %{ SELECT FROM "users" HAVING foo AND bar } + end end end |