aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-07-21 20:21:03 -0300
committerEmilio Tagua <miloops@gmail.com>2009-07-21 20:21:03 -0300
commit0e0866e0565bd530ee265b9298accff4185f7022 (patch)
tree80bb65ebb7567999ca3de72cec47fc31fd2e3969 /activerecord/lib/active_record
parentf32c3709830eb8d9f68a59c94f6791621c2b52ac (diff)
downloadrails-0e0866e0565bd530ee265b9298accff4185f7022.tar.gz
rails-0e0866e0565bd530ee265b9298accff4185f7022.tar.bz2
rails-0e0866e0565bd530ee265b9298accff4185f7022.zip
Introduced ActiveRecord::Relation, a layer between an ARel relation and an AR relation
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb17
-rwxr-xr-xactiverecord/lib/active_record/base.rb56
-rw-r--r--activerecord/lib/active_record/relation.rb39
3 files changed, 67 insertions, 45 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 47f97718eb..a0aeff68b6 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1602,16 +1602,17 @@ module ActiveRecord
conditions = construct_conditions(options[:conditions], scope) || ''
conditions << construct_limited_ids_condition(conditions, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
- arel_table((scope && scope[:from]) || options[:from])
- join(joins)
- where(conditions)
- project(column_aliases(join_dependency))
- group(construct_group(options[:group], options[:having], scope))
- order(construct_order(options[:order], scope))
+ relation = arel_table((scope && scope[:from]) || options[:from]).
+ join(joins).
+ where(conditions).
+ project(column_aliases(join_dependency)).
+ group(construct_group(options[:group], options[:having], scope)).
+ order(construct_order(options[:order], scope)
+ )
- take(construct_limit(options[:limit], scope)) if using_limitable_reflections?(join_dependency.reflections)
+ relation = relation.take(construct_limit(options[:limit], scope)) if using_limitable_reflections?(join_dependency.reflections)
- return sanitize_sql(arel_relation.to_sql)
+ return sanitize_sql(relation.to_sql)
end
def construct_limited_ids_condition(where, options, join_dependency)
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 85f7cbfad2..718f7ea37b 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -663,7 +663,8 @@ module ActiveRecord #:nodoc:
# This is an alias for find(:all). You can pass in all the same arguments to this method as you can
# to find(:all)
def all(*args)
- find(:all, *args)
+ relation = arel_table
+ construct_finder_arel(*args)
end
# Executes a custom SQL query against your database and returns all the results. The results will
@@ -861,22 +862,21 @@ module ActiveRecord #:nodoc:
def update_all(updates, conditions = nil, options = {})
scope = scope(:find)
- arel_table
+ relation = arel_table.relation
if conditions = construct_conditions(conditions, scope)
- where(Arel::SqlLiteral.new(conditions))
+ relation = relation.where(Arel::SqlLiteral.new(conditions))
end
if options.has_key?(:limit) || (scope && scope[:limit])
# Only take order from scope if limit is also provided by scope, this
# is useful for updating a has_many association with a limit.
- order(construct_order(options[:order], scope))
- take(construct_limit(options[:limit], scope))
+ relation = relation.order(construct_order(options[:order], scope)).take(construct_limit(options[:limit], scope))
else
- order(construct_order(options[:order], nil))
+ relation = relation.order(construct_order(options[:order], nil))
end
- arel_relation.update(sanitize_sql_for_assignment(updates))
+ relation.update(sanitize_sql_for_assignment(updates))
end
# Destroys the records matching +conditions+ by instantiating each
@@ -1536,25 +1536,7 @@ module ActiveRecord #:nodoc:
def arel_table(table = nil)
table = table_name if table.blank?
- self.arel_relation = Arel::Table.new(table)
- end
-
- def arel_relation
- Thread.current[:"#{self}_arel_relation"] ||= Arel::Table.new(table_name)
- end
-
- def arel_relation=(relation)
- Thread.current[:"#{self}_arel_relation"] = relation
- end
-
- CLAUSES_METHODS = ["where", "join", "project", "group", "order", "take", "skip"].freeze
-
- for clause in CLAUSES_METHODS
- class_eval %{
- def #{clause}(_#{clause})
- self.arel_relation = self.arel_relation.#{clause}(_#{clause}) if _#{clause}
- end
- }
+ Relation.new(self, table)
end
private
@@ -1736,21 +1718,21 @@ module ActiveRecord #:nodoc:
end
end
- def construct_finder_arel(options, scope = scope(:find))
+ def construct_finder_arel(options = {}, scope = scope(:find))
# TODO add lock to Arel
- arel_table(options[:from])
- join(construct_join(options[:joins], scope))
- where(construct_conditions(options[:conditions], scope))
- project(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins])))
- group(construct_group(options[:group], options[:having], scope))
- order(construct_order(options[:order], scope))
- take(construct_limit(options[:limit], scope))
- skip(construct_offset(options[:offset], scope))
- arel_relation
+ arel_table(options[:from]).
+ join(construct_join(options[:joins], scope)).
+ where(construct_conditions(options[:conditions], scope)).
+ project(options[:select] || (scope && scope[:select]) || default_select(options[:joins] || (scope && scope[:joins]))).
+ group(construct_group(options[:group], options[:having], scope)).
+ order(construct_order(options[:order], scope)).
+ take(construct_limit(options[:limit], scope)).
+ skip(construct_offset(options[:offset], scope)
+ )
end
def construct_finder_sql(options, scope = scope(:find))
- construct_finder_arel(options, scope).to_sql
+ construct_finder_arel(options, scope).relation.to_sql
end
def construct_join(joins, scope)
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
new file mode 100644
index 0000000000..36e8d98298
--- /dev/null
+++ b/activerecord/lib/active_record/relation.rb
@@ -0,0 +1,39 @@
+module ActiveRecord
+ class Relation
+ delegate :delete, :to_sql, :to => :relation
+ CLAUSES_METHODS = ["where", "join", "project", "group", "order", "take", "skip"].freeze
+ attr_reader :relation, :klass
+
+ def initialize(klass, table = nil)
+ @klass = klass
+ @relation = Arel::Table.new(table || @klass.table_name)
+ end
+
+ def to_a
+ @klass.find_by_sql(@relation.to_sql)
+ end
+
+ def first
+ @relation = @relation.take(1)
+ to_a.first
+ end
+
+ for clause in CLAUSES_METHODS
+ class_eval %{
+ def #{clause}(_#{clause})
+ @relation = @relation.#{clause}(_#{clause}) if _#{clause}
+ self
+ end
+ }
+ end
+
+ private
+ def method_missing(method, *args, &block)
+ if @relation.respond_to?(method)
+ @relation.send(method, *args, &block)
+ elsif Array.instance_methods.include?(method.to_s)
+ to_a.send(method, *args, &block)
+ end
+ end
+ end
+end