aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-09-18 11:33:07 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-09-18 11:33:07 -0700
commitdfa520b9ab08522e10978f2ee3006ba42db418ba (patch)
tree15596813cb582bf2ec616018075c0957b68d92b7
parent25e93e817eb9d2a038e556aa5a3a16c2beb94dd2 (diff)
downloadrails-dfa520b9ab08522e10978f2ee3006ba42db418ba.tar.gz
rails-dfa520b9ab08522e10978f2ee3006ba42db418ba.tar.bz2
rails-dfa520b9ab08522e10978f2ee3006ba42db418ba.zip
returning nil for tables that do not exist
-rw-r--r--lib/arel/table.rb16
-rw-r--r--spec/arel/table_spec.rb26
2 files changed, 21 insertions, 21 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index 06bbe7b99e..d23245691b 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -88,8 +88,24 @@ module Arel
end
def [] name
+ return nil unless table_exists?
+
name = name.to_sym
columns.find { |column| column.name == name }
end
+
+ private
+ def table_exists?
+ @table_exists ||= tables.key?(@name) || engine.connection.table_exists?(name)
+ end
+
+ def tables
+ self.class.table_cache(@engine)
+ end
+
+ @@table_cache = nil
+ def self.table_cache engine # :nodoc:
+ @@table_cache ||= Hash[engine.connection.tables.map { |x| [x,true] }]
+ end
end
end
diff --git a/spec/arel/table_spec.rb b/spec/arel/table_spec.rb
index 5b68040aac..28fc95dec5 100644
--- a/spec/arel/table_spec.rb
+++ b/spec/arel/table_spec.rb
@@ -147,27 +147,11 @@ module Arel
end
end
- ### FIXME: this seems like a bad requirement.
- #describe 'when given an', Attribute do
- # it "returns the attribute if the attribute is within the relation" do
- # @relation[@relation[:id]].should == @relation[:id]
- # end
-
- # it "returns nil if the attribtue is not within the relation" do
- # another_relation = Table.new(:photos)
- # @relation[another_relation[:id]].should be_nil
- # end
- #end
-
- #describe 'when given an', Expression do
- # before do
- # @expression = @relation[:id].count
- # end
-
- # it "returns the Expression if the Expression is within the relation" do
- # @relation[@expression].should be_nil
- # end
- #end
+ describe 'when table does not exist' do
+ it 'returns nil' do
+ @relation[:foooo].should be_nil
+ end
+ end
end
end
end