Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added more specific plugin error reporting.

  • Loading branch information...
commit 5d01367e7c9cc5ae02ddd0a5507746d471a5bd6d 1 parent f35dba2
@nbibler nbibler authored
Showing with 25 additions and 1 deletion.
  1. +17 −1 lib/find_by_param.rb
  2. +8 −0 test/find_by_param_test.rb
View
18 lib/find_by_param.rb
@@ -1,4 +1,20 @@
module FindByParam
+
+ ##
+ # Catch-all error for any issue arrising within the FindByParam plugin.
+ #
+ class Error < RuntimeError; end
+
+ ##
+ # Raised when the param requested is not in the model's table definition.
+ # For example:
+ #
+ # class WillRaiseError < ActiveRecord::Base
+ # define_find_param :undefined_column
+ # end
+ #
+ class ColumnNotFoundError < Error; end
+
module ClassMethods
def define_find_param(param, options={})
param = param.to_s
@@ -13,7 +29,7 @@ def define_find_param(param, options={})
end
self.class.send(:define_method, 'find_by_param', &bl)
else
- raise StandardError
+ raise ColumnNotFoundError
end
self.send(:include, FindByParam::InstanceMethods)
end
View
8 test/find_by_param_test.rb
@@ -38,4 +38,12 @@ def test_raises_on_not_found_if_specified
BlogPost.find_by_param('in ur tests, failing')
end
end
+
+ def test_raises_column_not_found_error_when_given_undefined_column
+ assert_raise(FindByParam::ColumnNotFoundError) do
+ BlogPost.send(:define_find_param, 'bad_column_name')
+ end
+ assert_kind_of FindByParam::Error, FindByParam::ColumnNotFoundError.new
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.