diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 40a605c4fac7a3f02134dff8403fb7c06a009504..739dbbcb10679639cb2cc3081f1c349f1365da12 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -47,14 +47,29 @@ module Associations # instantiation of the actual post records. class AssociationProxy #:nodoc: alias_method :proxy_respond_to?, :respond_to? - alias_method :proxy_extend, :extend delegate :to_param, :to => :proxy_target instance_methods.each { |m| undef_method m unless m.to_s =~ /^(?:nil\?|send|object_id)$|^__|^respond_to_missing|proxy_/ } + def self.new(owner, reflection) + klass = + reflection.cached_extend_class ||= + if reflection.options[:extend] + const_name = "AR_CACHED_EXTEND_CLASS_#{reflection.name}_#{reflection.options[:extend].join("_").gsub("::","_")}" + reflection.active_record.const_set(const_name, Class.new(self) do + include *reflection.options[:extend] + end) + else + self + end + + proxy = klass.allocate + proxy.send(:initialize, owner, reflection) + proxy + end + def initialize(owner, reflection) @owner, @reflection = owner, reflection reflection.check_validity! - Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) } reset end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 13bcb8b1afb67bae9f45ac36085665b49562ec48..b3a81f728381126edc2a8b0f0c4b86c8305efed6 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -77,6 +77,7 @@ def reflect_on_all_autosave_associations # those classes. Objects of AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods. class MacroReflection attr_reader :active_record + attr_accessor :cached_extend_class def initialize(macro, name, options, active_record) @macro, @name, @options, @active_record = macro, name, options, active_record