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

Ticket #6822: association_callbacks_fix.diff

File association_callbacks_fix.diff, 4.4 kB (added by stopdropandrew, 10 months ago)

New patch w/ additional tests

  • test/associations_test.rb

    old new  
    7272    end 
    7373  end 
    7474end 
    75  
     75  
    7676class AssociationProxyTest < Test::Unit::TestCase 
    7777  fixtures :authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects 
    78  
     78   
    7979  def test_proxy_accessors 
    8080    welcome = posts(:welcome) 
    8181    assert_equal  welcome, welcome.author.proxy_owner 
    8282    assert_equal  welcome.class.reflect_on_association(:author), welcome.author.proxy_reflection 
    8383    welcome.author.class  # force load target 
    8484    assert_equal  welcome.author, welcome.author.proxy_target 
    85  
     85   
    8686    david = authors(:david) 
    8787    assert_equal  david, david.posts.proxy_owner 
    8888    assert_equal  david.class.reflect_on_association(:posts), david.posts.proxy_reflection 
    8989    david.posts.first   # force load target 
    9090    assert_equal  david.posts, david.posts.proxy_target 
    91  
     91   
    9292    assert_equal  david, david.posts_with_extension.testing_proxy_owner 
    9393    assert_equal  david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection 
    9494    david.posts_with_extension.first   # force load target 
     
    9898  def test_push_does_not_load_target 
    9999    david = authors(:david) 
    100100 
     101    david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!")) 
     102    assert !david.posts.loaded? 
     103    assert david.posts.include?(post) 
     104  end 
     105 
     106  def test_push_has_many_through_does_not_load_target 
     107    david = authors(:david) 
     108 
    101109    david.categories << categories(:technology) 
    102110    assert !david.categories.loaded? 
    103111    assert david.categories.include?(categories(:technology)) 
    104112  end 
     113   
     114  def test_push_followed_by_save_does_not_load_target 
     115    david = authors(:david) 
    105116 
     117    david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!")) 
     118    assert !david.posts.loaded? 
     119    david.save 
     120    assert !david.posts.loaded? 
     121    assert david.posts.include?(post) 
     122  end 
     123 
    106124  def test_push_does_not_lose_additions_to_new_record 
    107125    josh = Author.new(:name => "Josh") 
    108126    josh.posts << Post.new(:title => "New on Edge", :body => "More cool stuff!") 
     
    772790    assert companies(:first_firm).save 
    773791    assert_equal 3, companies(:first_firm).clients_of_firm(true).size 
    774792  end 
    775  
     793   
     794  def test_build_followed_by_save_does_not_load_target 
     795    new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client") 
     796    assert companies(:first_firm).save 
     797    assert !companies(:first_firm).clients_of_firm.loaded? 
     798  end 
     799   
    776800  def test_build_without_loading_association 
    777801    first_topic = topics(:first) 
    778802    Reply.column_names 
     
    825849    assert_equal 3, companies(:first_firm).clients_of_firm(true).size 
    826850  end 
    827851 
     852  def test_create_followed_by_save_does_not_load_target 
     853    new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") 
     854    assert companies(:first_firm).save 
     855    assert !companies(:first_firm).clients_of_firm.loaded? 
     856  end 
     857 
    828858  def test_find_or_initialize 
    829859    the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client") 
    830860    assert_equal companies(:first_firm).id, the_client.firm_id 
  • lib/active_record/associations.rb

    old new  
    10661066            if association.respond_to?(:loaded?) 
    10671067              if new_record? 
    10681068                association 
     1069              elsif association.loaded? 
     1070                association.select { |record| record.new_record? } 
    10691071              else 
    1070                 association.select { |record| record.new_record? } 
     1072                association.target.select { |record| record.new_record? } 
    10711073              end.each do |record| 
    10721074                errors.add "#{association_name}" unless record.valid? 
    10731075              end 
     
    10841086              association 
    10851087            elsif association.respond_to?(:loaded?) && association.loaded? 
    10861088              association.select { |record| record.new_record? } 
     1089            elsif association.respond_to?(:loaded?) && !association.loaded? 
     1090              association.target.select { |record| record.new_record? } 
    10871091            else 
    10881092              [] 
    10891093            end