1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Inherit decorator from superclass.

If the class itself does not have a decorator, try the superclass.
This commit is contained in:
Michael Schuerig 2013-11-10 18:02:34 +01:00
parent ac9dcd8f22
commit 10eac99b44
3 changed files with 13 additions and 2 deletions

View file

@ -75,8 +75,12 @@ module Draper
decorator_name = "#{prefix}Decorator"
decorator_name.constantize
rescue NameError => error
raise unless error.missing_name?(decorator_name)
raise Draper::UninferrableDecoratorError.new(self)
if superclass.respond_to?(:decorator_class)
superclass.decorator_class
else
raise unless error.missing_name?(decorator_name)
raise Draper::UninferrableDecoratorError.new(self)
end
end
# Compares with possibly-decorated objects.

View file

@ -152,6 +152,12 @@ module Draper
it "infers the decorator from the class" do
expect(Product.decorator_class).to be ProductDecorator
end
context "without a decorator on its own" do
it "infers the decorator from a superclass" do
expect(SpecialProduct.decorator_class).to be ProductDecorator
end
end
end
context "for ActiveModel classes" do

View file

@ -13,6 +13,7 @@ end
class Model; include Draper::Decoratable; end
class Product < Model; end
class SpecialProduct < Product; end
class ProductDecorator < Draper::Decorator; end
class ProductsDecorator < Draper::CollectionDecorator; end