aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-02-14 07:24:09 +0000
committerRick Olson <technoweenie@gmail.com>2008-02-14 07:24:09 +0000
commit8a2266c02063c1111c666c71ced7822f095ed56c (patch)
tree1420ac25830d4e1f8da1c1035369b703617f4ed0 /activerecord
parentdfa786631bb8476c8cae555ad8de5e83811dfe9d (diff)
downloadrails-8a2266c02063c1111c666c71ced7822f095ed56c.tar.gz
rails-8a2266c02063c1111c666c71ced7822f095ed56c.tar.bz2
rails-8a2266c02063c1111c666c71ced7822f095ed56c.zip
Improve associations performance by avoiding named block arguments. Closes #11109
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8865 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb24
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb10
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb12
4 files changed, 30 insertions, 18 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 6dcf02c77f..06a22f1dbf 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Improve associations performance by avoiding named block arguments. #11109 [adymo]
+
* Introduce the :readonly option to all associations. Records from the association cannot be saved. #11084 [miloops]
* Multiparameter attributes for time columns fail over to DateTime when out of range of Time [Geoff Buesing]
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 79cd227c0c..d400a5c772 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -43,8 +43,8 @@ module ActiveRecord
end
# Calculate sum using SQL, not Enumerable
- def sum(*args, &block)
- calculate(:sum, *args, &block)
+ def sum(*args)
+ calculate(:sum, *args) { |*block_args| yield(*block_args) if block_given? }
end
# Remove +records+ from this association. Does not destroy +records+.
@@ -121,9 +121,9 @@ module ActiveRecord
size.zero?
end
- def any?(&block)
+ def any?
if block_given?
- method_missing(:any?, &block)
+ method_missing(:any?) { |*block_args| yield(*block_args) if block_given? }
else
!empty?
end
@@ -157,11 +157,13 @@ module ActiveRecord
protected
- def method_missing(method, *args, &block)
+ def method_missing(method, *args)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
- super
+ super { |*block_args| yield(*block_args) if block_given? }
else
- @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) }
+ @reflection.klass.send(:with_scope, construct_scope) {
+ @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) if block_given? }
+ }
end
end
@@ -187,15 +189,15 @@ module ActiveRecord
private
- def create_record(attrs, &block)
+ def create_record(attrs)
ensure_owner_is_not_new
record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) }
- add_record_to_target_with_callbacks(record, &block)
+ add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) if block_given? }
end
- def build_record(attrs, &block)
+ def build_record(attrs)
record = @reflection.klass.new(attrs)
- add_record_to_target_with_callbacks(record, &block)
+ add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) if block_given? }
end
def add_record_to_target_with_callbacks(record)
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index 9fc0d44a01..2f9ba31786 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -77,6 +77,12 @@ module ActiveRecord
@target.inspect
end
+ def to_xml(options={}, &block)
+ if load_target
+ @target.to_xml(options, &block)
+ end
+ end
+
protected
def dependent?
@reflection.options[:dependent]
@@ -120,9 +126,9 @@ module ActiveRecord
end
private
- def method_missing(method, *args, &block)
+ def method_missing(method, *args)
if load_target
- @target.send(method, *args, &block)
+ @target.send(method, *args) { |*block_args| yield(*block_args) if block_given? }
end
end
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 94842f424d..23931bcda2 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -113,8 +113,8 @@ module ActiveRecord
end
# Calculate sum using SQL, not Enumerable
- def sum(*args, &block)
- calculate(:sum, *args, &block)
+ def sum(*args)
+ calculate(:sum, *args) { |*block_args| yield(*block_args) if block_given? }
end
def count(*args)
@@ -128,11 +128,13 @@ module ActiveRecord
end
protected
- def method_missing(method, *args, &block)
+ def method_missing(method, *args)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
- super
+ super { |*block_args| yield(*block_args) if block_given? }
else
- @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) }
+ @reflection.klass.send(:with_scope, construct_scope) {
+ @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) if block_given? }
+ }
end
end