diff options
-rw-r--r-- | lib/arel/relations/alias.rb | 12 | ||||
-rw-r--r-- | lib/arel/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 5 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 9 | ||||
-rw-r--r-- | spec/arel/unit/relations/alias_spec.rb | 6 | ||||
-rw-r--r-- | spec/arel/unit/relations/join_spec.rb | 11 | ||||
-rw-r--r-- | spec/arel/unit/relations/relation_spec.rb | 24 |
7 files changed, 29 insertions, 40 deletions
diff --git a/lib/arel/relations/alias.rb b/lib/arel/relations/alias.rb index 2dab52515f..329f94638e 100644 --- a/lib/arel/relations/alias.rb +++ b/lib/arel/relations/alias.rb @@ -2,18 +2,12 @@ module Arel class Alias < Compound attr_reader :alias - def initialize(relation, aliaz) - @relation, @alias = relation, aliaz - end - - def alias? - true + def initialize(relation) + @relation = relation end def ==(other) - self.class == other.class and - relation == other.relation and - @alias == other.alias + self.equal? other end end end
\ No newline at end of file diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 4ffac6d1c3..ae848433be 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -5,7 +5,7 @@ module Arel hash_on :relation delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :taken, - :skipped, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, + :skipped, :name, :alias, :aggregation?, :prefix_for, :column_for, :engine, :to => :relation diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index ce236d79d0..6c12a0ea26 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -48,6 +48,11 @@ module Arel [relation1.joins, relation2.joins, this_join].compact.join(" ") end + # FIXME + def name + 'user' + end + def selects (externalize(relation1).selects + externalize(relation2).selects).collect { |s| s.bind(self) } end diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 0a5bf2ff24..3453b9adc6 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -52,8 +52,8 @@ module Arel attributes.all?(&:blank?) ? self : Projection.new(self, *attributes) end - def as(aliaz = nil) - aliaz.blank?? self : Alias.new(self, aliaz) + def alias + Alias.new(self) end def order(*attributes) @@ -100,10 +100,6 @@ module Arel false end - def alias? - false - end - def relation_for(attribute) self[attribute] and self end @@ -166,6 +162,5 @@ module Arel def joins; nil end def taken; nil end def skipped; nil end - def alias; nil end end end
\ No newline at end of file diff --git a/spec/arel/unit/relations/alias_spec.rb b/spec/arel/unit/relations/alias_spec.rb index 420a91c806..25dbf70668 100644 --- a/spec/arel/unit/relations/alias_spec.rb +++ b/spec/arel/unit/relations/alias_spec.rb @@ -4,12 +4,12 @@ module Arel describe Alias do before do @relation = Table.new(:users) - @alias_relation = Alias.new(@relation, :foo) end - describe '#alias' do + describe '==' do it "returns the alias" do - @alias_relation.alias.should == :foo + Alias.new(@relation).should_not == Alias.new(@relation) + (aliaz = Alias.new(@relation)).should == aliaz end end end diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index b14d951aae..c3f33e1ae7 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -129,7 +129,7 @@ module Arel describe 'when joining a relation to itself' do before do - @aliased_relation = @relation1.as(:alias) + @aliased_relation = @relation1.alias @predicate = @relation1[:id].eq(@aliased_relation[:id]) end @@ -145,6 +145,13 @@ module Arel describe 'when joining the same relation to itself' do describe '#to_sql' do + it '' do + @relation1 \ + .join(@relation1.alias.join(@relation1.alias).on(@relation1[:id].eq(1))) \ + .on(@relation1[:id].eq(1)) \ + .to_sql.should be_like("") + end + it 'aliases the table and attributes properly in the join predicate and the where clause' do @relation1.join(@aliased_relation).on(@relation1[:id].eq(@aliased_relation[:id])).to_sql.should be_like(" SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name` @@ -173,7 +180,7 @@ module Arel SELECT `users`.`id`, `users`.`name` FROM `users` INNER JOIN asdf ON fdsa - ") + ") end end end diff --git a/spec/arel/unit/relations/relation_spec.rb b/spec/arel/unit/relations/relation_spec.rb index d6874a1554..3b8be55c7d 100644 --- a/spec/arel/unit/relations/relation_spec.rb +++ b/spec/arel/unit/relations/relation_spec.rb @@ -30,12 +30,6 @@ module Arel @relation.should_not be_aggregation end end - - describe '#alias?' do - it "returns false" do - @relation.should_not be_alias - end - end end describe Relation::Operations do @@ -86,31 +80,25 @@ module Arel end end - describe '#as' do + describe '#alias' do it "manufactures an alias relation" do - @relation.as(:paul).should == Alias.new(@relation, :paul) - end - - describe 'when given a blank alias' do - it 'returns self' do - @relation.as.should == @relation - end + @relation.alias.relation.should == Alias.new(@relation).relation end end - + describe '#select' do before do @predicate = Equality.new(@attribute1, @attribute2) end - + it "manufactures a selection relation" do @relation.select(@predicate).should == Selection.new(@relation, @predicate) end - + it "accepts arbitrary strings" do @relation.select("arbitrary").should == Selection.new(@relation, "arbitrary") end - + describe 'when given a blank predicate' do it 'returns self' do @relation.select.should == @relation |