From 8a2266c02063c1111c666c71ced7822f095ed56c Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 14 Feb 2008 07:24:09 +0000 Subject: 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 --- .../associations/association_collection.rb | 24 ++++++++++++---------- .../associations/association_proxy.rb | 10 +++++++-- .../associations/has_many_through_association.rb | 12 ++++++----- 3 files changed, 28 insertions(+), 18 deletions(-) (limited to 'activerecord/lib/active_record') 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 -- cgit v1.2.3