mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Implement proxy_owner, proxy_target and proxy_reflection methods on CollectionProxy with deprecations. Fixes #1148.
This commit is contained in:
parent
c0374999c8
commit
0afd5850f5
3 changed files with 42 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue