Permalink
Browse files

return value of block is returned

  • Loading branch information...
1 parent f0182d5 commit de6a067078ade6d4c9775ebd9d05ebb94b3a6b54 @tenderlove tenderlove committed Apr 30, 2012
Showing with 14 additions and 3 deletions.
  1. +5 −2 activerecord/lib/active_record/relation/finder_methods.rb
  2. +9 −1 activerecord/test/cases/base_test.rb
@@ -217,8 +217,11 @@ def find_by_attributes(match, attributes, *args)
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
- result
+ if block_given? && result
+ yield(result)
+ else
+ result
+ end
end
end
@@ -180,9 +180,17 @@ def test_finder_block
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
- Topic.find_by_id(bad_id) { |f| raise }
+ 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

0 comments on commit de6a067

Please sign in to comment.