From 0859779d6f1cadfec12f1112c80382658466ab7d Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 16 Mar 2006 02:10:11 +0000 Subject: Allow :dependent options to be used with polymorphic joins. #3820 [Rick Olson] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3881 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_join_model_test.rb | 74 ++++++++++++++++++++++- activerecord/test/fixtures/post.rb | 2 +- activerecord/test/fixtures/taggings.yml | 6 ++ 3 files changed, 79 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index b14693d7a6..2d74895664 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -69,13 +69,72 @@ class AssociationsJoinModelTest < Test::Unit::TestCase end def test_create_polymorphic_has_many_with_scope - tagging = posts(:welcome).taggings.create(:tag => tags(:general)) + old_count = posts(:welcome).taggings.count + tagging = posts(:welcome).taggings.create(:tag => tags(:misc)) assert_equal "Post", tagging.taggable_type + assert_equal old_count+1, posts(:welcome).taggings.count end def test_create_polymorphic_has_one_with_scope - tagging = posts(:welcome).tagging.create(:tag => tags(:general)) + old_count = Tagging.count + tagging = posts(:welcome).tagging.create(:tag => tags(:misc)) assert_equal "Post", tagging.taggable_type + assert_equal old_count+1, Tagging.count + end + + def test_delete_polymorphic_has_many_with_delete_all + assert_equal 1, posts(:welcome).taggings.count + posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyDeleteAll' + post = find_post_with_dependency(1, :has_many, :taggings, :delete_all) + + old_count = Tagging.count + post.destroy + assert_equal old_count-1, Tagging.count + assert_equal 0, posts(:welcome).taggings.count + end + + def test_delete_polymorphic_has_many_with_destroy + assert_equal 1, posts(:welcome).taggings.count + posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyDestroy' + post = find_post_with_dependency(1, :has_many, :taggings, :destroy) + + old_count = Tagging.count + post.destroy + assert_equal old_count-1, Tagging.count + assert_equal 0, posts(:welcome).taggings.count + end + + def test_delete_polymorphic_has_many_with_nullify + assert_equal 1, posts(:welcome).taggings.count + posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyNullify' + post = find_post_with_dependency(1, :has_many, :taggings, :nullify) + + old_count = Tagging.count + post.destroy + assert_equal old_count, Tagging.count + assert_equal 0, posts(:welcome).taggings.count + end + + def test_delete_polymorphic_has_one_with_destroy + assert posts(:welcome).tagging + posts(:welcome).tagging.update_attribute :taggable_type, 'PostWithHasOneDestroy' + post = find_post_with_dependency(1, :has_one, :tagging, :destroy) + + old_count = Tagging.count + post.destroy + assert_equal old_count-1, Tagging.count + assert_nil posts(:welcome).tagging(true) + end + + def test_delete_polymorphic_has_one_with_nullify + assert posts(:welcome).tagging + posts(:welcome).tagging.update_attribute :taggable_type, 'PostWithHasOneNullify' + post = find_post_with_dependency(1, :has_one, :tagging, :nullify) + + old_count = Tagging.count + post.destroy + assert_equal old_count, Tagging.count + assert_nil posts(:welcome).tagging(true) end def test_has_many_with_piggyback @@ -139,4 +198,15 @@ class AssociationsJoinModelTest < Test::Unit::TestCase tagging.destroy assert posts(:welcome, :reload)[:taggings_count].zero? end + + private + # create dynamic Post models to allow different dependency options + def find_post_with_dependency(post_id, association, association_name, dependency) + class_name = "PostWith#{association.to_s.classify}#{dependency.to_s.classify}" + Post.find(post_id).update_attribute :type, class_name + klass = Object.const_set(class_name, Class.new(ActiveRecord::Base)) + klass.set_table_name 'posts' + klass.send(association, association_name, :as => :taggable, :dependent => dependency) + klass.find(post_id) + end end diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb index 7c1f8e1961..e5b95aff32 100644 --- a/activerecord/test/fixtures/post.rb +++ b/activerecord/test/fixtures/post.rb @@ -18,7 +18,7 @@ class Post < ActiveRecord::Base has_many :special_comments has_and_belongs_to_many :categories - has_and_belongs_to_many :special_categories, :join_table => "categories_posts" + has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id' has_many :taggings, :as => :taggable has_many :tags, :through => :taggings diff --git a/activerecord/test/fixtures/taggings.yml b/activerecord/test/fixtures/taggings.yml index dced625580..38ec1ba5b1 100644 --- a/activerecord/test/fixtures/taggings.yml +++ b/activerecord/test/fixtures/taggings.yml @@ -9,3 +9,9 @@ thinking_general: tag_id: 1 taggable_id: 2 taggable_type: Post + +fake: + id: 3 + tag_id: 1 + taggable_id: 1 + taggable_type: FakeModel \ No newline at end of file -- cgit v1.2.3