GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/ddollar/rails.git
if a hash is passed into an association proxy setter, hydrate it as an 
ActiveRecord model instead
ddollar (author)
Mon Jun 23 11:25:47 -0700 2008
commit  fb20da9288fb68343f8694353c73511630be939b
tree    8f6425f2cc07b4d48e6f504405aac61ee00bba2d
parent  a9259ccfe05690dc4cb7993d551603c39619c27f
...
1105
1106
1107
 
 
 
 
1108
1109
1110
...
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
0
@@ -1105,6 +1105,10 @@ module ActiveRecord
0
               association = association_proxy_class.new(self, reflection)
0
             end
0
 
0
+ # if a Hash is passed in, convert it to an ActiveRecord model
0
+ # using the Hash as the initialization attributes
0
+ new_value = reflection.klass.new(new_value) if new_value.is_a?(Hash)
0
+
0
             if association_proxy_class == HasOneThroughAssociation
0
               association.create_through_record(new_value)
0
               self.send(reflection.name, new_value)
...
94
95
96
 
 
 
 
 
97
98
99
...
94
95
96
97
98
99
100
101
102
103
104
0
@@ -94,6 +94,11 @@ module ActiveRecord
0
 
0
         @owner.transaction do
0
           flatten_deeper(records).each do |record|
0
+
0
+ # if a Hash is passed in, convert it to an ActiveRecord model
0
+ # using the Hash as the initialization attributes
0
+ record = @reflection.klass.new(record) if record.is_a?(Hash)
0
+
0
             raise_on_type_mismatch(record)
0
             add_record_to_target_with_callbacks(record) do |r|
0
               result &&= insert_record(record) unless @owner.new_record?
...
189
190
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
193
194
...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
0
@@ -189,6 +189,24 @@ class AssociationProxyTest < ActiveRecord::TestCase
0
     end
0
   end
0
 
0
+ def test_association_proxy_setter_can_take_hash
0
+ special_comment_attributes = { :body => 'Setter Takes Hash' }
0
+
0
+ post = posts(:welcome)
0
+ post.very_special_comment = { :body => 'Setter Takes Hash' }
0
+
0
+ assert_equal post.very_special_comment.body, special_comment_attributes[:body]
0
+ end
0
+
0
+ def test_association_collection_can_take_hash
0
+ post_attributes = { :title => 'Setter Takes', :body => 'Hash' }
0
+
0
+ david = authors(:david)
0
+ post = (david.posts << post_attributes).last
0
+
0
+ assert_equal post.title, post_attributes[:title]
0
+ end
0
+
0
   def setup_dangling_association
0
     josh = Author.create(:name => "Josh")
0
     p = Post.create(:title => "New on Edge", :body => "More cool stuff!", :author => josh)

Comments

    No one has commented yet.