From a001547bf2fea9905384d2548682cd06b98d0ce2 Mon Sep 17 00:00:00 2001 From: Ken Miller Date: Wed, 15 Oct 2008 17:34:57 -0700 Subject: [PATCH] Fixed issue where block is not called on the very first invocation of a find_or_create_by_ automatic finder. --- activerecord/lib/active_record/base.rb | 4 ++-- activerecord/test/cases/finder_test.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6a1a379..0332348 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1763,7 +1763,7 @@ module ActiveRecord #:nodoc: # # Each dynamic finder or initializer/creator is also defined in the class after it is first invoked, so that future # attempts to use it do not run through method_missing. - def method_missing(method_id, *arguments) + def method_missing(method_id, *arguments, &block) if match = DynamicFinderMatch.match(method_id) attribute_names = match.attribute_names super unless all_attributes_exists?(attribute_names) @@ -1818,7 +1818,7 @@ module ActiveRecord #:nodoc: end end }, __FILE__, __LINE__ - send(method_id, *arguments) + send(method_id, *arguments, &block) end else super diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 8534749..af9528f 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -846,6 +846,25 @@ class FinderTest < ActiveRecord::TestCase assert !c.new_record? end + def test_find_or_create_should_work_with_block_on_first_call + class << Company + undef_method(:find_or_create_by_name) if method_defined?(:find_or_create_by_name) + end + c = Company.find_or_create_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 } + assert_equal "Fortune 1000", c.name + assert_equal 1000.to_f, c.rating.to_f + assert c.valid? + assert !c.new_record? + end + +# def test_find_or_create_should_work_with_block_on_first_call +# c = Company.find_or_create_by_rating(1000000000) { |f| f.name = 'Fortune 1000000000' } +# assert_equal "Fortune 1000000000", c.name +# assert_equal 1000_000_000.to_f, c.rating.to_f +# assert c.valid? +# assert !c.new_record? +# end +# def test_dynamic_find_or_initialize_from_one_attribute_caches_method class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' } assert !Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' } -- 1.6.0.1 From 633ad557f296412001ec6060ccedfeeecba38cec Mon Sep 17 00:00:00 2001 From: Ken Miller Date: Wed, 15 Oct 2008 17:55:30 -0700 Subject: [PATCH] Removing cruft from a finder_test.rb --- activerecord/test/cases/finder_test.rb | 8 -------- 1 files changed, 0 insertions(+), 8 deletions(-) diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index af9528f..153880a 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -857,14 +857,6 @@ class FinderTest < ActiveRecord::TestCase assert !c.new_record? end -# def test_find_or_create_should_work_with_block_on_first_call -# c = Company.find_or_create_by_rating(1000000000) { |f| f.name = 'Fortune 1000000000' } -# assert_equal "Fortune 1000000000", c.name -# assert_equal 1000_000_000.to_f, c.rating.to_f -# assert c.valid? -# assert !c.new_record? -# end -# def test_dynamic_find_or_initialize_from_one_attribute_caches_method class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' } assert !Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' } -- 1.6.0.1