aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/relation.rb
diff options
context:
space:
mode:
authorBryan Helmkamp <bryan@brynary.com>2008-01-14 10:50:46 -0500
committerBryan Helmkamp <bryan@brynary.com>2008-01-14 10:50:46 -0500
commit553eb0ad490abc7f85d9836c3ba959ab771d3cf4 (patch)
treebaa9714465488d77980e8d252a82849b32844d3b /lib/active_relation/relations/relation.rb
parent17a5fd13bc4ba8405d95e90d12b87dcd7e5bea5b (diff)
downloadrails-553eb0ad490abc7f85d9836c3ba959ab771d3cf4.tar.gz
rails-553eb0ad490abc7f85d9836c3ba959ab771d3cf4.tar.bz2
rails-553eb0ad490abc7f85d9836c3ba959ab771d3cf4.zip
Remove ActiveRelation sub-modules and refactor specs
Diffstat (limited to 'lib/active_relation/relations/relation.rb')
-rw-r--r--lib/active_relation/relations/relation.rb103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb
new file mode 100644
index 0000000000..663450e69c
--- /dev/null
+++ b/lib/active_relation/relations/relation.rb
@@ -0,0 +1,103 @@
+module ActiveRelation
+ class Relation
+ include Sql::Quoting
+
+ module Iteration
+ include Enumerable
+
+ def each(&block)
+ connection.select_all(to_s).each(&block)
+ end
+
+ def first
+ connection.select_one(to_s)
+ end
+ end
+ include Iteration
+
+ module Operations
+ def join(other)
+ JoinOperation.new("INNER JOIN", self, other)
+ end
+
+ def outer_join(other)
+ JoinOperation.new("LEFT OUTER JOIN", self, other)
+ end
+
+ def [](index)
+ case index
+ when Symbol
+ attribute(index)
+ when ::Range
+ Range.new(self, index)
+ end
+ end
+
+ def include?(attribute)
+ RelationInclusion.new(attribute, self)
+ end
+
+ def select(*predicates)
+ Selection.new(self, *predicates)
+ end
+
+ def project(*attributes)
+ Projection.new(self, *attributes)
+ end
+
+ def as(aliaz)
+ Alias.new(self, aliaz)
+ end
+
+ def order(*attributes)
+ Order.new(self, *attributes)
+ end
+
+ def rename(attribute, aliaz)
+ Rename.new(self, attribute => aliaz)
+ end
+
+ def insert(record)
+ Insertion.new(self, record)
+ end
+
+ def delete
+ Deletion.new(self)
+ end
+
+ JoinOperation = Struct.new(:join_sql, :relation1, :relation2) do
+ def on(*predicates)
+ Join.new(join_sql, relation1, relation2, *predicates)
+ end
+ end
+ end
+ include Operations
+
+ def to_sql(strategy = Sql::Select.new)
+ strategy.select [
+ "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?),
+ ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank?),
+ ("LIMIT #{limit.to_sql}" unless limit.blank?),
+ ("OFFSET #{offset.to_sql}" unless offset.blank?)
+ ].compact.join("\n")
+ end
+ alias_method :to_s, :to_sql
+
+ protected
+ def connection
+ ActiveRecord::Base.connection
+ end
+
+ def attributes; [] end
+ def selects; [] end
+ def orders; [] end
+ def inserts; [] 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