aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-09-12 18:15:21 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-09-12 18:15:21 -0700
commitd4abd35feb84db37815ecd8f0b37f657317105c1 (patch)
treedd1de06baa686e96868284df5534a5eb0bad0e2e
parent53159770271c24a4c0ba6bca6a9e09683157dd9d (diff)
downloadrails-d4abd35feb84db37815ecd8f0b37f657317105c1.tar.gz
rails-d4abd35feb84db37815ecd8f0b37f657317105c1.tar.bz2
rails-d4abd35feb84db37815ecd8f0b37f657317105c1.zip
using table_alias when :as is passed to the constructor
-rw-r--r--lib/arel/table.rb5
-rw-r--r--lib/arel/visitors/to_sql.rb6
-rw-r--r--spec/arel/select_manager_spec.rb9
3 files changed, 19 insertions, 1 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index efdc34ed94..1519efa63b 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -14,6 +14,11 @@ module Arel
@columns = nil
@aliases = []
@table_alias = nil
+
+ # Sometime AR sends an :as parameter to table, to let the table know that
+ # it is an Alias. We may want to override new, and return a TableAlias
+ # node?
+ @table_alias = engine[:as] if Hash === engine
end
def alias
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index be9a62325a..391d2ff1c4 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -139,7 +139,11 @@ module Arel
end
def visit_Arel_Table o
- quote_table_name o.name
+ if o.table_alias
+ "#{quote_table_name o.name} #{quote_table_name o.table_alias}"
+ else
+ quote_table_name o.name
+ end
end
def visit_Arel_Nodes_In o
diff --git a/spec/arel/select_manager_spec.rb b/spec/arel/select_manager_spec.rb
index 86a4daee44..2ed1a851e5 100644
--- a/spec/arel/select_manager_spec.rb
+++ b/spec/arel/select_manager_spec.rb
@@ -49,6 +49,15 @@ module Arel
end
end
+ describe 'initialize' do
+ it 'uses alias in sql' do
+ table = Table.new :users, :engine => Table.engine, :as => 'foo'
+ mgr = table.from table
+ mgr.skip 10
+ mgr.to_sql.should be_like %{ SELECT FROM "users" "foo" OFFSET 10 }
+ end
+ end
+
describe 'skip' do
it 'should add an offset' do
table = Table.new :users