diff options
-rw-r--r-- | lib/active_relation/relations/nil.rb | 7 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 7 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/join_spec.rb | 4 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/relation_spec.rb | 27 |
4 files changed, 34 insertions, 11 deletions
diff --git a/lib/active_relation/relations/nil.rb b/lib/active_relation/relations/nil.rb index b063db92c9..289ba834d8 100644 --- a/lib/active_relation/relations/nil.rb +++ b/lib/active_relation/relations/nil.rb @@ -1,5 +1,12 @@ module ActiveRelation class Nil < Relation def table_sql; '' end + + def to_s; '' end + + def ==(other) + self.class == other.class + end end + end
\ No newline at end of file diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index eebfd48e30..1b80494cd7 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -19,7 +19,12 @@ module ActiveRelation module Operations def join(other) - JoinOperation.new("INNER JOIN", self, other) + case other + when String + Join.new(other, self) + when Relation + JoinOperation.new("INNER JOIN", self, other) + end end def outer_join(other) diff --git a/spec/active_relation/unit/relations/join_spec.rb b/spec/active_relation/unit/relations/join_spec.rb index b274da7565..423e513be4 100644 --- a/spec/active_relation/unit/relations/join_spec.rb +++ b/spec/active_relation/unit/relations/join_spec.rb @@ -161,10 +161,10 @@ module ActiveRelation describe 'when joining with a string' do it "passes the string through to the where clause" do - Join.new("INNER JOIN ON asdf", @relation1).to_sql.should be_like(" + Join.new("INNER JOIN asdf ON fdsa", @relation1).to_sql.should be_like(" SELECT `users`.`id`, `users`.`name` FROM `users` - INNER JOIN ON asdf + INNER JOIN asdf ON fdsa ") end end diff --git a/spec/active_relation/unit/relations/relation_spec.rb b/spec/active_relation/unit/relations/relation_spec.rb index 682ec88f10..70bf87271a 100644 --- a/spec/active_relation/unit/relations/relation_spec.rb +++ b/spec/active_relation/unit/relations/relation_spec.rb @@ -51,24 +51,35 @@ module ActiveRelation end describe '#join' do - it "manufactures an inner join operation between those two relations" do - @relation.join(@relation).on(@predicate).should == Join.new("INNER JOIN", @relation, @relation, @predicate) + describe 'when given a relation' do + it "manufactures an inner join operation between those two relations" do + @relation.join(@relation).on(@predicate). \ + should == Join.new("INNER JOIN", @relation, @relation, @predicate) + end + end + + describe "when given a string" do + it "manufactures a join operation with the string passed through" do + @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) + end end end - + describe '#outer_join' do it "manufactures a left outer join operation between those two relations" do - @relation.outer_join(@relation).on(@predicate).should == Join.new("LEFT OUTER JOIN", @relation, @relation, @predicate) - end + @relation.outer_join(@relation).on(@predicate). \ + should == Join.new("LEFT OUTER JOIN", @relation, @relation, @predicate) + end end end - + describe '#project' do it "manufactures a projection relation" do - @relation.project(@attribute1, @attribute2).should == Projection.new(@relation, @attribute1, @attribute2) + @relation.project(@attribute1, @attribute2). \ + should == Projection.new(@relation, @attribute1, @attribute2) end end - + describe '#as' do it "manufactures an alias relation" do @relation.as(:paul).should == Alias.new(@relation, :paul) |