diff options
-rw-r--r-- | doc/TODO | 3 | ||||
-rw-r--r-- | lib/active_relation/relations.rb | 1 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 14 | ||||
-rw-r--r-- | lib/active_relation/relations/rename.rb | 26 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/relation_spec.rb | 61 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/rename_spec.rb | 50 |
6 files changed, 56 insertions, 99 deletions
@@ -1,6 +1,6 @@ todo: -- test Value, in particular bind. - rename ActiveRelation to arel +- investigate linq vocabularity - test blank checks in relation.rb - mock out database - standardize quoting @@ -64,6 +64,7 @@ done: :joins=>"INNER JOIN posts ON comments.post_id = posts.id" - finish pending tests - test relation, table reset +- test Value, in particular bind. icebox: - #bind in Attribute and Expression should be doing a descend? diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb index 240c20736e..9cede2a6d1 100644 --- a/lib/active_relation/relations.rb +++ b/lib/active_relation/relations.rb @@ -10,7 +10,6 @@ require 'active_relation/relations/selection' require 'active_relation/relations/order' require 'active_relation/relations/take' require 'active_relation/relations/skip' -require 'active_relation/relations/rename' require 'active_relation/relations/deletion' require 'active_relation/relations/insertion' require 'active_relation/relations/update' diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index f5f2809724..b62a5d9fb9 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -18,12 +18,14 @@ module ActiveRelation include Enumerable module Operations - def join(other) + def join(other = nil) case other when String Join.new(other, self) when Relation JoinOperation.new("INNER JOIN", self, other) + else + self end end @@ -48,7 +50,7 @@ module ActiveRelation attributes.all?(&:blank?) ? self : Projection.new(self, *attributes) end - def as(aliaz) + def as(aliaz = nil) aliaz.blank?? self : Alias.new(self, aliaz) end @@ -56,18 +58,14 @@ module ActiveRelation attributes.all?(&:blank?) ? self : Order.new(self, *attributes) end - def take(taken) + def take(taken = nil) taken.blank?? self : Take.new(self, taken) end - def skip(skipped) + def skip(skipped = nil) skipped.blank?? self : Skip.new(self, skipped) end - def rename(attribute, aliaz) - Rename.new(self, attribute => aliaz) - end - def aggregate(*expressions) AggregateOperation.new(self, expressions) end diff --git a/lib/active_relation/relations/rename.rb b/lib/active_relation/relations/rename.rb deleted file mode 100644 index 9ab07707a0..0000000000 --- a/lib/active_relation/relations/rename.rb +++ /dev/null @@ -1,26 +0,0 @@ -module ActiveRelation - class Rename < Compound - attr_reader :attribute, :pseudonym - - def initialize(relation, pseudonyms) - @attribute, @pseudonym = pseudonyms.shift - @relation = pseudonyms.empty?? relation : Rename.new(relation, pseudonyms) - end - - def ==(other) - self.class == other.class and - relation == other.relation and - attribute == other.attribute and - pseudonym == other.pseudonym - end - - def attributes - relation.attributes.collect(&method(:christen)) - end - - private - def christen(attribute) - (attribute =~ self.attribute ? attribute.as(pseudonym) : attribute).bind(self) rescue nil - end - end -end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/relation_spec.rb b/spec/active_relation/unit/relations/relation_spec.rb index fa66352c91..2b62c8db5e 100644 --- a/spec/active_relation/unit/relations/relation_spec.rb +++ b/spec/active_relation/unit/relations/relation_spec.rb @@ -57,6 +57,12 @@ module ActiveRelation @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) end end + + describe "when given something blank" do + it "returns self" do + @relation.join.should == @relation + end + end end describe '#outer_join' do @@ -72,17 +78,23 @@ module ActiveRelation @relation.project(@attribute1, @attribute2). \ should == Projection.new(@relation, @attribute1, @attribute2) end + + describe "when given blank attributes" do + it "returns self" do + @relation.project.should == @relation + end + end end describe '#as' do it "manufactures an alias relation" do @relation.as(:paul).should == Alias.new(@relation, :paul) end - end - - describe '#rename' do - it "manufactures a rename relation" do - @relation.rename(@attribute1, :users).should == Rename.new(@relation, @attribute1 => :users) + + describe 'when given a blank alias' do + it 'returns self' do + @relation.as.should == @relation + end end end @@ -98,34 +110,50 @@ module ActiveRelation 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 + end + end end describe '#order' do it "manufactures an order relation" do @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) end + + describe 'when given a blank ordering' do + it 'returns self' do + @relation.order.should == @relation + end + end end describe '#take' do it "manufactures a take relation" do @relation.take(5).should == Take.new(@relation, 5) end + + describe 'when given a blank number of items' do + it 'returns self' do + @relation.take.should == @relation + end + end end describe '#skip' do it "manufactures a skip relation" do @relation.skip(4).should == Skip.new(@relation, 4) end - end - - describe '#call' do - it 'executes a select_all on the connection' do - mock(connection = Object.new).select_all(@relation.to_sql) - @relation.call(connection) + + describe 'when given a blank number of items' do + it 'returns self' do + @relation.skip.should == @relation + end end end - - + describe '#aggregate' do before do @expression1 = @attribute1.sum @@ -179,5 +207,12 @@ module ActiveRelation @relation.first.should == @relation.session.read(@relation).first end end + + describe '#call' do + it 'executes a select_all on the connection' do + mock(connection = Object.new).select_all(@relation.to_sql) + @relation.call(connection) + end + end end end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/rename_spec.rb b/spec/active_relation/unit/relations/rename_spec.rb deleted file mode 100644 index 192c819848..0000000000 --- a/spec/active_relation/unit/relations/rename_spec.rb +++ /dev/null @@ -1,50 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module ActiveRelation - describe Rename do - before do - @relation = Table.new(:users) - end - - describe '#initialize' do - it "manufactures nested rename relations if multiple renames are provided" do - Rename.new(@relation, @relation[:id] => :humpty, @relation[:name] => :dumpty). \ - should == Rename.new(Rename.new(@relation, @relation[:name] => :dumpty), @relation[:id] => :humpty) - end - end - - describe '==' do - before do - @another_relation = Table.new(:photos) - end - - it "obtains if the relation, attribute, and rename are identical" do - Rename.new(@relation, @relation[:id] => :humpty).should == Rename.new(@relation, @relation[:id] => :humpty) - Rename.new(@relation, @relation[:id] => :humpty).should_not == Rename.new(@relation, @relation[:id] => :dumpty) - Rename.new(@relation, @relation[:id] => :humpty).should_not == Rename.new(@another_relation, @relation[:id] => :humpty) - end - end - - describe '#attributes' do - before do - @renamed_relation = Rename.new(@relation, @relation[:id] => :schmid) - end - - it "manufactures a list of attributes with the renamed attribute renameed" do - @renamed_relation.attributes.should include(@relation[:id].as(:schmid).bind(@renamed_relation)) - @renamed_relation.attributes.should_not include(@relation[:id].bind(@renamed_relation)) - @renamed_relation.attributes.should include(@relation[:name].bind(@renamed_relation)) - @renamed_relation.should have(@relation.attributes.size).attributes - end - end - - describe '#to_sql' do - it 'manufactures sql renaming the attribute' do - Rename.new(@relation, @relation[:id] => :schmid).to_sql.should be_like(" - SELECT `users`.`id` AS 'schmid', `users`.`name` - FROM `users` - ") - end - end - end -end
\ No newline at end of file |