Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4372

Show
Ignore:
Timestamp:
05/28/06 21:33:34 (2 years ago)
Author:
rick
Message:

Provide Association Extensions access to the instance that the association is being accessed from. Closes #4433 [josh@hasmanythrough.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4366 r4372  
    11*SVN* 
     2 
     3* Provide Association Extensions access to the instance that the association is being accessed from.   
     4  Closes #4433 [josh@hasmanythrough.com] 
    25 
    36* Update OpenBase adaterp's maintainer's email address. Closes #5176. [Derrick Spell] 
  • trunk/activerecord/lib/active_record/associations.rb

    r4363 r4372  
    232232    #     has_many :people, :extend => [FindOrCreateByNameExtension, FindRecentExtension] 
    233233    #   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. 
    234241    # 
    235242    # === Association Join Models 
  • trunk/activerecord/lib/active_record/associations/association_proxy.rb

    r4206 r4372  
    55      alias_method :proxy_respond_to?, :respond_to? 
    66      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_)/ } 
    88 
    99      def initialize(owner, reflection) 
     
    1111        Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) } 
    1212        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 
    1325      end 
    1426       
  • trunk/activerecord/test/associations_test.rb

    r4357 r4372  
    6363      assert_equal "Natural Company", db["apple"].clients.first.name 
    6464    end 
     65  end 
     66end 
     67 
     68class 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 
    6588  end 
    6689end 
  • trunk/activerecord/test/fixtures/author.rb

    r4325 r4372  
    44  has_many :posts_with_categories, :include => :categories, :class_name => "Post" 
    55  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 
    617  has_many :comments, :through => :posts 
    718  has_many :funky_comments, :through => :posts, :source => :comments