Browse files

Added automatic incrementing of table alias names

  • Loading branch information...
1 parent 2b27e65 commit 38070119340c81d3a2937a43de9f783e60eadf60 ozzyaaron committed Mar 31, 2011
Showing with 28 additions and 1 deletion.
  1. +1 −1 lib/arel/table.rb
  2. +27 −0 test/test_table.rb
View
2 lib/arel/table.rb
@@ -42,7 +42,7 @@ def primary_key
end
end
- def alias name = "#{self.name}_2"
+ def alias name = "#{self.name}_#{@aliases.length + 2}"
Nodes::TableAlias.new(name, self).tap do |node|
@aliases << node
end
View
27 test/test_table.rb
@@ -101,6 +101,33 @@ module Arel
node.name.must_equal 'users_2'
node[:id].relation.must_equal node
end
+
+ describe 'when given a alias name' do
+ it 'will give the alias the provided name' do
+ @relation.aliases.must_equal []
+
+ node = @relation.alias("test_alias")
+ @relation.aliases.must_equal [node]
+ node.name.must_equal 'test_alias'
+ node[:id].relation.must_equal node
+ end
+ end
+
+ describe 'when not given an alias name' do
+ it 'should increment alias names' do
+ @relation.aliases.must_equal []
+
+ node = @relation.alias
+ @relation.aliases.must_equal [node]
+ node.name.must_equal 'users_2'
+ node[:id].relation.must_equal node
+
+ node_2 = @relation.alias
+ @relation.aliases.must_equal [node, node_2]
+ node_2.name.must_equal 'users_3'
+ node_2[:id].relation.must_equal node_2
+ end
+ end
end
describe 'new' do

0 comments on commit 3807011

Please sign in to comment.