Skip to content

Commit

Permalink
Merge remote branch 'sven/master' into foo
Browse files Browse the repository at this point in the history
* sven/master:
  don't leave @options uninitialized on Table
  revert 19c5a95 and add tests
  • Loading branch information
tenderlove committed Aug 30, 2010
2 parents bd89d4b + 3b1b245 commit 026594c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/arel/engines/sql/relations/table.rb
Expand Up @@ -31,7 +31,8 @@ def initialize(name, options = {})
@table_alias = as unless as == @name
end
else
@engine = options # Table.new('foo', engine)
@engine = options # Table.new('foo', engine)
@options = {}
end

if @engine.connection
Expand All @@ -50,6 +51,10 @@ def initialize(name, options = {})
end
end

def table_exists?
@table_exists ||= @@tables.include?(name) || engine.connection.table_exists?(name)
end

def as(table_alias)
Table.new(name, options.merge(:as => table_alias))
end
Expand Down
23 changes: 23 additions & 0 deletions spec/engines/sql/unit/relations/table_spec.rb
Expand Up @@ -118,5 +118,28 @@ module Arel
Table.new(:users, engine = Sql::Engine.new).engine.should == engine
end
end

describe 'lazy evaluation' do
before do
@relation = Table.new(:authors)
ActiveRecord::Base.connection.execute('CREATE TABLE authors (id INTEGER)')
end

after do
ActiveRecord::Base.connection.execute('DROP TABLE authors')
end

describe '#table_exists?' do
it 'detects a table that was created after the model has loaded' do
@relation.table_exists?.should be_true
end
end

describe '#attributes' do
it 'detects attributes from a table that was created after the model has loaded' do
@relation.attributes.should == [Attribute.new(@relation, :id)]
end
end
end
end
end

0 comments on commit 026594c

Please sign in to comment.