From 9490a5781266fc7b6834620ce10ce870677e9d98 Mon Sep 17 00:00:00 2001 From: Cliff Braton Date: Mon, 3 Apr 2017 22:24:33 -0500 Subject: [PATCH] Never re-raise NameError and always default to CollectionDecorator (#795) * minor fix to error raising on collection decoration. * Don't re-raise NameError in collection_decorator_class. * Add more tests. --- lib/draper/decorator.rb | 3 +-- spec/draper/decorator_spec.rb | 22 +++++++++++++++------- spec/spec_helper.rb | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/draper/decorator.rb b/lib/draper/decorator.rb index 2090b40..4f30066 100644 --- a/lib/draper/decorator.rb +++ b/lib/draper/decorator.rb @@ -223,8 +223,7 @@ module Draper def self.collection_decorator_class name = collection_decorator_name name.constantize - rescue NameError => error - raise if name && !error.missing_name?(name) + rescue NameError Draper::CollectionDecorator end diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index f4f8e0a..c188ce1 100644 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -145,13 +145,6 @@ module Draper ProductDecorator.decorate_collection([], options) end end - - context "when a NameError is thrown" do - it "re-raises that error" do - allow_any_instance_of(String).to receive(:constantize) { Draper::DecoratedEnumerableProxy } - expect{ProductDecorator.decorate_collection([])}.to raise_error NameError, /Draper::DecoratedEnumerableProxy/ - end - end end describe ".decorates" do @@ -230,6 +223,21 @@ module Draper end end + describe '.collection_decorator_class' do + it 'defaults to CollectionDecorator' do + allow_any_instance_of(String).to receive(:constantize) { SomethingThatDoesntExist } + expect(ProductDecorator.collection_decorator_class).to be Draper::CollectionDecorator + end + + it 'infers collection decorator based on name' do + expect(ProductDecorator.collection_decorator_class).to be ProductsDecorator + end + + it 'infers collection decorator base on name for namespeced model' do + expect(Namespaced::ProductDecorator.collection_decorator_class).to be Namespaced::ProductsDecorator + end + end + describe ".decorates_association" do protect_class Decorator diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 45abd7c..e38ad6f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,7 +24,7 @@ class OtherDecorator < Draper::Decorator; end module Namespaced class Product < Model; end class ProductDecorator < Draper::Decorator; end - + ProductsDecorator = Class.new(Draper::CollectionDecorator) class OtherDecorator < Draper::Decorator; end end