When decoration fails, show original class (#821)

This commit is contained in:
Pascal Betz 2017-10-12 18:55:00 +02:00 committed by Cliff Braton
parent cc23669889
commit 6f76c64528
3 changed files with 15 additions and 3 deletions

View File

@ -69,16 +69,16 @@ module Draper
# `Product` maps to `ProductDecorator`).
#
# @return [Class] the inferred decorator class.
def decorator_class
def decorator_class(called_on = self)
prefix = respond_to?(:model_name) ? model_name : name
decorator_name = "#{prefix}Decorator"
decorator_name_constant = decorator_name.safe_constantize
return decorator_name_constant unless decorator_name_constant.nil?
if superclass.respond_to?(:decorator_class)
superclass.decorator_class
superclass.decorator_class(called_on)
else
raise Draper::UninferrableDecoratorError.new(self)
raise Draper::UninferrableDecoratorError.new(called_on)
end
end

View File

@ -73,6 +73,16 @@ module Draper
expect(Product).to receive(:decorator_class).and_return(:some_decorator)
expect(product.decorator_class).to be :some_decorator
end
it "specifies the class that #decorator_class was first called on (superclass)" do
person = Person.new
expect { person.decorator_class }.to raise_error(Draper::UninferrableDecoratorError, 'Could not infer a decorator for Person.')
end
it "specifies the class that #decorator_class was first called on (subclass)" do
child = Child.new
expect { child.decorator_class }.to raise_error(Draper::UninferrableDecoratorError, 'Could not infer a decorator for Child.')
end
end
describe "#==" do

View File

@ -16,6 +16,8 @@ class Model; include Draper::Decoratable; end
class Product < Model; end
class SpecialProduct < Product; end
class Other < Model; end
class Person < Model; end
class Child < Person; end
class ProductDecorator < Draper::Decorator; end
class ProductsDecorator < Draper::CollectionDecorator; end