From 6e28a6152de0e211e46c84a7b5d86d2810b27b24 Mon Sep 17 00:00:00 2001 From: Trevor Turk Date: Fri, 23 May 2008 15:51:56 -0500 Subject: [PATCH] add failing test case for block-setting of attributes via association --- activerecord/test/cases/associations_test.rb | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 3ad8c60..034fe14 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -160,6 +160,15 @@ class AssociationProxyTest < ActiveRecord::TestCase assert_equal 1, developer.reload.audit_logs.size end + def test_create_via_association_with_block + post1 = Post.create(:title => "setting body with a block") {|p| p.body = "will work"} + assert_equal post1.body, "will work" + assert_nothing_raised do + post2 = authors(:david).posts.create(:title => "setting body with a block") {|p| p.body = "won't work"} + end + assert_equal post2.body, "won't work" + end + def test_failed_reload_returns_nil p = setup_dangling_association assert_nil p.author.reload -- 1.5.4 From 5a245fb74d6566f59c98310e82ddea9ccb083d54 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Fri, 23 May 2008 14:57:11 -0700 Subject: [PATCH] create through association can now accept a block --- .../associations/association_collection.rb | 10 ++++++++-- activerecord/test/cases/associations_test.rb | 15 +++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 2d3750e..2f03197 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -166,12 +166,18 @@ module ActiveRecord if attrs.is_a?(Array) attrs.collect { |attr| create(attr) } else - create_record(attrs) { |record| record.save } + create_record(attrs) do |record| + yield(record) if block_given? + record.save + end end end def create!(attrs = {}) - create_record(attrs) { |record| record.save! } + create_record(attrs) do |record| + yield(record) if block_given? + record.save! + end end # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 034fe14..59349dd 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -161,12 +161,15 @@ class AssociationProxyTest < ActiveRecord::TestCase end def test_create_via_association_with_block - post1 = Post.create(:title => "setting body with a block") {|p| p.body = "will work"} - assert_equal post1.body, "will work" - assert_nothing_raised do - post2 = authors(:david).posts.create(:title => "setting body with a block") {|p| p.body = "won't work"} - end - assert_equal post2.body, "won't work" + post = authors(:david).posts.create(:title => "New on Edge") {|p| p.body = "More cool stuff!"} + assert_equal post.title, "New on Edge" + assert_equal post.body, "More cool stuff!" + end + + def test_create_with_bang_via_association_with_block + post = authors(:david).posts.create!(:title => "New on Edge") {|p| p.body = "More cool stuff!"} + assert_equal post.title, "New on Edge" + assert_equal post.body, "More cool stuff!" end def test_failed_reload_returns_nil -- 1.5.4