diff --git a/lib/draper/decoratable.rb b/lib/draper/decoratable.rb index 70ae4f9..2c39dd6 100644 --- a/lib/draper/decoratable.rb +++ b/lib/draper/decoratable.rb @@ -23,6 +23,10 @@ module Draper self.class.decorator_class end + def decorator_class? + self.class.decorator_class? + end + # The list of decorators that have been applied to the object. # # @return [Array] `[]` @@ -56,6 +60,12 @@ module Draper decorator_class.decorate_collection(collection, options.reverse_merge(with: nil)) end + def decorator_class? + decorator_class + rescue Draper::UninferrableDecoratorError + false + end + # Infers the decorator class to be used by {Decoratable#decorate} (e.g. # `Product` maps to `ProductDecorator`). # diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index e880acb..46ef103 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -46,6 +46,26 @@ module Draper end end + describe "#decorator_class?" do + it "returns true for decoratable model" do + expect(Product.new.decorator_class?).to be_true + end + + it "returns false for non-decoratable model" do + expect(Model.new.decorator_class?).to be_false + end + end + + describe ".decorator_class?" do + it "returns true for decoratable model" do + expect(Product.decorator_class?).to be_true + end + + it "returns false for non-decoratable model" do + expect(Model.decorator_class?).to be_false + end + end + describe "#decorator_class" do it "delegates to .decorator_class" do product = Product.new