When decoration fails, show original class (#821)
This commit is contained in:
parent
cc23669889
commit
6f76c64528
|
@ -69,16 +69,16 @@ module Draper
|
||||||
# `Product` maps to `ProductDecorator`).
|
# `Product` maps to `ProductDecorator`).
|
||||||
#
|
#
|
||||||
# @return [Class] the inferred decorator class.
|
# @return [Class] the inferred decorator class.
|
||||||
def decorator_class
|
def decorator_class(called_on = self)
|
||||||
prefix = respond_to?(:model_name) ? model_name : name
|
prefix = respond_to?(:model_name) ? model_name : name
|
||||||
decorator_name = "#{prefix}Decorator"
|
decorator_name = "#{prefix}Decorator"
|
||||||
decorator_name_constant = decorator_name.safe_constantize
|
decorator_name_constant = decorator_name.safe_constantize
|
||||||
return decorator_name_constant unless decorator_name_constant.nil?
|
return decorator_name_constant unless decorator_name_constant.nil?
|
||||||
|
|
||||||
if superclass.respond_to?(:decorator_class)
|
if superclass.respond_to?(:decorator_class)
|
||||||
superclass.decorator_class
|
superclass.decorator_class(called_on)
|
||||||
else
|
else
|
||||||
raise Draper::UninferrableDecoratorError.new(self)
|
raise Draper::UninferrableDecoratorError.new(called_on)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -73,6 +73,16 @@ module Draper
|
||||||
expect(Product).to receive(:decorator_class).and_return(:some_decorator)
|
expect(Product).to receive(:decorator_class).and_return(:some_decorator)
|
||||||
expect(product.decorator_class).to be :some_decorator
|
expect(product.decorator_class).to be :some_decorator
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "#==" do
|
describe "#==" do
|
||||||
|
|
|
@ -16,6 +16,8 @@ class Model; include Draper::Decoratable; end
|
||||||
class Product < Model; end
|
class Product < Model; end
|
||||||
class SpecialProduct < Product; end
|
class SpecialProduct < Product; end
|
||||||
class Other < Model; end
|
class Other < Model; end
|
||||||
|
class Person < Model; end
|
||||||
|
class Child < Person; end
|
||||||
class ProductDecorator < Draper::Decorator; end
|
class ProductDecorator < Draper::Decorator; end
|
||||||
class ProductsDecorator < Draper::CollectionDecorator; end
|
class ProductsDecorator < Draper::CollectionDecorator; end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue