From 10eac99b443ee6de52d9201f76ddec8bdfa11b19 Mon Sep 17 00:00:00 2001 From: Michael Schuerig Date: Sun, 10 Nov 2013 18:02:34 +0100 Subject: [PATCH] Inherit decorator from superclass. If the class itself does not have a decorator, try the superclass. --- lib/draper/decoratable.rb | 8 ++++++-- spec/draper/decoratable_spec.rb | 6 ++++++ spec/spec_helper.rb | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/draper/decoratable.rb b/lib/draper/decoratable.rb index 2c39dd6..3771e57 100644 --- a/lib/draper/decoratable.rb +++ b/lib/draper/decoratable.rb @@ -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. diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 46ef103..023d5d6 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2e3d2a2..9e1334c 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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