aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/relation.rb
diff options
context:
space:
mode:
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