aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/relation.rb
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-03 15:43:16 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-03 15:43:16 -0800
commit6c73e3dbc714a9752a66a6da51e7e41f372797b3 (patch)
tree55571262b97fc7a91114af78d04f231b1c3e3892 /lib/active_relation/relations/relation.rb
parent9c4403f67a8f2ac40fc0c964d8c24ecc6c6d7f27 (diff)
downloadrails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.tar.gz
rails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.tar.bz2
rails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.zip
i know it doesn't work but need to anchor here...
Diffstat (limited to 'lib/active_relation/relations/relation.rb')
-rw-r--r--lib/active_relation/relations/relation.rb46
1 files changed, 32 insertions, 14 deletions
diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb
index d09ee058ef..838697a2ac 100644
--- a/lib/active_relation/relations/relation.rb
+++ b/lib/active_relation/relations/relation.rb
@@ -26,10 +26,14 @@ module ActiveRelation
def [](index)
case index
- when Symbol
- attribute(index)
+ when Symbol, String
+ attribute_for_name(index)
when ::Range
Range.new(self, index)
+ when Attribute
+ attribute_for_attribute(index)
+ when Expression
+ attribute_for_expression(index)
end
end
@@ -65,8 +69,8 @@ module ActiveRelation
Deletion.new(self)
end
- def group(*attributes)
- Group.new(self, *attributes)
+ def aggregate(*expressions)
+ AggregateOperation.new(self, expressions)
end
JoinOperation = Struct.new(:join_sql, :relation1, :relation2) do
@@ -74,6 +78,12 @@ module ActiveRelation
Join.new(join_sql, relation1, relation2, *predicates)
end
end
+
+ AggregateOperation = Struct.new(:relation, :expressions) do
+ def group(*groupings)
+ Aggregation.new(relation, :expressions => expressions, :groupings => groupings)
+ end
+ end
end
include Operations
@@ -83,7 +93,7 @@ module ActiveRelation
def to_sql(strategy = Sql::Select.new)
strategy.select [
- "SELECT #{projections.collect{ |a| a.to_sql(Sql::Projection.new) }.join(', ')}",
+ "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new) }.join(', ')}",
"FROM #{table_sql}",
(joins unless joins.blank?),
("WHERE #{selects.collect{|s| s.to_sql(Sql::Predicate.new)}.join("\n\tAND ")}" unless selects.blank?),
@@ -99,15 +109,23 @@ module ActiveRelation
def connection
ActiveRecord::Base.connection
end
+
+ def attribute_for_attribute(attribute)
+ self == attribute.relation ? attribute : nil
+ end
+
+ def attribute_for_expression(expression)
+ nil
+ end
- def projections; [] end
- def selects; [] end
- def orders; [] end
- def inserts; [] end
- def groupings; [] end
- def joins; nil end
- def limit; nil end
- def offset; nil end
- def alias; nil end
+ def attributes; [] end
+ def selects; [] end
+ def orders; [] end
+ def inserts; [] end
+ def groupings; [] end
+ def joins; nil end
+ def limit; nil end
+ def offset; nil end
+ def alias; nil end
end
end \ No newline at end of file