public
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/NZKoz/koz-rails.git
Search Repo:
Make HABTM#create behave the same as << with after_add callbacks.  
Closes #11374 [freels]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9224 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
nzkoz (author)
Fri Apr 04 07:37:22 -0700 2008
commit  0b273182c8703ede509a16a4340b5e75e85e382f
tree    db61c081ffdbc5ae43426dbad23d5baa5bd2daf7
parent  5acb1840f62ea6de1352dee8f9c64a13845072c0
...
148
149
150
151
 
152
153
154
155
156
157
158
159
 
 
160
161
162
...
148
149
150
 
151
152
153
154
155
156
 
 
 
157
158
159
160
161
0
@@ -148,15 +148,14 @@ module ActiveRecord
0
         end
0
 
0
       private
0
- def create_record(attributes)
0
+ def create_record(attributes, &block)
0
           # Can't use Base.create because the foreign key may be a protected attribute.
0
           ensure_owner_is_not_new
0
           if attributes.is_a?(Array)
0
             attributes.collect { |attr| create(attr) }
0
           else
0
- record = build(attributes)
0
- yield(record)
0
- record
0
+ load_target
0
+ build_record(attributes, &block)
0
           end
0
         end
0
     end
...
94
95
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
98
99
...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
0
@@ -94,6 +94,21 @@ class AssociationCallbacksTest < ActiveRecord::TestCase
0
                   "after_adding#{david.id}"], ar.developers_log
0
   end
0
 
0
+ def test_has_and_belongs_to_many_after_add_called_after_save
0
+ ar = projects(:active_record)
0
+ assert ar.developers_log.empty?
0
+ alice = Developer.new(:name => 'alice')
0
+ ar.developers_with_callbacks << alice
0
+ assert_equal"after_adding#{alice.id}", ar.developers_log.last
0
+
0
+ bob = ar.developers_with_callbacks.create(:name => 'bob')
0
+ assert_equal "after_adding#{bob.id}", ar.developers_log.last
0
+
0
+ ar.developers_with_callbacks.build(:name => 'charlie')
0
+ assert_equal "after_adding<new>", ar.developers_log.last
0
+ end
0
+
0
+
0
   def test_has_and_belongs_to_many_remove_callback
0
     david = developers(:david)
0
     jamis = developers(:jamis)

Comments

    No one has commented yet.