diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-05-19 23:28:44 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-05-19 23:28:44 +0100 |
commit | 0afd5850f5bba4bd3f1218e27a4a1488c9fdb83e (patch) | |
tree | a5444cdbf2b70d9883ef66a751461b386738eedd | |
parent | c0374999c8656e02db38ab8198af76a411669a53 (diff) | |
download | rails-0afd5850f5bba4bd3f1218e27a4a1488c9fdb83e.tar.gz rails-0afd5850f5bba4bd3f1218e27a4a1488c9fdb83e.tar.bz2 rails-0afd5850f5bba4bd3f1218e27a4a1488c9fdb83e.zip |
Implement proxy_owner, proxy_target and proxy_reflection methods on CollectionProxy with deprecations. Fixes #1148.
-rw-r--r-- | activerecord/lib/active_record/associations.rb | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 24 | ||||
-rw-r--r-- | activerecord/test/cases/associations_test.rb | 12 |
3 files changed, 42 insertions, 5 deletions
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 diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 49d82ba2df..38d439d68a 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -203,6 +203,18 @@ class AssociationProxyTest < ActiveRecord::TestCase assert_equal david.projects, david.projects.reload.reload end end + + # Tests that proxy_owner, proxy_target and proxy_reflection are implement as deprecated methods + def test_proxy_deprecations + david = developers(:david) + david.projects.load_target + + [:owner, :target, :reflection].each do |name| + assert_deprecated do + assert_equal david.association(:projects).send(name), david.projects.send("proxy_#{name}") + end + end + end end class OverridingAssociationsTest < ActiveRecord::TestCase |