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.
This commit is contained in:
Cliff Braton 2017-04-03 22:24:33 -05:00 committed by GitHub
parent 3ad38b6187
commit 9490a57812
3 changed files with 17 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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