From 7dc03cfd768b730933c5d4331b8202237f2fe10f Mon Sep 17 00:00:00 2001 From: Jorge Bejar Date: Thu, 12 Apr 2012 00:41:25 -0300 Subject: Define array methods in ActiveRecord::Associations::CollectionProxy if they are not defined or delegated. In this way, we have a performance boost invoking some array methods which are not defined in CollectionAssociation. --- .../lib/active_record/associations/collection_proxy.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 5eda0387c4..44cbf473a1 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -126,6 +126,19 @@ module ActiveRecord proxy_association.reload self end + + # Define array public methods because we know it should be invoked over + # the target, so we can have a performance improvement using those methods + # in association collections + Array.public_instance_methods.each do |m| + unless method_defined?(m) + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{m}(*args, &block) + target.public_send(:#{m}, *args, &block) if load_target + end + RUBY + end + end end end end -- cgit v1.2.3