From 0afd5850f5bba4bd3f1218e27a4a1488c9fdb83e Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 19 May 2011 23:28:44 +0100 Subject: Implement proxy_owner, proxy_target and proxy_reflection methods on CollectionProxy with deprecations. Fixes #1148. --- activerecord/lib/active_record/associations.rb | 11 +++++----- .../active_record/associations/collection_proxy.rb | 24 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 9bc44e5163..a017c1aa1e 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -457,12 +457,13 @@ module ActiveRecord # has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension] # end # - # Some extensions can only be made to work with knowledge of the association proxy's internals. - # Extensions can access relevant state using accessors on the association proxy: + # Some extensions can only be made to work with knowledge of the association's internals. + # Extensions can access relevant state using the following methods (where 'items' is the + # name of the association): # - # * +proxy_owner+ - Returns the object the association is part of. - # * +proxy_reflection+ - Returns the reflection object that describes the association. - # * +proxy_target+ - Returns the associated object for +belongs_to+ and +has_one+, or + # * +record.association(:items).owner+ - Returns the object the association is part of. + # * +record.association(:items).reflection+ - Returns the reflection object that describes the association. + # * +record.association(:items).target+ - Returns the associated object for +belongs_to+ and +has_one+, or # the collection of associated objects for +has_many+ and +has_and_belongs_to_many+. # # === Association Join Models diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index adfc71d435..8415942a1a 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -123,6 +123,30 @@ module ActiveRecord method_missing(:new, *args, &block) end end + + def proxy_owner + ActiveSupport::Deprecation.warn( + "Calling record.#{@association.reflection.name}.proxy_owner is deprecated. Please use " \ + "record.association(:#{@association.reflection.name}).owner instead." + ) + @association.owner + end + + def proxy_target + ActiveSupport::Deprecation.warn( + "Calling record.#{@association.reflection.name}.proxy_target is deprecated. Please use " \ + "record.association(:#{@association.reflection.name}).target instead." + ) + @association.target + end + + def proxy_reflection + ActiveSupport::Deprecation.warn( + "Calling record.#{@association.reflection.name}.proxy_reflection is deprecated. Please use " \ + "record.association(:#{@association.reflection.name}).reflection instead." + ) + @association.reflection + end end end end -- cgit v1.2.3