diff --git a/lib/draper/decoratable.rb b/lib/draper/decoratable.rb index 8804ed0..94500cb 100644 --- a/lib/draper/decoratable.rb +++ b/lib/draper/decoratable.rb @@ -74,10 +74,10 @@ module Draper decorator_name = "#{prefix}Decorator" decorator_name.constantize rescue NameError => error + raise unless error.missing_name?(decorator_name) if superclass.respond_to?(:decorator_class) superclass.decorator_class else - raise unless error.missing_name?(decorator_name) raise Draper::UninferrableDecoratorError.new(self) end end diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 0da6d5e..269bfc4 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -182,6 +182,15 @@ module Draper end end + context "when the decorator contains name error" do + it "throws an NameError" do + # We imitate ActiveSupport::Autoload behavior here in order to cause lazy NameError exception raising + allow_any_instance_of(Module).to receive(:const_missing) { Class.new { any_nonexisting_method_name } } + + expect{Model.decorator_class}.to raise_error { |error| expect(error).to be_an_instance_of(NameError) } + end + end + context "when the decorator can't be inferred" do it "throws an UninferrableDecoratorError" do expect{Model.decorator_class}.to raise_error UninferrableDecoratorError