diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-05 07:20:24 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-05 07:20:24 +0000 |
commit | 0e92f36d7506ebb7248d046e19299553edad6f53 (patch) | |
tree | 4efe2f16a4c57a466154ec12b64241c88344ff60 | |
parent | f1880cac5862172608ff26d1178a31c05b904d77 (diff) | |
download | rails-0e92f36d7506ebb7248d046e19299553edad6f53.tar.gz rails-0e92f36d7506ebb7248d046e19299553edad6f53.tar.bz2 rails-0e92f36d7506ebb7248d046e19299553edad6f53.zip |
Added callbacks on push_with_attributes #1594 [Florian Weber]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1698 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 2 | ||||
-rw-r--r-- | activerecord/test/association_callbacks_test.rb | 32 |
2 files changed, 23 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index da43e32fb6..39a6a7ed5f 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -82,8 +82,10 @@ module ActiveRecord def push_with_attributes(record, join_attributes = {}) raise_on_type_mismatch(record) join_attributes.each { |key, value| record[key.to_s] = value } + callback(:before_add, record) insert_record(record) unless @owner.new_record? @target << record + callback(:after_add, record) self end diff --git a/activerecord/test/association_callbacks_test.rb b/activerecord/test/association_callbacks_test.rb index 359f853397..5ea1f541ed 100644 --- a/activerecord/test/association_callbacks_test.rb +++ b/activerecord/test/association_callbacks_test.rb @@ -10,10 +10,10 @@ class AssociationCallbacksTest < Test::Unit::TestCase fixtures :posts, :authors, :projects, :developers def setup - @david = authors(:david) - @thinking = posts(:thinking) - @authorless = posts(:authorless) - assert @david.post_log.empty? + @david = authors(:david) + @thinking = posts(:thinking) + @authorless = posts(:authorless) + assert @david.post_log.empty? end def test_adding_macro_callbacks @@ -74,14 +74,14 @@ class AssociationCallbacksTest < Test::Unit::TestCase def test_has_and_belongs_to_many_remove_callback david = developers(:david) jamis = developers(:jamis) - ar = projects(:active_record) - assert ar.developers_log.empty? - ar.developers_with_callbacks.delete(david) - assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], ar.developers_log + activerecord = projects(:active_record) + assert activerecord.developers_log.empty? + activerecord.developers_with_callbacks.delete(david) + assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log - ar.developers_with_callbacks.delete(jamis) + activerecord.developers_with_callbacks.delete(jamis) assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}", - "after_removing#{jamis.id}"], ar.developers_log + "after_removing#{jamis.id}"], activerecord.developers_log end def test_dont_add_if_before_callback_raises_exception @@ -95,6 +95,16 @@ class AssociationCallbacksTest < Test::Unit::TestCase @david.reload assert !@david.unchangable_posts.include?(@authorless) end - + + def test_push_with_attributes + david = developers(:david) + activerecord = projects(:active_record) + assert activerecord.developers_log.empty? + activerecord.developers_with_callbacks.push_with_attributes(david, {}) + assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log + activerecord.developers_with_callbacks.push_with_attributes(david, {}) + assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}", + "after_adding#{david.id}"], activerecord.developers_log + end end |