diff options
author | Michael Koziarski <michael@koziarski.com> | 2008-02-29 23:16:53 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-02-29 23:16:53 +0000 |
commit | 13a60b83a470fb90f91a073b0540f7b02aa11ebd (patch) | |
tree | 4bc3008ab6fd9ed5066b12d8ece47c1aa4f70db0 /activerecord/lib | |
parent | 7dcd0d7d967c613f4e447ba6330e1f616b93c1a3 (diff) | |
download | rails-13a60b83a470fb90f91a073b0540f7b02aa11ebd.tar.gz rails-13a60b83a470fb90f91a073b0540f7b02aa11ebd.tar.bz2 rails-13a60b83a470fb90f91a073b0540f7b02aa11ebd.zip |
Improve performance by avoiding named block arguments. Closes #11109 [adymo]
Reapplies [8865] with some fixes
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8957 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
3 files changed, 58 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index c993eb07fd..28c257a7e4 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -43,8 +43,12 @@ module ActiveRecord end # Calculate sum using SQL, not Enumerable - def sum(*args, &block) - calculate(:sum, *args, &block) + def sum(*args) + if block_given? + calculate(:sum, *args) { |*block_args| yield(*block_args) } + else + calculate(:sum, *args) + end end # Remove +records+ from this association. Does not destroy +records+. @@ -121,9 +125,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) } else !empty? end @@ -157,11 +161,21 @@ 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 + if block_given? + super { |*block_args| yield(*block_args) } + else + super + end else - @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) } + @reflection.klass.send(:with_scope, construct_scope) do + if block_given? + @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } + else + @reflection.klass.send(method, *args) + end + end end end @@ -187,15 +201,23 @@ 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) + if block_given? + add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } + else + add_record_to_target_with_callbacks(record) + end end - def build_record(attrs, &block) + def build_record(attrs) record = @reflection.klass.new(attrs) - add_record_to_target_with_callbacks(record, &block) + if block_given? + add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } + else + add_record_to_target_with_callbacks(record) + end 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..adfc4610d5 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -120,9 +120,13 @@ module ActiveRecord end private - def method_missing(method, *args, &block) + def method_missing(method, *args) if load_target - @target.send(method, *args, &block) + if block_given? + @target.send(method, *args) { |*block_args| yield(*block_args) } + else + @target.send(method, *args) + end 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 19112216aa..72bf693384 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,12 @@ module ActiveRecord end # Calculate sum using SQL, not Enumerable - def sum(*args, &block) - calculate(:sum, *args, &block) + def sum(*args) + if block_given? + calculate(:sum, *args) { |*block_args| yield(*block_args) } + else + calculate(:sum, *args) + end end def count(*args) @@ -128,11 +132,21 @@ 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 + if block_given? + super { |*block_args| yield(*block_args) } + else + super + end else - @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.send(method, *args, &block) } + @reflection.klass.send(:with_scope, construct_scope) do + if block_given? + @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) } + else + @reflection.klass.send(method, *args) + end + end end end |