aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-12 23:36:28 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-12 23:36:28 -0800
commiteecab85b331c6bb716978262b418ab02ff4a22e9 (patch)
treef0f5be2e5961a2b884bd4258be8010d3d6dfd6be
parent7c7044085617a66abb3f9bd37dc7c072701f03c7 (diff)
downloadrails-eecab85b331c6bb716978262b418ab02ff4a22e9.tar.gz
rails-eecab85b331c6bb716978262b418ab02ff4a22e9.tar.bz2
rails-eecab85b331c6bb716978262b418ab02ff4a22e9.zip
experimenting with new aliasing terminology and interface
-rw-r--r--lib/active_relation/primitives/aggregation.rb2
-rw-r--r--lib/active_relation/relations/alias.rb4
-rw-r--r--lib/active_relation/relations/base.rb2
-rw-r--r--lib/active_relation/relations/rename.rb12
-rw-r--r--spec/active_relation/relations/rename_spec.rb10
5 files changed, 13 insertions, 17 deletions
diff --git a/lib/active_relation/primitives/aggregation.rb b/lib/active_relation/primitives/aggregation.rb
index a3c7747165..403c03fab5 100644
--- a/lib/active_relation/primitives/aggregation.rb
+++ b/lib/active_relation/primitives/aggregation.rb
@@ -10,7 +10,7 @@ module ActiveRelation
end
def to_sql(options = {})
- "#{function_sql}(#{@attribute.to_sql})"
+ "#{function_sql}(#{attribute.to_sql})"
end
def ==(other)
diff --git a/lib/active_relation/relations/alias.rb b/lib/active_relation/relations/alias.rb
index 97950ea5b3..5f4a10a672 100644
--- a/lib/active_relation/relations/alias.rb
+++ b/lib/active_relation/relations/alias.rb
@@ -10,10 +10,6 @@ module ActiveRelation
def ==(other)
relation == other.relation and self.alias == other.alias
end
-
- def to_sql(options = {})
- super + " AS #{@alias}"
- end
end
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/base.rb b/lib/active_relation/relations/base.rb
index b4654f8f4b..ea3404e7af 100644
--- a/lib/active_relation/relations/base.rb
+++ b/lib/active_relation/relations/base.rb
@@ -88,7 +88,7 @@ module ActiveRelation
("LIMIT #{limit.to_sql}" unless limit.blank?),
("OFFSET #{offset.to_sql}" unless offset.blank?)
].compact.join("\n")
- options[:use_parens] ? "(#{sql})" : sql
+ (options[:use_parens] ? "(#{sql})" : sql) + (self.alias ? " AS #{self.alias}" : "")
end
alias_method :to_s, :to_sql
diff --git a/lib/active_relation/relations/rename.rb b/lib/active_relation/relations/rename.rb
index cff042dbc6..563b28922c 100644
--- a/lib/active_relation/relations/rename.rb
+++ b/lib/active_relation/relations/rename.rb
@@ -1,15 +1,15 @@
module ActiveRelation
module Relations
class Rename < Compound
- attr_reader :schmattribute, :alias
+ attr_reader :schmattribute, :rename
def initialize(relation, renames)
- @schmattribute, @alias = renames.shift
+ @schmattribute, @rename = renames.shift
@relation = renames.empty?? relation : Rename.new(relation, renames)
end
def ==(other)
- relation == other.relation and schmattribute == other.schmattribute and self.alias == other.alias
+ relation == other.relation and schmattribute == other.schmattribute and self.rename == other.rename
end
def attributes
@@ -17,13 +17,13 @@ module ActiveRelation
end
def qualify
- Rename.new(relation.qualify, schmattribute.qualify => self.alias)
+ Rename.new(relation.qualify, schmattribute.qualify => self.rename)
end
protected
def attribute(name)
case
- when name == self.alias then schmattribute.as(self.alias)
+ when name == self.rename then schmattribute.as(self.rename)
when relation[name] == schmattribute then nil
else relation[name]
end
@@ -31,7 +31,7 @@ module ActiveRelation
private
def substitute(a)
- a == schmattribute ? a.as(self.alias) : a
+ a == schmattribute ? a.as(self.rename) : a
end
end
end
diff --git a/spec/active_relation/relations/rename_spec.rb b/spec/active_relation/relations/rename_spec.rb
index 76b7069190..bdc5c97492 100644
--- a/spec/active_relation/relations/rename_spec.rb
+++ b/spec/active_relation/relations/rename_spec.rb
@@ -12,26 +12,26 @@ describe ActiveRelation::Relations::Rename do
should == ActiveRelation::Relations::Rename.new(ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :humpty), @relation[:name] => :dumpty)
end
- it "raises an exception if the alias provided is already used" do
+ it "raises an exception if the rename provided is already used" do
pending
end
end
describe '==' do
- it "obtains if the relation, attribute, and alias are identical" do
+ it "obtains if the relation, attribute, and rename are identical" do
pending
end
end
describe '#attributes' do
- it "manufactures a list of attributes with the renamed attribute aliased" do
+ it "manufactures a list of attributes with the renamed attribute renameed" do
ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :schmid).attributes.should ==
(@relation.attributes - [@relation[:id]]) + [@relation[:id].as(:schmid)]
end
end
describe '[]' do
- it 'indexes attributes by alias' do
+ it 'indexes attributes by rename' do
@renamed_relation[:id].should be_nil
@renamed_relation[:schmid].should == @relation[:id].as(:schmid)
end
@@ -51,7 +51,7 @@ describe ActiveRelation::Relations::Rename do
end
describe '#to_sql' do
- it 'manufactures sql aliasing the attribute' do
+ it 'manufactures sql renameing the attribute' do
@renamed_relation.to_sql.should be_like("""
SELECT `foo`.`name`, `foo`.`id` AS 'schmid'
FROM `foo`