aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-09-21 13:50:53 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-09-21 13:50:53 -0700
commitdd425e1258b8480aa8e28e52edea7ef4c9c6f55f (patch)
tree2762077693ff328512eb65f4d80afdaa95c4f3ef
parent741b8795779d24057ffb6f98f736f4f9bf00b49b (diff)
downloadrails-dd425e1258b8480aa8e28e52edea7ef4c9c6f55f.tar.gz
rails-dd425e1258b8480aa8e28e52edea7ef4c9c6f55f.tar.bz2
rails-dd425e1258b8480aa8e28e52edea7ef4c9c6f55f.zip
constructor can take column info
-rw-r--r--lib/arel/table.rb27
-rw-r--r--spec/arel/table_spec.rb7
2 files changed, 26 insertions, 8 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index d23245691b..a1243f582b 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -10,16 +10,20 @@ module Arel
def initialize name, engine = Table.engine
@name = name
@engine = engine
- @engine = engine[:engine] if Hash === engine
@columns = nil
@aliases = []
@table_alias = nil
@primary_key = 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
+ if Hash === engine
+ @engine = engine[:engine] || Table.engine
+ @columns = attributes_for engine[:columns]
+
+ # 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]
+ end
end
def primary_key
@@ -82,9 +86,8 @@ module Arel
end
def columns
- @columns ||= @engine.connection.columns(@name, "#{@name} Columns").map do |column|
- Attributes.for(column).new self, column.name.to_sym, column
- end
+ @columns ||=
+ attributes_for @engine.connection.columns(@name, "#{@name} Columns")
end
def [] name
@@ -95,6 +98,14 @@ module Arel
end
private
+ def attributes_for columns
+ return nil unless columns
+
+ columns.map do |column|
+ Attributes.for(column).new self, column.name.to_sym, column
+ end
+ end
+
def table_exists?
@table_exists ||= tables.key?(@name) || engine.connection.table_exists?(name)
end
diff --git a/spec/arel/table_spec.rb b/spec/arel/table_spec.rb
index 28fc95dec5..5c8cfac012 100644
--- a/spec/arel/table_spec.rb
+++ b/spec/arel/table_spec.rb
@@ -71,6 +71,13 @@ module Arel
end
describe 'new' do
+ it 'takes :columns' do
+ columns = Table.engine.connection.columns("users")
+ @relation = Table.new(:users, :columns => columns)
+ check @relation.columns.first.name.should == :id
+ check @relation.engine.should == Table.engine
+ end
+
it 'should accept an engine' do
rel = Table.new :users, 'foo'
check rel.engine.should == 'foo'