mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
constructor can take column info
This commit is contained in:
parent
741b879577
commit
dd425e1258
2 changed files with 26 additions and 8 deletions
|
@ -10,16 +10,20 @@ module Arel
|
||||||
def initialize name, engine = Table.engine
|
def initialize name, engine = Table.engine
|
||||||
@name = name
|
@name = name
|
||||||
@engine = engine
|
@engine = engine
|
||||||
@engine = engine[:engine] if Hash === engine
|
|
||||||
@columns = nil
|
@columns = nil
|
||||||
@aliases = []
|
@aliases = []
|
||||||
@table_alias = nil
|
@table_alias = nil
|
||||||
@primary_key = nil
|
@primary_key = nil
|
||||||
|
|
||||||
# Sometime AR sends an :as parameter to table, to let the table know that
|
if Hash === engine
|
||||||
# it is an Alias. We may want to override new, and return a TableAlias
|
@engine = engine[:engine] || Table.engine
|
||||||
# node?
|
@columns = attributes_for engine[:columns]
|
||||||
@table_alias = engine[:as] if Hash === engine
|
|
||||||
|
# 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
|
end
|
||||||
|
|
||||||
def primary_key
|
def primary_key
|
||||||
|
@ -82,9 +86,8 @@ module Arel
|
||||||
end
|
end
|
||||||
|
|
||||||
def columns
|
def columns
|
||||||
@columns ||= @engine.connection.columns(@name, "#{@name} Columns").map do |column|
|
@columns ||=
|
||||||
Attributes.for(column).new self, column.name.to_sym, column
|
attributes_for @engine.connection.columns(@name, "#{@name} Columns")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def [] name
|
def [] name
|
||||||
|
@ -95,6 +98,14 @@ module Arel
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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?
|
def table_exists?
|
||||||
@table_exists ||= tables.key?(@name) || engine.connection.table_exists?(name)
|
@table_exists ||= tables.key?(@name) || engine.connection.table_exists?(name)
|
||||||
end
|
end
|
||||||
|
|
|
@ -71,6 +71,13 @@ module Arel
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'new' do
|
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
|
it 'should accept an engine' do
|
||||||
rel = Table.new :users, 'foo'
|
rel = Table.new :users, 'foo'
|
||||||
check rel.engine.should == 'foo'
|
check rel.engine.should == 'foo'
|
||||||
|
|
Loading…
Reference in a new issue