Created
April 30, 2012 22:57
-
-
Save tenderlove/2563454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb | |
index 416b55f..1ceb194 100644 | |
--- a/activerecord/lib/active_record/relation/finder_methods.rb | |
+++ b/activerecord/lib/active_record/relation/finder_methods.rb | |
@@ -217,8 +217,11 @@ module ActiveRecord | |
if match.bang? && result.blank? | |
raise RecordNotFound, "Couldn't find #{@klass.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}" | |
else | |
- yield(result) if block_given? | |
- result | |
+ if block_given? && result | |
+ yield(result) | |
+ else | |
+ result | |
+ end | |
end | |
end | |
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb | |
index 8a4ce5e..da4dfb4 100644 | |
--- a/activerecord/test/cases/base_test.rb | |
+++ b/activerecord/test/cases/base_test.rb | |
@@ -175,6 +175,24 @@ class BasicsTest < ActiveRecord::TestCase | |
assert Topic.table_exists? | |
end | |
+ def test_finder_block | |
+ t = Topic.first | |
+ found = nil | |
+ Topic.find_by_id(t.id) { |f| found = f } | |
+ assert_equal t, found | |
+ end | |
+ | |
+ def test_finder_block_nothing_found | |
+ bad_id = Topic.maximum(:id) + 1 | |
+ assert_nil Topic.find_by_id(bad_id) { |f| raise } | |
+ end | |
+ | |
+ def test_find_returns_block_value | |
+ t = Topic.first | |
+ x = Topic.find_by_id(t.id) { |f| "hi mom!" } | |
+ assert_equal "hi mom!", x | |
+ end | |
+ | |
def test_preserving_date_objects | |
if current_adapter?(:SybaseAdapter) | |
# Sybase ctlib does not (yet?) support the date type; use datetime instead. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment