Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 0e92f36d7506ebb7248d046e19299553edad6f53 1 parent f1880ca
David Heinemeier Hansson dhh authored
2  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -82,8 +82,10 @@ def find(*args)
82 82 def push_with_attributes(record, join_attributes = {})
83 83 raise_on_type_mismatch(record)
84 84 join_attributes.each { |key, value| record[key.to_s] = value }
  85 + callback(:before_add, record)
85 86 insert_record(record) unless @owner.new_record?
86 87 @target << record
  88 + callback(:after_add, record)
87 89 self
88 90 end
89 91
32 activerecord/test/association_callbacks_test.rb
@@ -10,10 +10,10 @@ class AssociationCallbacksTest < Test::Unit::TestCase
10 10 fixtures :posts, :authors, :projects, :developers
11 11
12 12 def setup
13   - @david = authors(:david)
14   - @thinking = posts(:thinking)
15   - @authorless = posts(:authorless)
16   - assert @david.post_log.empty?
  13 + @david = authors(:david)
  14 + @thinking = posts(:thinking)
  15 + @authorless = posts(:authorless)
  16 + assert @david.post_log.empty?
17 17 end
18 18
19 19 def test_adding_macro_callbacks
@@ -74,14 +74,14 @@ def test_has_and_belongs_to_many_add_callback
74 74 def test_has_and_belongs_to_many_remove_callback
75 75 david = developers(:david)
76 76 jamis = developers(:jamis)
77   - ar = projects(:active_record)
78   - assert ar.developers_log.empty?
79   - ar.developers_with_callbacks.delete(david)
80   - assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], ar.developers_log
  77 + activerecord = projects(:active_record)
  78 + assert activerecord.developers_log.empty?
  79 + activerecord.developers_with_callbacks.delete(david)
  80 + assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log
81 81
82   - ar.developers_with_callbacks.delete(jamis)
  82 + activerecord.developers_with_callbacks.delete(jamis)
83 83 assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}",
84   - "after_removing#{jamis.id}"], ar.developers_log
  84 + "after_removing#{jamis.id}"], activerecord.developers_log
85 85 end
86 86
87 87 def test_dont_add_if_before_callback_raises_exception
@@ -95,6 +95,16 @@ def test_dont_add_if_before_callback_raises_exception
95 95 @david.reload
96 96 assert !@david.unchangable_posts.include?(@authorless)
97 97 end
98   -
  98 +
  99 + def test_push_with_attributes
  100 + david = developers(:david)
  101 + activerecord = projects(:active_record)
  102 + assert activerecord.developers_log.empty?
  103 + activerecord.developers_with_callbacks.push_with_attributes(david, {})
  104 + assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log
  105 + activerecord.developers_with_callbacks.push_with_attributes(david, {})
  106 + assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
  107 + "after_adding#{david.id}"], activerecord.developers_log
  108 + end
99 109 end
100 110

0 comments on commit 0e92f36

Please sign in to comment.
Something went wrong with that request. Please try again.