aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-05-28 21:33:34 +0000
committerRick Olson <technoweenie@gmail.com>2006-05-28 21:33:34 +0000
commitea51d72edb36737445834777f06664096066a0ba (patch)
tree34924b8d0a41a652e5cc543e6f5b7f2afc0e1fe7 /activerecord/lib/active_record
parent062845b4da06f7025d6190899cde25deb8eaac42 (diff)
downloadrails-ea51d72edb36737445834777f06664096066a0ba.tar.gz
rails-ea51d72edb36737445834777f06664096066a0ba.tar.bz2
rails-ea51d72edb36737445834777f06664096066a0ba.zip
Provide Association Extensions access to the instance that the association is being accessed from. Closes #4433 [josh@hasmanythrough.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4372 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb7
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb14
2 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index ad28407e57..e1a0289039 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -232,6 +232,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:
+ #
+ # * +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 the collection of associated objects for has_many and has_and_belongs_to_many.
+ #
# === Association Join Models
#
# Has Many associations can be configured with the :through option to use an explicit join model to retrieve the data. This
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index adf7ad6ac6..ce0e154644 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -4,7 +4,7 @@ module ActiveRecord
attr_reader :reflection
alias_method :proxy_respond_to?, :respond_to?
alias_method :proxy_extend, :extend
- instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?|^proxy_respond_to\?|^proxy_extend|^send)/ }
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_)/ }
def initialize(owner, reflection)
@owner, @reflection = owner, reflection
@@ -12,6 +12,18 @@ module ActiveRecord
reset
end
+ def proxy_owner
+ @owner
+ end
+
+ def proxy_reflection
+ @reflection
+ end
+
+ def proxy_target
+ @target
+ end
+
def respond_to?(symbol, include_priv = false)
proxy_respond_to?(symbol, include_priv) || (load_target && @target.respond_to?(symbol, include_priv))
end