From f76517d0fa49103547ac76c94dd8e50fcc2c646b Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Tue, 22 Feb 2011 11:01:40 +0100 Subject: [PATCH] Narrow the rescued exception to avoid catching class evaluation errors that has nothing to to with the association. Example: /Users/weppos/.rvm/gems/ruby-1.8.7-p330/gems/couchrest_model-1.0.0/lib/couchrest/model/associations.rb:29:in `belongs_to': Unable to convert class name into Constant for Document#dossier (RuntimeError) although the real error is /Users/weppos/Projects/working/.../app/models/user.rb:56: undefined method `validates_uniqueness_of' for User:Class (NoMethodError) --- lib/couchrest/model/associations.rb | 4 ++-- spec/couchrest/assocations_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/couchrest/model/associations.rb b/lib/couchrest/model/associations.rb index 889ff5c..ccebcf8 100644 --- a/lib/couchrest/model/associations.rb +++ b/lib/couchrest/model/associations.rb @@ -25,8 +25,8 @@ module CouchRest begin opts[:class] = opts[:class_name].constantize - rescue - raise "Unable to convert class name into Constant for #{self.name}##{attrib}" + rescue NameError + raise NameError, "Unable to convert class name into Constant for #{self.name}##{attrib}" end prop = property(opts[:foreign_key], opts) diff --git a/spec/couchrest/assocations_spec.rb b/spec/couchrest/assocations_spec.rb index fe16970..36e0e5d 100644 --- a/spec/couchrest/assocations_spec.rb +++ b/spec/couchrest/assocations_spec.rb @@ -44,11 +44,11 @@ describe "Assocations" do end it "should raise error if class name does not exist" do - lambda { + lambda do class TestBadAssoc < CouchRest::Model::Base belongs_to :test_bad_item end - }.should raise_error + end.should raise_error(NameError, /TestBadAssoc#test_bad_item/) end it "should allow override of foreign key" do