From 9eaf626200929df42532a38f762695ecc94d48f0 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Wed, 28 Jul 2010 16:09:09 -0400 Subject: [PATCH] added test cases for #2765 --- .../test/cases/autosave_association_test.rb | 11 +++++++++++ activerecord/test/models/author.rb | 13 +++++++++++++ activerecord/test/models/etch.rb | 3 +++ activerecord/test/models/scribbling.rb | 5 +++++ activerecord/test/schema/schema.rb | 15 +++++++++++++++ 5 files changed, 47 insertions(+), 0 deletions(-) create mode 100644 activerecord/test/models/etch.rb create mode 100644 activerecord/test/models/scribbling.rb diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 49e7147..bc26d01 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -17,6 +17,9 @@ require 'models/tag' require 'models/tagging' require 'models/treasure' require 'models/company' +require 'models/author' +require 'models/etch' +require 'models/scribbling' class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase def test_autosave_should_be_a_valid_option_for_has_one @@ -1277,3 +1280,11 @@ class TestAutosaveAssociationWithTouch < ActiveRecord::TestCase assert_nothing_raised { invoice.line_items.create(:amount => 10) } end end + +class TestAutsaveAssociationWithPolymorphic < ActiveRecord::TestCase + def test_after_create_does_not_create_multiple_through_records + Etch.create!(:name => "etch") + author = Author.create!(:name => "Alfred Embiggins") + assert_equal 1, Scribbling.count + end +end diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 7279784..5d57bf4 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -1,6 +1,10 @@ class Author < ActiveRecord::Base has_many :posts has_many :very_special_comments, :through => :posts + + has_many :scribblings + has_many :etches, :through => :scribblings, :source => :wording, :source_type => 'Etch' + has_many :posts_with_comments, :include => :comments, :class_name => "Post" has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type" has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id' @@ -96,6 +100,8 @@ class Author < ActiveRecord::Base attr_accessor :post_log after_initialize :set_post_log + after_save :create_etch + def set_post_log @post_log = [] end @@ -130,6 +136,13 @@ class Author < ActiveRecord::Base def raise_exception(object) raise Exception.new("You can't add a post") end + + def create_etch + etch = Etch.last + Scribbling.create!(:author => self, :wording => etch) + self.etches + true + end end class AuthorAddress < ActiveRecord::Base diff --git a/activerecord/test/models/etch.rb b/activerecord/test/models/etch.rb new file mode 100644 index 0000000..70e2a67 --- /dev/null +++ b/activerecord/test/models/etch.rb @@ -0,0 +1,3 @@ +class Etch < ActiveRecord::Base + has_many :scribblings, :as => :wording +end diff --git a/activerecord/test/models/scribbling.rb b/activerecord/test/models/scribbling.rb new file mode 100644 index 0000000..d914335 --- /dev/null +++ b/activerecord/test/models/scribbling.rb @@ -0,0 +1,5 @@ +require 'pp' +class Scribbling < ActiveRecord::Base + belongs_to :author + belongs_to :wording, :polymorphic => true +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index f3fd37c..7673c01 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -616,6 +616,21 @@ ActiveRecord::Schema.define do t.datetime :updated_at end + create_table "etches", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "scribblings", :force => true do |t| + t.integer "author_id" + t.integer "wording_id" + t.string "wording_type" + t.datetime "created_at" + t.datetime "updated_at" + end + + except 'SQLite' do # fk_test_has_fk should be before fk_test_has_pk create_table :fk_test_has_fk, :force => true do |t| -- 1.7.0.4