Changeset 4372
- Timestamp:
- 05/28/06 21:33:34 (2 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/association_proxy.rb (modified) (2 diffs)
- trunk/activerecord/test/associations_test.rb (modified) (1 diff)
- trunk/activerecord/test/fixtures/author.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r4366 r4372 1 1 *SVN* 2 3 * Provide Association Extensions access to the instance that the association is being accessed from. 4 Closes #4433 [josh@hasmanythrough.com] 2 5 3 6 * Update OpenBase adaterp's maintainer's email address. Closes #5176. [Derrick Spell] trunk/activerecord/lib/active_record/associations.rb
r4363 r4372 232 232 # has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension] 233 233 # end 234 # 235 # Some extensions can only be made to work with knowledge of the association proxy's internals. 236 # Extensions can access relevant state using accessors on the association proxy: 237 # 238 # * +proxy_owner+ - Returns the object the association is part of. 239 # * +proxy_reflection+ - Returns the reflection object that describes the association. 240 # * +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. 234 241 # 235 242 # === Association Join Models trunk/activerecord/lib/active_record/associations/association_proxy.rb
r4206 r4372 5 5 alias_method :proxy_respond_to?, :respond_to? 6 6 alias_method :proxy_extend, :extend 7 instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\? |^proxy_respond_to\?|^proxy_extend|^send)/ }7 instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_)/ } 8 8 9 9 def initialize(owner, reflection) … … 11 11 Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) } 12 12 reset 13 end 14 15 def proxy_owner 16 @owner 17 end 18 19 def proxy_reflection 20 @reflection 21 end 22 23 def proxy_target 24 @target 13 25 end 14 26 trunk/activerecord/test/associations_test.rb
r4357 r4372 63 63 assert_equal "Natural Company", db["apple"].clients.first.name 64 64 end 65 end 66 end 67 68 class AssociationProxyTest < Test::Unit::TestCase 69 fixtures :authors, :posts 70 71 def test_proxy_accessors 72 welcome = posts(:welcome) 73 assert_equal welcome, welcome.author.proxy_owner 74 assert_equal welcome.class.reflect_on_association(:author), welcome.author.proxy_reflection 75 welcome.author.class # force load target 76 assert_equal welcome.author, welcome.author.proxy_target 77 78 david = authors(:david) 79 assert_equal david, david.posts.proxy_owner 80 assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection 81 david.posts.first # force load target 82 assert_equal david.posts, david.posts.proxy_target 83 84 assert_equal david, david.posts_with_extension.testing_proxy_owner 85 assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection 86 david.posts_with_extension.first # force load target 87 assert_equal david.posts_with_extension, david.posts_with_extension.testing_proxy_target 65 88 end 66 89 end trunk/activerecord/test/fixtures/author.rb
r4325 r4372 4 4 has_many :posts_with_categories, :include => :categories, :class_name => "Post" 5 5 has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" 6 has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension 7 def testing_proxy_owner 8 proxy_owner 9 end 10 def testing_proxy_reflection 11 proxy_reflection 12 end 13 def testing_proxy_target 14 proxy_target 15 end 16 end 6 17 has_many :comments, :through => :posts 7 18 has_many :funky_comments, :through => :posts, :source => :comments