Browse files

Added test for raise_on_not_found

  • Loading branch information...
1 parent fd735cb commit 1646424e603cc430a267ec7a5e85d18983e86ad7 @jseifer committed Mar 16, 2008
Showing with 8 additions and 2 deletions.
  1. +1 −1 lib/find_by_param.rb
  2. +7 −0 test/find_by_param_test.rb
  3. +0 −1 test/test_helper.rb
View
2 lib/find_by_param.rb
@@ -8,7 +8,7 @@ def define_find_param(param, options={})
write_inheritable_attribute :find_param_options, options
bl = lambda do |args|
results = send("find_by_#{read_inheritable_attribute(:find_parameter)}", *args)
- raise ActiveRecord::RecordNotFound if options[:raise_on_not_found] && (results.nil? or (results.is_a?(Array) && results.empty?))
+ raise ActiveRecord::RecordNotFound if options[:raise_on_not_found] && (results.nil? or (results.is_a?(Array) && results.size == 0))
return results
end
self.class.send(:define_method, 'find_by_param', &bl)
View
7 test/find_by_param_test.rb
@@ -30,4 +30,11 @@ def test_correctly_goes_to_param
bp = BlogPost.find(:first, :conditions => {:slug => 'adam-west'})
assert_equal bp.to_param, 'adam-west'
end
+
+ def test_raises_on_not_found_if_specified
+ BlogPost.send(:define_find_param, 'slug', :raise_on_not_found => true)
+ assert_raises ActiveRecord::RecordNotFound do
+ BlogPost.find_by_param('in ur tests, failing')
+ end
+ end
end
View
1 test/test_helper.rb
@@ -28,5 +28,4 @@
end
class BlogPost < ActiveRecord::Base
- define_find_param :slug
end

0 comments on commit 1646424

Please sign in to comment.