From a72c1ec43ea91c1dd9b37870c77e82da7cc2f5d8 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 16 Oct 2007 05:07:58 +0000 Subject: Refactor association create and build so before & after callbacks behave consistently. Closes #8854. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7935 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations/callbacks_test.rb | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'activerecord/test/associations/callbacks_test.rb') diff --git a/activerecord/test/associations/callbacks_test.rb b/activerecord/test/associations/callbacks_test.rb index afb1b8e84d..4b6b7f4123 100644 --- a/activerecord/test/associations/callbacks_test.rb +++ b/activerecord/test/associations/callbacks_test.rb @@ -60,6 +60,29 @@ class AssociationCallbacksTest < Test::Unit::TestCase "after_adding#{@thinking.id}", "after_adding_proc#{@thinking.id}"], @david.post_log end + def test_has_many_callbacks_with_create + morten = Author.create :name => "Morten" + post = morten.posts_with_proc_callbacks.create! :title => "Hello", :body => "How are you doing?" + assert_equal ["before_adding", "after_adding#{post.id}"], morten.post_log + end + + def test_has_many_callbacks_with_create! + morten = Author.create! :name => "Morten" + post = morten.posts_with_proc_callbacks.create :title => "Hello", :body => "How are you doing?" + assert_equal ["before_adding", "after_adding#{post.id}"], morten.post_log + end + + def test_has_many_callbacks_for_save_on_parent + jack = Author.new :name => "Jack" + post = jack.posts_with_callbacks.build :title => "Call me back!", :body => "Before you wake up and after you sleep" + + callback_log = ["before_adding", "after_adding#{jack.posts_with_callbacks.first.id}"] + assert_equal callback_log, jack.post_log + assert jack.save + assert_equal 1, jack.posts_with_callbacks.count + assert_equal callback_log, jack.post_log + end + def test_has_and_belongs_to_many_add_callback david = developers(:david) ar = projects(:active_record) @@ -98,6 +121,17 @@ class AssociationCallbacksTest < Test::Unit::TestCase assert_equal log_array, activerecord.developers_log.sort end + def test_has_many_and_belongs_to_many_callbacks_for_save_on_parent + project = Project.new :name => "Callbacks" + project.developers_with_callbacks.build :name => "Jack", :salary => 95000 + + callback_log = ["before_adding", "after_adding"] + assert_equal callback_log, project.developers_log + assert project.save + assert_equal 1, project.developers_with_callbacks.count + assert_equal callback_log, project.developers_log + end + def test_dont_add_if_before_callback_raises_exception assert !@david.unchangable_posts.include?(@authorless) begin -- cgit v1.2.3