Skip to content

Commit

Permalink
Added test for raise_on_not_found
Browse files Browse the repository at this point in the history
  • Loading branch information
jseifer committed Mar 16, 2008
1 parent fd735cb commit 1646424
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/find_by_param.rb
Expand Up @@ -8,7 +8,7 @@ def define_find_param(param, options={})
write_inheritable_attribute :find_param_options, options write_inheritable_attribute :find_param_options, options
bl = lambda do |args| bl = lambda do |args|
results = send("find_by_#{read_inheritable_attribute(:find_parameter)}", *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 return results
end end
self.class.send(:define_method, 'find_by_param', &bl) self.class.send(:define_method, 'find_by_param', &bl)
Expand Down
7 changes: 7 additions & 0 deletions test/find_by_param_test.rb
Expand Up @@ -30,4 +30,11 @@ def test_correctly_goes_to_param
bp = BlogPost.find(:first, :conditions => {:slug => 'adam-west'}) bp = BlogPost.find(:first, :conditions => {:slug => 'adam-west'})
assert_equal bp.to_param, 'adam-west' assert_equal bp.to_param, 'adam-west'
end 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 end
1 change: 0 additions & 1 deletion test/test_helper.rb
Expand Up @@ -28,5 +28,4 @@
end end


class BlogPost < ActiveRecord::Base class BlogPost < ActiveRecord::Base
define_find_param :slug
end end

0 comments on commit 1646424

Please sign in to comment.